Skip to content

Commit

Permalink
feat(gw): add ipfs_http_gw_request_types metric
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed May 22, 2023
1 parent 4fb455d commit 498d5a6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions gateway/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type handler struct {
api IPFSBackend

// response type metrics
requestTypeMetric *prometheus.CounterVec
getMetric *prometheus.HistogramVec
unixfsFileGetMetric *prometheus.HistogramVec
unixfsDirIndexGetMetric *prometheus.HistogramVec
Expand Down Expand Up @@ -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())
Expand Down
26 changes: 26 additions & 0 deletions gateway/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 498d5a6

Please sign in to comment.