72 lines
2.6 KiB
Go
72 lines
2.6 KiB
Go
package transport
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/geo-platform/tenant-api/internal/bootstrap"
|
|
"github.com/geo-platform/tenant-api/internal/shared/response"
|
|
tenantapp "github.com/geo-platform/tenant-api/internal/tenant/app"
|
|
)
|
|
|
|
func registerInternalMetricsRoutes(app *bootstrap.App) {
|
|
token := strings.TrimSpace(app.Config().Scheduler.InternalMetricsToken)
|
|
if token == "" {
|
|
app.Logger.Warn("tenant-api internal metrics routes not registered: scheduler.internal_metrics_token is required")
|
|
return
|
|
}
|
|
|
|
metrics := app.Engine.Group("/api/internal/metrics")
|
|
metrics.Use(internalMetricsAuthMiddleware(app))
|
|
metrics.GET("/monitoring/heartbeat-primary-lease", func(c *gin.Context) {
|
|
response.Success(c, tenantapp.MonitoringHeartbeatPrimaryLeaseMetricsSnapshotValue())
|
|
})
|
|
primaryLeasePrometheusHandler := tenantapp.MonitoringHeartbeatPrimaryLeaseMetricsPrometheusHandler()
|
|
metrics.GET("/monitoring/heartbeat-primary-lease/prometheus", func(c *gin.Context) {
|
|
primaryLeasePrometheusHandler.ServeHTTP(c.Writer, c.Request)
|
|
})
|
|
metrics.GET("/monitoring/heartbeat-snapshots", func(c *gin.Context) {
|
|
response.Success(c, tenantapp.MonitoringHeartbeatSnapshotMetricsSnapshotValue())
|
|
})
|
|
snapshotPrometheusHandler := tenantapp.MonitoringHeartbeatSnapshotMetricsPrometheusHandler()
|
|
metrics.GET("/monitoring/heartbeat-snapshots/prometheus", func(c *gin.Context) {
|
|
snapshotPrometheusHandler.ServeHTTP(c.Writer, c.Request)
|
|
})
|
|
metrics.GET("/generation/tasks", func(c *gin.Context) {
|
|
response.Success(c, tenantapp.GenerationTaskMetricsSnapshotValue())
|
|
})
|
|
generationTasksPrometheusHandler := tenantapp.GenerationTaskMetricsPrometheusHandler()
|
|
metrics.GET("/generation/tasks/prometheus", func(c *gin.Context) {
|
|
generationTasksPrometheusHandler.ServeHTTP(c.Writer, c.Request)
|
|
})
|
|
}
|
|
|
|
func internalMetricsAuthMiddleware(app *bootstrap.App) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
expected := ""
|
|
if cfg := app.Config(); cfg != nil {
|
|
expected = strings.TrimSpace(cfg.Scheduler.InternalMetricsToken)
|
|
}
|
|
if expected == "" || !internalMetricsTokenMatches(c, expected) {
|
|
response.Error(c, response.ErrUnauthorized(40181, "internal_metrics_unauthorized", "valid internal metrics token is required"))
|
|
c.Abort()
|
|
return
|
|
}
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
func internalMetricsTokenMatches(c *gin.Context, expected string) bool {
|
|
if c == nil {
|
|
return false
|
|
}
|
|
header := strings.TrimSpace(c.GetHeader("Authorization"))
|
|
if strings.HasPrefix(strings.ToLower(header), "bearer ") {
|
|
if strings.TrimSpace(header[len("bearer "):]) == expected {
|
|
return true
|
|
}
|
|
}
|
|
return strings.TrimSpace(c.GetHeader("X-Internal-Metrics-Token")) == expected
|
|
}
|