github.com/openshift-online/ocm-sdk-go@v0.1.473/metrics_test.go (about)

     1  /*
     2  Copyright (c) 2021 Red Hat, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8    http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // This file contains tests for the Prometheus metrics.
    18  
    19  package sdk
    20  
    21  import (
    22  	"net/http"
    23  	"time"
    24  
    25  	"github.com/onsi/gomega/ghttp"
    26  
    27  	. "github.com/onsi/ginkgo/v2/dsl/core"             // nolint
    28  	. "github.com/onsi/gomega"                         // nolint
    29  	. "github.com/openshift-online/ocm-sdk-go/testing" // nolint
    30  )
    31  
    32  var _ = Describe("Metrics enabled", func() {
    33  	// Servers used during the tests:
    34  	var oidServer *ghttp.Server
    35  	var apiServer *ghttp.Server
    36  	var metricsServer *MetricsServer
    37  
    38  	// Connection used during the tests:
    39  	var connection *Connection
    40  
    41  	BeforeEach(func() {
    42  		var err error
    43  
    44  		// Create the tokens:
    45  		accessToken := MakeTokenString("Bearer", 5*time.Minute)
    46  		refreshToken := MakeTokenString("Refresh", 10*time.Hour)
    47  
    48  		// Create the OpenID server:
    49  		oidServer = MakeTCPServer()
    50  		oidServer.AppendHandlers(
    51  			ghttp.CombineHandlers(
    52  				RespondWithAccessAndRefreshTokens(accessToken, refreshToken),
    53  			),
    54  		)
    55  
    56  		// Create the API server:
    57  		apiServer = MakeTCPServer()
    58  		apiServer.AppendHandlers(
    59  			RespondWithJSON(http.StatusOK, ""),
    60  		)
    61  
    62  		// Create the metrics server:
    63  		metricsServer = NewMetricsServer()
    64  
    65  		// Create the connection:
    66  		connection, err = NewConnectionBuilder().
    67  			Logger(logger).
    68  			URL(apiServer.URL()).
    69  			TokenURL(oidServer.URL() + "/my_token").
    70  			Tokens(refreshToken).
    71  			MetricsSubsystem("my").
    72  			MetricsRegisterer(metricsServer.Registry()).
    73  			Build()
    74  		Expect(err).ToNot(HaveOccurred())
    75  	})
    76  
    77  	AfterEach(func() {
    78  		// Stop the servers:
    79  		oidServer.Close()
    80  		apiServer.Close()
    81  		metricsServer.Close()
    82  
    83  		// Close the connection:
    84  		err := connection.Close()
    85  		Expect(err).ToNot(HaveOccurred())
    86  	})
    87  
    88  	It("Generates request count for raw request", func() {
    89  		// Send the request:
    90  		_, err := connection.Get().
    91  			Path("/api/clusters_mgmt/v1/clusters/123").
    92  			Send()
    93  		Expect(err).ToNot(HaveOccurred())
    94  
    95  		// Verify the metrics:
    96  		metrics := metricsServer.Metrics()
    97  		Expect(metrics).To(MatchLine(`^my_request_count\{.*path="/api/clusters_mgmt/v1/clusters/-".*\} .*$`))
    98  	})
    99  
   100  	It("Generates request count for type safe request", func() {
   101  		// Send the request:
   102  		_, err := connection.ClustersMgmt().V1().Clusters().Cluster("123").Get().
   103  			Send()
   104  		Expect(err).ToNot(HaveOccurred())
   105  
   106  		// Verify the metrics:
   107  		metrics := metricsServer.Metrics()
   108  		Expect(metrics).To(MatchLine(`^my_request_count\{.*path="/api/clusters_mgmt/v1/clusters/-".*\} .*$`))
   109  	})
   110  
   111  	It("Generates token request count", func() {
   112  		// Send the request:
   113  		_, err := connection.ClustersMgmt().V1().Clusters().Cluster("123").Get().
   114  			Send()
   115  		Expect(err).ToNot(HaveOccurred())
   116  
   117  		// Verify the metrics:
   118  		metrics := metricsServer.Metrics()
   119  		Expect(metrics).To(MatchLine(`^my_request_count\{.*path="/my_token".*\} .*$`))
   120  		Expect(metrics).To(MatchLine(`^my_token_request_count\{attempt="1",code="200"\} .*$`))
   121  	})
   122  
   123  	It("Generates token request duration", func() {
   124  		// Send the request:
   125  		_, err := connection.ClustersMgmt().V1().Clusters().Cluster("123").Get().
   126  			Send()
   127  		Expect(err).ToNot(HaveOccurred())
   128  
   129  		// Verify the metrics:
   130  		metrics := metricsServer.Metrics()
   131  		Expect(metrics).To(MatchLine(`^my_request_duration_bucket\{.*path="/my_token".*,le="0.1"\} .*$`))
   132  		Expect(metrics).To(MatchLine(`^my_request_duration_bucket\{.*path="/my_token".*,le="1"\} .*$`))
   133  		Expect(metrics).To(MatchLine(`^my_request_duration_bucket\{.*path="/my_token".*,le="10"\} .*$`))
   134  		Expect(metrics).To(MatchLine(`^my_request_duration_bucket\{.*path="/my_token".*,le="30"\} .*$`))
   135  		Expect(metrics).To(MatchLine(`^my_request_duration_bucket\{.*path="/my_token".*,le="\+Inf"\} .*$`))
   136  		Expect(metrics).To(MatchLine(`^my_request_duration_count\{.*path="/my_token".*\} .*$`))
   137  		Expect(metrics).To(MatchLine(`^my_request_duration_sum\{.*path="/my_token"\} .*$`))
   138  		Expect(metrics).To(MatchLine(`^my_token_request_duration_bucket\{attempt="1",code="200",le="0.1"\} .*$`))
   139  		Expect(metrics).To(MatchLine(`^my_token_request_duration_bucket\{attempt="1",code="200",le="1"\} .*$`))
   140  		Expect(metrics).To(MatchLine(`^my_token_request_duration_bucket\{attempt="1",code="200",le="10"\} .*$`))
   141  		Expect(metrics).To(MatchLine(`^my_token_request_duration_bucket\{attempt="1",code="200",le="30"\} .*$`))
   142  		Expect(metrics).To(MatchLine(`^my_token_request_duration_bucket\{attempt="1",code="200",le="\+Inf"\} .*$`))
   143  		Expect(metrics).To(MatchLine(`^my_token_request_duration_count\{attempt="1",code="200"\} .*$`))
   144  		Expect(metrics).To(MatchLine(`^my_token_request_duration_sum\{attempt="1",code="200"\} .*$`))
   145  	})
   146  })
   147  
   148  var _ = Describe("Metrics disabled", func() {
   149  	// Servers used during the tests:
   150  	var oidServer *ghttp.Server
   151  	var apiServer *ghttp.Server
   152  	var metricsServer *MetricsServer
   153  
   154  	// Connection used during the tests:
   155  	var connection *Connection
   156  
   157  	BeforeEach(func() {
   158  		var err error
   159  
   160  		// Create the tokens:
   161  		accessToken := MakeTokenString("Bearer", 5*time.Minute)
   162  		refreshToken := MakeTokenString("Refresh", 10*time.Hour)
   163  
   164  		// Create the OpenID server:
   165  		oidServer = MakeTCPServer()
   166  		oidServer.AppendHandlers(
   167  			ghttp.CombineHandlers(
   168  				RespondWithAccessAndRefreshTokens(accessToken, refreshToken),
   169  			),
   170  		)
   171  
   172  		// Create the API server:
   173  		apiServer = MakeTCPServer()
   174  		apiServer.AppendHandlers(
   175  			RespondWithJSON(http.StatusOK, ""),
   176  		)
   177  
   178  		// Create the metrics server:
   179  		metricsServer = NewMetricsServer()
   180  
   181  		// Create the connection:
   182  		connection, err = NewConnectionBuilder().
   183  			Logger(logger).
   184  			URL(apiServer.URL()).
   185  			TokenURL(oidServer.URL()).
   186  			Tokens(refreshToken).
   187  			MetricsRegisterer(metricsServer.Registry()).
   188  			Build()
   189  		Expect(err).ToNot(HaveOccurred())
   190  	})
   191  
   192  	AfterEach(func() {
   193  		// Stop the servers:
   194  		oidServer.Close()
   195  		apiServer.Close()
   196  		metricsServer.Close()
   197  
   198  		// Close the connection:
   199  		err := connection.Close()
   200  		Expect(err).ToNot(HaveOccurred())
   201  	})
   202  
   203  	It("Doesn't generate metrics for raw request", func() {
   204  		// Send the request:
   205  		_, err := connection.Get().
   206  			Path("/api/clusters_mgmt/v1/clusters/123").
   207  			Send()
   208  		Expect(err).ToNot(HaveOccurred())
   209  
   210  		// Verify the metrics:
   211  		metrics := metricsServer.Metrics()
   212  		Expect(metrics).To(ConsistOf(""))
   213  	})
   214  
   215  	It("Doesn't generate metrics for type safe request", func() {
   216  		// Send the request:
   217  		_, err := connection.ClustersMgmt().V1().Clusters().Cluster("123").Get().
   218  			Send()
   219  		Expect(err).ToNot(HaveOccurred())
   220  
   221  		// Verify the metrics:
   222  		metrics := metricsServer.Metrics()
   223  		Expect(metrics).To(ConsistOf(""))
   224  	})
   225  })