-
Notifications
You must be signed in to change notification settings - Fork 23
/
metrics.go
93 lines (78 loc) · 3.31 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// +kubebuilder:validation:Optional
package metrics
import (
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/fx"
"github.com/fluxninja/aperture/v2/pkg/config"
"github.com/fluxninja/aperture/v2/pkg/log"
)
// Module is a fx module that provides Prometheus registry and invokes registering metrics handler.
func Module() fx.Option {
return fx.Options(
fx.Provide(ProvidePrometheusRegistry),
fx.Invoke(RegisterMetricsHandler),
)
}
const (
// MetricsConfigKey is the key for the Prometheus configuration.
metricsConfigKey = "metrics"
// MetricsEndpoint is default endpoint which is used to register metrics handler.
metricsEndpoint = "/metrics"
)
// swagger:operation POST /metrics common-configuration Metrics
// ---
// x-fn-config-env: true
// parameters:
// - in: body
// schema:
// "$ref": "#/definitions/MetricsConfig"
// MetricsConfig holds configuration for service metrics.
// swagger:model
// +kubebuilder:object:generate=true
type MetricsConfig struct {
// Pedantic controls whether a pedantic registry is used. See <https://godoc.org/github.com/prometheus/client_golang/prometheus#NewPedanticRegistry>
Pedantic bool `json:"pedantic" default:"false"`
// EnableGoCollector controls whether the go collector is registered on startup. See <https://godoc.org/github.com/prometheus/client_golang/prometheus#NewGoCollector>
EnableGoCollector bool `json:"enable_go_metrics" default:"false"`
// EnableProcessCollector controls whether the process collector is registered on startup. See <https://godoc.org/github.com/prometheus/client_golang/prometheus#NewProcessCollector>
EnableProcessCollector bool `json:"enable_process_collector" default:"false"`
}
// ProvidePrometheusRegistry creates a new Prometheus Registry and provides it via Fx.
// Metrics from the Registry are served on /metrics via the default http server of Fx application.
func ProvidePrometheusRegistry(unmarshaller config.Unmarshaller) (*prometheus.Registry, error) {
var pr *prometheus.Registry
var config MetricsConfig
if err := unmarshaller.UnmarshalKey(metricsConfigKey, &config); err != nil {
log.Error().Err(err).Msg("Unable to deserialize prometheus configuration!")
return nil, err
}
if config.Pedantic {
pr = prometheus.NewRegistry()
} else {
pr = prometheus.NewPedanticRegistry()
}
if config.EnableGoCollector {
if err := pr.Register(collectors.NewGoCollector()); err == nil {
log.Warn().Err(err).Msg("Unable to register prometheus go collector!")
} else {
log.Info().Msg("Registering Go Metrics collector")
}
}
if config.EnableProcessCollector {
if err := pr.Register(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{})); err != nil {
log.Warn().Err(err).Msg("Unable to register prometheus process collector!")
} else {
log.Info().Msg("Registering Process Metrics collector")
}
}
return pr, nil
}
// RegisterMetricsHandler registers the metrics handler on the promhttp server.
func RegisterMetricsHandler(router *mux.Router, pr *prometheus.Registry) {
log.Info().Msg("Registering Prometheus metrics endpoint")
logger := log.WithComponent("prom-http")
router.Handle(metricsEndpoint, promhttp.HandlerFor(pr, promhttp.HandlerOpts{ErrorLog: logger}))
}