instrument metrics

This commit is contained in:
Vivek Kumar 2024-02-29 10:26:57 +05:30
parent 57b07c911b
commit 2ba4422ea4
No known key found for this signature in database
GPG key ID: 81605FDFBA4EF440

View file

@ -47,15 +47,15 @@ var (
) )
// define a counter for API errors for various ErrorTypes // define a counter for API errors for various ErrorTypes
apiErrorCount = metrics.NewCounterVec( apiErrorCount = metrics.NewCounterVec(
&metrics.CounterOpts{ &metrics.CounterOpts{
Namespace: "prometheus_adapter", Namespace: "prometheus_adapter",
Subsystem: "prometheus_client", Subsystem: "prometheus_client",
Name: "api_errors_total", Name: "api_errors_total",
Help: "Total number of API errors", Help: "Total number of API errors",
}, },
[]string{"error_code", "path", "server"}, []string{"error_code", "path", "server"},
) )
) )
func MetricsHandler() (http.HandlerFunc, error) { func MetricsHandler() (http.HandlerFunc, error) {
@ -67,9 +67,9 @@ func MetricsHandler() (http.HandlerFunc, error) {
} }
errRegisterAPIErrorCount := registry.Register(apiErrorCount) errRegisterAPIErrorCount := registry.Register(apiErrorCount)
if errRegisterAPIErrorCount != nil { if errRegisterAPIErrorCount != nil {
return nil, errRegisterAPIErrorCount return nil, errRegisterAPIErrorCount
} }
apimetrics.Register() apimetrics.Register()
return func(w http.ResponseWriter, req *http.Request) { return func(w http.ResponseWriter, req *http.Request) {
@ -93,12 +93,12 @@ func (c *instrumentedGenericClient) Do(ctx context.Context, verb, endpoint strin
// skip calls where we don't make the actual request // skip calls where we don't make the actual request
if err != nil { if err != nil {
if apiErr, wasAPIErr := err.(*client.Error); wasAPIErr { if apiErr, wasAPIErr := err.(*client.Error); wasAPIErr {
// Measure API errors // measure API errors
apiErrorCount.With(prometheus.Labels{"error_code": string(apiErr.Type), "path": endpoint, "server": c.serverName}).Inc() apiErrorCount.With(prometheus.Labels{"error_code": string(apiErr.Type), "path": endpoint, "server": c.serverName}).Inc()
} else { } else {
// Increment a generic error code counter // increment a generic error code counter
apiErrorCount.With(prometheus.Labels{"error_code": "generic", "path": endpoint, "server": c.serverName}).Inc() apiErrorCount.With(prometheus.Labels{"error_code": "generic", "path": endpoint, "server": c.serverName}).Inc()
} }
return return
} }
queryLatency.With(prometheus.Labels{"path": endpoint, "server": c.serverName}).Observe(endTime.Sub(startTime).Seconds()) queryLatency.With(prometheus.Labels{"path": endpoint, "server": c.serverName}).Observe(endTime.Sub(startTime).Seconds())