-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Prometheus metrics to HTTP servers
We already have Prometheus metrics for HTTP clients (e.g., AWS SDK, Pushgateway), but don't have it for servers. This has so far not been a big issue because we tend to use gRPC more frequently, but now that we have a more advanced authentication framework for HTTP, it makes sense to give some more insight there as well.
- Loading branch information
1 parent
a9ac0c7
commit 842770c
Showing
3 changed files
with
42 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package http | ||
|
||
import ( | ||
"net/http" | ||
"sync" | ||
|
||
"github.com/buildbarn/bb-storage/pkg/util" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
) | ||
|
||
var ( | ||
handlerPrometheusMetrics sync.Once | ||
|
||
handlerRequestsDurationSeconds = prometheus.NewHistogramVec( | ||
prometheus.HistogramOpts{ | ||
Namespace: "buildbarn", | ||
Subsystem: "http", | ||
Name: "handler_requests_duration_seconds", | ||
Help: "Amount of time spent per HTTP request, in seconds.", | ||
Buckets: util.DecimalExponentialBuckets(-3, 6, 2), | ||
}, | ||
[]string{"name", "code", "method"}) | ||
) | ||
|
||
// NewMetricsHandler creates an adapter for http.Handler that adds basic | ||
// instrumentation in the form of Prometheus metrics. | ||
func NewMetricsHandler(base http.Handler, name string) http.Handler { | ||
handlerPrometheusMetrics.Do(func() { | ||
prometheus.MustRegister(handlerRequestsDurationSeconds) | ||
}) | ||
|
||
return promhttp.InstrumentHandlerDuration( | ||
handlerRequestsDurationSeconds.MustCurryWith(prometheus.Labels{"name": name}), | ||
base) | ||
} |