Skip to content

Commit

Permalink
monitoring: give a timeout to our prometheus request handler (scionpr…
Browse files Browse the repository at this point in the history
…oto#4575)

Set a 1 m timeout to all prometheus incoming requests.
Fixes scionproto#1971
  • Loading branch information
jiceatscion authored Jul 16, 2024
1 parent eab1cab commit 799204f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions private/env/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ go_library(
"//pkg/scrypto:go_default_library",
"//private/config:go_default_library",
"@com_github_opentracing_opentracing_go//:go_default_library",
"@com_github_prometheus_client_golang//prometheus:go_default_library",
"@com_github_prometheus_client_golang//prometheus/promhttp:go_default_library",
"@com_github_uber_jaeger_client_go//:go_default_library",
"@com_github_uber_jaeger_client_go//config:go_default_library",
Expand Down
14 changes: 13 additions & 1 deletion private/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"time"

opentracing "github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
jaeger "github.com/uber/jaeger-client-go"
jaegercfg "github.com/uber/jaeger-client-go/config"
Expand Down Expand Up @@ -61,6 +62,10 @@ const (
// ShutdownGraceInterval is the time applications wait after issuing a
// clean shutdown signal, before forcerfully tearing down the application.
ShutdownGraceInterval = 5 * time.Second

// HandlerTimeout is the time after which the http handler gives up on a request and
// returns an error instead.
HandlerTimeout = time.Minute
)

var sighupC chan os.Signal
Expand Down Expand Up @@ -185,7 +190,14 @@ func (cfg *Metrics) ServePrometheus(ctx context.Context) error {
if cfg.Prometheus == "" {
return nil
}
http.Handle("/metrics", promhttp.Handler())
handler := promhttp.InstrumentMetricHandler(
prometheus.DefaultRegisterer,
promhttp.HandlerFor(
prometheus.DefaultGatherer,
promhttp.HandlerOpts{Timeout: HandlerTimeout},
),
)
http.Handle("/metrics", handler)
log.Info("Exporting prometheus metrics", "addr", cfg.Prometheus)

server := &http.Server{Addr: cfg.Prometheus}
Expand Down

0 comments on commit 799204f

Please sign in to comment.