Skip to content

Commit

Permalink
fix: metrics server endpoint to have timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
jrwbabylonlab committed Sep 3, 2024
1 parent 9266470 commit 02a435b
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions internal/observability/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import (
type Outcome string

const (
Success Outcome = "success"
Error Outcome = "error"
Success Outcome = "success"
Error Outcome = "error"
MetricRequestTimeout time.Duration = 5 * time.Second
MetricRequestIdleTimeout time.Duration = 10 * time.Second
)

func (O Outcome) String() string {
Expand Down Expand Up @@ -47,12 +49,21 @@ func initMetricsRouter(metricsPort int) {
metricsRouter.Get("/metrics", func(w http.ResponseWriter, r *http.Request) {
promhttp.Handler().ServeHTTP(w, r)
})
// Create a custom server with timeout settings
metricsAddr := fmt.Sprintf(":%d", metricsPort)
server := &http.Server{
Addr: metricsAddr,
Handler: metricsRouter,
ReadTimeout: MetricRequestTimeout,
WriteTimeout: MetricRequestTimeout,
IdleTimeout: MetricRequestIdleTimeout,
}

// Start the server in a separate goroutine
go func() {
metricsAddr := fmt.Sprintf(":%d", metricsPort)
err := http.ListenAndServe(metricsAddr, metricsRouter)
if err != nil {
log.Fatal().Err(err).Msgf("error starting metrics server on %s", metricsAddr)
log.Printf("Starting metrics server on %s", metricsAddr)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal().Err(err).Msgf("Error starting metrics server on %s", metricsAddr)
}
}()
}
Expand Down

0 comments on commit 02a435b

Please sign in to comment.