diff --git a/gateway/handler.go b/gateway/handler.go index 8ce4ff7eb..743fb9942 100644 --- a/gateway/handler.go +++ b/gateway/handler.go @@ -67,6 +67,7 @@ type handler struct { api IPFSBackend // response type metrics + requestTypeMetric *prometheus.CounterVec getMetric *prometheus.HistogramVec unixfsFileGetMetric *prometheus.HistogramVec unixfsDirIndexGetMetric *prometheus.HistogramVec @@ -234,6 +235,7 @@ func (i *handler) getOrHeadHandler(w http.ResponseWriter, r *http.Request) { return } trace.SpanFromContext(r.Context()).SetAttributes(attribute.String("ResponseFormat", responseFormat)) + i.requestTypeMetric.WithLabelValues(contentPath.Namespace(), responseFormat).Inc() i.addUserHeaders(w) // ok, _now_ write user's headers. w.Header().Set("X-Ipfs-Path", contentPath.String()) diff --git a/gateway/metrics.go b/gateway/metrics.go index f0de3c7d3..5fc9a796a 100644 --- a/gateway/metrics.go +++ b/gateway/metrics.go @@ -190,6 +190,10 @@ func newHandlerWithMetrics(c Config, api IPFSBackend) *handler { // Response-type specific metrics // ---------------------------- + requestTypeMetric: newRequestTypeMetric( + "gw_request_types", + "The number of requests per implicit or explicit request type.", + ), // Generic: time it takes to execute a successful gateway request (all request types) getMetric: newHistogramMetric( "gw_get_duration_seconds", @@ -239,6 +243,28 @@ func newHandlerWithMetrics(c Config, api IPFSBackend) *handler { return i } +func newRequestTypeMetric(name string, help string) *prometheus.CounterVec { + // We can add buckets as a parameter in the future, but for now using static defaults + // suggested in https://github.com/ipfs/kubo/issues/8441 + metric := prometheus.NewCounterVec( + prometheus.CounterOpts{ + Namespace: "ipfs", + Subsystem: "http", + Name: name, + Help: help, + }, + []string{"gateway", "type"}, + ) + if err := prometheus.Register(metric); err != nil { + if are, ok := err.(prometheus.AlreadyRegisteredError); ok { + metric = are.ExistingCollector.(*prometheus.CounterVec) + } else { + log.Errorf("failed to register ipfs_http_%s: %v", name, err) + } + } + return metric +} + func newHistogramMetric(name string, help string) *prometheus.HistogramVec { // We can add buckets as a parameter in the future, but for now using static defaults // suggested in https://github.com/ipfs/kubo/issues/8441