Skip to content

Commit

Permalink
Add Prometheus metrics to HTTP servers
Browse files Browse the repository at this point in the history
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
EdSchouten committed Sep 5, 2023
1 parent a9ac0c7 commit 842770c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/global/apply_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ func (ls *LifecycleState) MarkReadyAndWait(group program.Group) {
}

group.Go(func(ctx context.Context, siblingsGroup, dependenciesGroup program.Group) error {
if err := bb_http.NewServersFromConfigurationAndServe(ls.config.HttpServers, router, group); err != nil {
if err := bb_http.NewServersFromConfigurationAndServe(
ls.config.HttpServers,
bb_http.NewMetricsHandler(router, "Diagnostics"),
group,
); err != nil {
return util.StatusWrap(err, "Failed to launch diagnostics HTTP server")
}
return nil
Expand Down
1 change: 1 addition & 0 deletions pkg/http/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ go_library(
"deny_authenticator.go",
"header_adding_round_tripper.go",
"jwt_authenticator.go",
"metrics_handler.go",
"metrics_round_tripper.go",
"oidc_authenticator.go",
"server.go",
Expand Down
36 changes: 36 additions & 0 deletions pkg/http/metrics_handler.go
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)
}

0 comments on commit 842770c

Please sign in to comment.