github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/metrics/collector.go (about)

     1  package metrics
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/prometheus/client_golang/prometheus"
     7  	"github.com/prometheus/client_golang/prometheus/promhttp"
     8  )
     9  
    10  // Config configures the behaviour of the metrics collector.
    11  type Config struct {
    12  	EnableGraphqlOperationInstrumentation bool `envconfig:"default=false,APP_METRICS_ENABLE_GRAPHQL_OPERATION_INSTRUMENTATION"`
    13  }
    14  
    15  // Collector missing godoc
    16  type Collector struct {
    17  	config Config
    18  
    19  	graphQLRequestTotal    *prometheus.CounterVec
    20  	graphQLRequestDuration *prometheus.HistogramVec
    21  	hydraRequestTotal      *prometheus.CounterVec
    22  	hydraRequestDuration   *prometheus.HistogramVec
    23  	graphQLOperationCount  *prometheus.CounterVec
    24  }
    25  
    26  // NewCollector missing godoc
    27  func NewCollector(config Config) *Collector {
    28  	return &Collector{
    29  		config: config,
    30  
    31  		graphQLRequestTotal: prometheus.NewCounterVec(prometheus.CounterOpts{
    32  			Namespace: Namespace,
    33  			Subsystem: DirectorSubsystem,
    34  			Name:      "graphql_request_total",
    35  			Help:      "Total handled GraphQL Requests",
    36  		}, []string{"code", "method"}),
    37  		graphQLRequestDuration: prometheus.NewHistogramVec(prometheus.HistogramOpts{
    38  			Namespace: Namespace,
    39  			Subsystem: DirectorSubsystem,
    40  			Name:      "graphql_request_duration_seconds",
    41  			Help:      "Duration of handling GraphQL requests",
    42  		}, []string{"code", "method"}),
    43  		hydraRequestTotal: prometheus.NewCounterVec(prometheus.CounterOpts{
    44  			Namespace: Namespace,
    45  			Subsystem: DirectorSubsystem,
    46  			Name:      "hydra_request_total",
    47  			Help:      "Total HTTP Requests to Hydra",
    48  		}, []string{"code", "method"}),
    49  		hydraRequestDuration: prometheus.NewHistogramVec(prometheus.HistogramOpts{
    50  			Namespace: Namespace,
    51  			Subsystem: DirectorSubsystem,
    52  			Name:      "hydra_request_duration_seconds",
    53  			Help:      "Duration of HTTP Requests to Hydra",
    54  		}, []string{"code", "method"}),
    55  		graphQLOperationCount: prometheus.NewCounterVec(prometheus.CounterOpts{
    56  			Namespace: Namespace,
    57  			Subsystem: DirectorSubsystem,
    58  			Name:      "graphql_operations_per_endpoint",
    59  			Help:      "Graphql Operations Per Operation",
    60  		}, []string{"operation_name", "operation_type"}),
    61  	}
    62  }
    63  
    64  // Describe missing godoc
    65  func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
    66  	c.graphQLRequestTotal.Describe(ch)
    67  	c.graphQLRequestDuration.Describe(ch)
    68  	c.hydraRequestTotal.Describe(ch)
    69  	c.hydraRequestDuration.Describe(ch)
    70  	c.graphQLOperationCount.Describe(ch)
    71  }
    72  
    73  // Collect missing godoc
    74  func (c *Collector) Collect(ch chan<- prometheus.Metric) {
    75  	c.graphQLRequestTotal.Collect(ch)
    76  	c.graphQLRequestDuration.Collect(ch)
    77  	c.hydraRequestTotal.Collect(ch)
    78  	c.hydraRequestDuration.Collect(ch)
    79  	c.graphQLOperationCount.Collect(ch)
    80  }
    81  
    82  // GraphQLHandlerWithInstrumentation missing godoc
    83  func (c *Collector) GraphQLHandlerWithInstrumentation(handler http.Handler) http.HandlerFunc {
    84  	return promhttp.InstrumentHandlerCounter(c.graphQLRequestTotal,
    85  		promhttp.InstrumentHandlerDuration(c.graphQLRequestDuration, handler),
    86  	)
    87  }
    88  
    89  // InstrumentOAuth20HTTPClient missing godoc
    90  func (c *Collector) InstrumentOAuth20HTTPClient(client *http.Client) {
    91  	client.Transport = promhttp.InstrumentRoundTripperCounter(c.hydraRequestTotal,
    92  		promhttp.InstrumentRoundTripperDuration(c.hydraRequestDuration, client.Transport),
    93  	)
    94  }
    95  
    96  // InstrumentGraphqlRequest instruments a graphql request given operationName and operationType
    97  func (c *Collector) InstrumentGraphqlRequest(operationType, operationName string) {
    98  	if !c.config.EnableGraphqlOperationInstrumentation {
    99  		return
   100  	}
   101  
   102  	c.graphQLOperationCount.With(prometheus.Labels{
   103  		"operation_name": operationName,
   104  		"operation_type": operationType,
   105  	}).Inc()
   106  }