Skip to content

Commit

Permalink
Merge pull request #8 from radiofrance/separate-metrics-health-ports
Browse files Browse the repository at this point in the history
feat: separate metrics and healthcheck ports
  • Loading branch information
antony-ramos authored May 24, 2023
2 parents c0a187b + 57b337b commit 2262046
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ COPY --from=builder /build/image-registry-metrics-exporter /image-registry-metri

RUN apk add --no-cache ca-certificates=${CA_CERTIFICATES_VERSION}

EXPOSE 9252
EXPOSE 8080 9252
USER 65534

ENTRYPOINT ["/image-registry-metrics-exporter"]
34 changes: 29 additions & 5 deletions cmd/image-registry-metrics-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,42 @@ func main() {
}
tags.GenerateMetricsOn()

bindAddr := flag.String("bind-address", ":8080", "address:port to bind /metrics endpoint to")
bindAddrHealth := flag.String("bind-address-health", ":8080", "address:port to bind status endpoints to")
bindAddrMetrics := flag.String("bind-address-metrics", ":9252", "address:port to bind /metrics endpoint to")

flag.Parse()

// Activating routes for HTTP server
router := mux.NewRouter().StrictSlash(true)
router.Use(
mux.CORSMethodMiddleware(router), // Handle CORS requests
)

router.HandleFunc("/health", controllers.HealthCheck)
router.HandleFunc("/readiness", controllers.Ready)
router.Handle("/metrics", metrics.Handler())

// Activating routes for Metrics server
routerMetrics := mux.NewRouter().StrictSlash(true)
routerMetrics.Use(
mux.CORSMethodMiddleware(routerMetrics), // Handle CORS requests
)
routerMetrics.Handle("/metrics", metrics.Handler())

timeoutDuration := 30 * time.Second
metricsSrv := &http.Server{
Addr: *bindAddr,
Addr: *bindAddrMetrics,
Handler: http.TimeoutHandler(routerMetrics, timeoutDuration, "Server Timeout"),
ReadTimeout: timeoutDuration,
WriteTimeout: timeoutDuration,
}
healthSrv := &http.Server{
Addr: *bindAddrHealth,
Handler: http.TimeoutHandler(router, timeoutDuration, "Server Timeout"),
ReadTimeout: timeoutDuration,
WriteTimeout: timeoutDuration,
}

var waitGroup sync.WaitGroup
waitGroup.Add(1)
waitGroup.Add(2)

go func() {
defer waitGroup.Done()
Expand All @@ -78,6 +91,16 @@ func main() {
}
}
}()
go func() {
defer waitGroup.Done()
if err := healthSrv.ListenAndServe(); err != nil {
if errors.Is(err, http.ErrServerClosed) {
log.Info("Http server closed")
} else {
log.Fatalf("Failed to start http server %v", err)
}
}
}()
controllers.UpdateHealth(true)
controllers.UpdateReady(true)

Expand Down Expand Up @@ -109,6 +132,7 @@ func main() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel()
_ = metricsSrv.Shutdown(ctx)
_ = healthSrv.Shutdown(ctx)
waitGroup.Wait()
log.Info("Shutting down")
}

0 comments on commit 2262046

Please sign in to comment.