Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: metrics server endpoint to have timeout #12

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading