2022-04-27 01:00:20 -05:00
|
|
|
//go:build metrics
|
|
|
|
// +build metrics
|
|
|
|
|
|
|
|
package extensions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2024-01-31 23:34:07 -05:00
|
|
|
"zotregistry.dev/zot/pkg/api/config"
|
|
|
|
"zotregistry.dev/zot/pkg/extensions/monitoring"
|
|
|
|
"zotregistry.dev/zot/pkg/log"
|
2022-04-27 01:00:20 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func EnableMetricsExtension(config *config.Config, log log.Logger, rootDir string) {
|
2023-09-15 06:49:34 -05:00
|
|
|
if config.IsMetricsEnabled() &&
|
2022-04-27 01:00:20 -05:00
|
|
|
config.Extensions.Metrics.Prometheus != nil {
|
|
|
|
if config.Extensions.Metrics.Prometheus.Path == "" {
|
|
|
|
config.Extensions.Metrics.Prometheus.Path = "/metrics"
|
|
|
|
|
2023-12-08 03:05:02 -05:00
|
|
|
log.Warn().Msg("prometheus instrumentation path not set, changing to '/metrics'.")
|
2022-04-27 01:00:20 -05:00
|
|
|
}
|
|
|
|
} else {
|
2023-12-08 03:05:02 -05:00
|
|
|
log.Info().Msg("metrics config not provided, skipping metrics config update")
|
2022-04-27 01:00:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 13:58:34 -05:00
|
|
|
func SetupMetricsRoutes(config *config.Config, router *mux.Router,
|
2023-10-20 02:33:26 -05:00
|
|
|
authnFunc, authzFunc mux.MiddlewareFunc, log log.Logger, metrics monitoring.MetricServer,
|
2022-04-27 01:00:20 -05:00
|
|
|
) {
|
|
|
|
log.Info().Msg("setting up metrics routes")
|
|
|
|
|
2023-09-15 06:49:34 -05:00
|
|
|
if config.IsMetricsEnabled() {
|
2023-02-10 17:52:54 -05:00
|
|
|
extRouter := router.PathPrefix(config.Extensions.Metrics.Prometheus.Path).Subrouter()
|
2023-10-20 02:33:26 -05:00
|
|
|
extRouter.Use(authnFunc)
|
|
|
|
extRouter.Use(authzFunc)
|
2023-02-10 17:52:54 -05:00
|
|
|
extRouter.Methods("GET").Handler(promhttp.Handler())
|
2022-04-27 01:00:20 -05:00
|
|
|
}
|
|
|
|
}
|