New feature: prometheus metrics endpoint (#46)

This commit is contained in:
2025-11-03 18:36:13 +01:00
committed by GitHub
parent ec1de1fa81
commit e8d3a20781
13 changed files with 564 additions and 82 deletions

View File

@@ -10,6 +10,7 @@ import (
"time"
"github.com/dcarrillo/whatismyip/internal/httputils"
"github.com/dcarrillo/whatismyip/internal/metrics"
"github.com/dcarrillo/whatismyip/internal/setting"
"github.com/dcarrillo/whatismyip/resolver"
"github.com/dcarrillo/whatismyip/server"
@@ -54,6 +55,11 @@ func main() {
router.Setup(engine, geoSvc)
servers = slices.Concat(servers, setupHTTPServers(context.Background(), engine.Handler()))
if setting.App.PrometheusAddress != "" {
prometheusServer := server.NewPrometheusServer(context.Background())
servers = append(servers, prometheusServer)
}
whatismyip := server.Setup(servers, geoSvc)
whatismyip.Run()
}
@@ -65,6 +71,10 @@ func setupEngine() *gin.Engine {
}
engine := gin.New()
engine.Use(gin.LoggerWithFormatter(httputils.GetLogFormatter), gin.Recovery())
if setting.App.PrometheusAddress != "" {
metrics.Enable()
engine.Use(metrics.GinMiddleware())
}
if setting.App.EnableSecureHeaders {
engine.Use(secure.New(secure.Config{
BrowserXssFilter: true,