mirror of
https://github.com/kubernetes-sigs/prometheus-adapter.git
synced 2026-04-06 09:47:54 +00:00
Adding extra metric till now, not impacting path
This commit is contained in:
parent
9156bf3fbc
commit
e70cb42780
3 changed files with 66 additions and 16 deletions
|
|
@ -18,11 +18,16 @@ package metrics
|
|||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
|
||||
apimetrics "k8s.io/apiserver/pkg/endpoints/metrics"
|
||||
"k8s.io/component-base/metrics"
|
||||
"k8s.io/component-base/metrics/legacyregistry"
|
||||
|
||||
"sigs.k8s.io/prometheus-adapter/pkg/client"
|
||||
)
|
||||
|
||||
|
|
@ -30,18 +35,47 @@ var (
|
|||
// queryLatency is the total latency of any query going through the
|
||||
// various endpoints (query, range-query, series). It includes some deserialization
|
||||
// overhead and HTTP overhead.
|
||||
queryLatency = prometheus.NewHistogramVec(
|
||||
prometheus.HistogramOpts{
|
||||
Name: "cmgateway_prometheus_query_latency_seconds",
|
||||
Help: "Prometheus client query latency in seconds. Broken down by target prometheus endpoint and target server",
|
||||
Buckets: prometheus.ExponentialBuckets(0.0001, 2, 10),
|
||||
queryLatency = metrics.NewHistogramVec(
|
||||
&metrics.HistogramOpts{
|
||||
Namespace: "prometheus_adapter",
|
||||
Subsystem: "prometheus_client",
|
||||
Name: "request_duration_seconds",
|
||||
Help: "Prometheus client query latency in seconds. Broken down by target prometheus endpoint and target server",
|
||||
Buckets: prometheus.DefBuckets,
|
||||
},
|
||||
[]string{"endpoint", "server"},
|
||||
[]string{"path", "server"},
|
||||
)
|
||||
|
||||
// define a counter for API errors for various ErrorTypes
|
||||
apiErrorCount = metrics.NewCounterVec(
|
||||
&metrics.CounterOpts{
|
||||
Namespace: "prometheus_adapter",
|
||||
Subsystem: "prometheus_client",
|
||||
Name: "api_errors_total",
|
||||
Help: "Total number of API errors",
|
||||
},
|
||||
[]string{"error_code", "path", "server"},
|
||||
)
|
||||
)
|
||||
|
||||
func init() {
|
||||
prometheus.MustRegister(queryLatency)
|
||||
func MetricsHandler() (http.HandlerFunc, error) {
|
||||
registry := metrics.NewKubeRegistry()
|
||||
|
||||
errRegisterQueryLatency := registry.Register(queryLatency)
|
||||
if errRegisterQueryLatency != nil {
|
||||
return nil, errRegisterQueryLatency
|
||||
}
|
||||
|
||||
errRegisterAPIErrorCount := registry.Register(apiErrorCount)
|
||||
if errRegisterAPIErrorCount != nil {
|
||||
return nil, errRegisterAPIErrorCount
|
||||
}
|
||||
|
||||
apimetrics.Register()
|
||||
return func(w http.ResponseWriter, req *http.Request) {
|
||||
legacyregistry.Handler().ServeHTTP(w, req)
|
||||
metrics.HandlerFor(registry, metrics.HandlerOpts{}).ServeHTTP(w, req)
|
||||
}, nil
|
||||
}
|
||||
|
||||
// instrumentedClient is a client.GenericAPIClient which instruments calls to Do,
|
||||
|
|
@ -58,12 +92,16 @@ func (c *instrumentedGenericClient) Do(ctx context.Context, verb, endpoint strin
|
|||
endTime := time.Now()
|
||||
// skip calls where we don't make the actual request
|
||||
if err != nil {
|
||||
if _, wasAPIErr := err.(*client.Error); !wasAPIErr {
|
||||
// TODO: measure API errors by code?
|
||||
return
|
||||
}
|
||||
if apiErr, wasAPIErr := err.(*client.Error); wasAPIErr {
|
||||
// Measure API errors
|
||||
apiErrorCount.With(prometheus.Labels{"error_code": string(apiErr.Type), "path": endpoint, "server": c.serverName}).Inc()
|
||||
} else {
|
||||
// Increment a generic error code counter
|
||||
apiErrorCount.With(prometheus.Labels{"error_code": "generic", "path": endpoint, "server": c.serverName}).Inc()
|
||||
}
|
||||
return
|
||||
}
|
||||
queryLatency.With(prometheus.Labels{"endpoint": endpoint, "server": c.serverName}).Observe(endTime.Sub(startTime).Seconds())
|
||||
queryLatency.With(prometheus.Labels{"path": endpoint, "server": c.serverName}).Observe(endTime.Sub(startTime).Seconds())
|
||||
}()
|
||||
|
||||
var resp client.APIResponse
|
||||
|
|
@ -76,4 +114,4 @@ func InstrumentGenericAPIClient(client client.GenericAPIClient, serverName strin
|
|||
serverName: serverName,
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue