diff --git a/common/reporter/metrics.go b/common/reporter/metrics.go index c3b2a62c..03cfeaf0 100644 --- a/common/reporter/metrics.go +++ b/common/reporter/metrics.go @@ -111,22 +111,7 @@ func (r *Reporter) MetricsHTTPHandler() http.Handler { return r.metrics.HTTPHandler() } -// MetricCollector register a custom collector. -func (r *Reporter) MetricCollector(c prometheus.Collector) { - r.metrics.Collector(c) -} - -// MetricCollectorForCurrentModule register a custom collector prefixed by the current module name. -func (r *Reporter) MetricCollectorForCurrentModule(c prometheus.Collector) { - r.metrics.CollectorForCurrentModule(1, c) -} - -// MetricDesc defines a new metric description. -func (r *Reporter) MetricDesc(name, help string, variableLabels []string) *MetricDesc { - return r.metrics.Desc(1, name, help, variableLabels) -} - -// MetricDesc2 defines a new metric description. It skips one callstack. -func (r *Reporter) MetricDesc2(name, help string, variableLabels []string) *MetricDesc { - return r.metrics.Desc(2, name, help, variableLabels) +// RegisterMetricCollector register a custom collector prefixed by the current module name. +func (r *Reporter) RegisterMetricCollector(c prometheus.Collector) { + r.metrics.RegisterCollector(1, c) } diff --git a/common/reporter/metrics/root.go b/common/reporter/metrics/root.go index 92036271..c143a52b 100644 --- a/common/reporter/metrics/root.go +++ b/common/reporter/metrics/root.go @@ -100,25 +100,9 @@ func (m *Metrics) Factory(skipCallstack int) *Factory { return &factory } -// Desc allocates and initializes and new metric description. Like for -// factory, names are prefixed with the module name. Unlike factory, -// there is no cache. -func (m *Metrics) Desc(skipCallstack int, name, help string, variableLabels []string) *prometheus.Desc { - callStack := stack.Callers() - call := callStack[1+skipCallstack] // Trial and error, there is a test to check it works - prefix := getPrefix(call.FunctionName()) - name = fmt.Sprintf("%s%s", prefix, name) - return prometheus.NewDesc(name, help, variableLabels, nil) -} - -// Collector register a custom collector. -func (m *Metrics) Collector(c prometheus.Collector) { - m.registry.MustRegister(c) -} - -// CollectorForCurrentModule register a custom collector and prefix +// RegisterCollector register a custom collector and prefix // everything with the module name. -func (m *Metrics) CollectorForCurrentModule(skipCallStack int, c prometheus.Collector) { +func (m *Metrics) RegisterCollector(skipCallStack int, c prometheus.Collector) { callStack := stack.Callers() call := callStack[1+skipCallStack] // Should be the same as above ! prefix := getPrefix(call.FunctionName()) diff --git a/common/reporter/metrics/root_test.go b/common/reporter/metrics/root_test.go index 57ebef7b..5eef9abc 100644 --- a/common/reporter/metrics/root_test.go +++ b/common/reporter/metrics/root_test.go @@ -7,6 +7,7 @@ import ( "fmt" "net/http/httptest" "runtime" + "slices" "strings" "testing" @@ -51,14 +52,7 @@ func TestNew(t *testing.T) { expecteds = append(expecteds, "process_open_fds") } for _, expected := range expecteds { - found := false - for _, line := range got { - if line == fmt.Sprintf("# TYPE %s gauge", expected) { - found = true - break - } - } - if !found { + if !slices.Contains(got, fmt.Sprintf("# TYPE %s gauge", expected)) { t.Errorf("GET /api/v0/metrics missing: %s", expected) } } diff --git a/common/reporter/metrics_test.go b/common/reporter/metrics_test.go index 2db0066f..3473309f 100644 --- a/common/reporter/metrics_test.go +++ b/common/reporter/metrics_test.go @@ -180,9 +180,9 @@ func TestMetricCollector(t *testing.T) { r := reporter.NewMock(t) m := customMetrics{} - m.metric1 = r.MetricDesc("metric1", "Custom metric 1", nil) - m.metric2 = r.MetricDesc("metric2", "Custom metric 2", nil) - r.MetricCollector(m) + m.metric1 = prometheus.NewDesc("metric1", "Custom metric 1", nil, nil) + m.metric2 = prometheus.NewDesc("metric2", "Custom metric 2", nil, nil) + r.RegisterMetricCollector(m) got := r.GetMetrics("akvorado_common_reporter_test_") expected := map[string]string{ diff --git a/console/data/docs/99-changelog.md b/console/data/docs/99-changelog.md index 3cb12fed..8294aeec 100644 --- a/console/data/docs/99-changelog.md +++ b/console/data/docs/99-changelog.md @@ -25,6 +25,7 @@ the ownership of the Prometheus volume: #1900](https://github.com/akvorado/akvorado/pull/1900) for the consequences if you upgrade from a previous beta) - 💥 *docker*: switch from Prometheus to Grafana Alloy for scraping metrics +- 🩹 *outlet*: move gRPC metrics for BioRIS provider in the routing namespace - 🌱 *docker*: enforce bridge name - 🌱 *docker*: add cAdvisor to the monitoring stack - 🌱 *docker*: update Prometheus to 3.5.0 diff --git a/inlet/kafka/root.go b/inlet/kafka/root.go index 0b94c52b..a5d10a45 100644 --- a/inlet/kafka/root.go +++ b/inlet/kafka/root.go @@ -87,7 +87,7 @@ func (c *Component) Start() error { Msg("unable to create Kafka client") return fmt.Errorf("unable to create Kafka client: %w", err) } - c.r.MetricCollectorForCurrentModule(kafkaMetrics) + c.r.RegisterMetricCollector(kafkaMetrics) c.kafkaClient = kafkaClient // When dying, close the client diff --git a/outlet/kafka/worker.go b/outlet/kafka/worker.go index e789466b..43eb4e95 100644 --- a/outlet/kafka/worker.go +++ b/outlet/kafka/worker.go @@ -35,7 +35,7 @@ func (c *realComponent) newClient(i int) (*kgo.Client, error) { Msg("unable to create new client") return nil, fmt.Errorf("unable to create Kafka client: %w", err) } - c.r.MetricCollectorForCurrentModule(kafkaMetrics) + c.r.RegisterMetricCollector(kafkaMetrics) return client, nil } diff --git a/outlet/routing/provider/bioris/metrics.go b/outlet/routing/provider/bioris/metrics.go index 0c90fba3..fe92ca63 100644 --- a/outlet/routing/provider/bioris/metrics.go +++ b/outlet/routing/provider/bioris/metrics.go @@ -18,7 +18,7 @@ type metrics struct { // initMetrics initialize the metrics for the BMP component. func (p *Provider) initMetrics() { - p.r.MetricCollector(p.clientMetrics) + p.r.RegisterMetricCollector(p.clientMetrics) p.metrics.risUp = p.r.GaugeVec( reporter.GaugeOpts{ Name: "connection_up", diff --git a/outlet/routing/provider/bioris/root_test.go b/outlet/routing/provider/bioris/root_test.go index bdd254d9..c5d84a33 100644 --- a/outlet/routing/provider/bioris/root_test.go +++ b/outlet/routing/provider/bioris/root_test.go @@ -462,7 +462,7 @@ func TestBioRIS(t *testing.T) { } for try := 2; try >= 0; try-- { - gotMetrics := r.GetMetrics("akvorado_outlet_routing_provider_bioris_") + gotMetrics := r.GetMetrics("akvorado_outlet_routing_provider_bioris_", "-grpc_client") expectedMetrics := map[string]string{ // connection_up may take a bit of time fmt.Sprintf(`connection_up{ris="%s"}`, addr): "1", @@ -506,6 +506,10 @@ func TestNonWorkingBioRIS(t *testing.T) { expectedMetrics := map[string]string{ `connection_up{ris="ris.invalid:1000"}`: "0", `connection_up{ris="192.0.2.10:1000"}`: "0", + `grpc_client_handled_total{grpc_code="Unavailable",grpc_method="GetRouters",grpc_service="bio.ris.RoutingInformationService",grpc_type="unary"}`: "2", + `grpc_client_msg_received_total{grpc_method="GetRouters",grpc_service="bio.ris.RoutingInformationService",grpc_type="unary"}`: "2", + `grpc_client_msg_sent_total{grpc_method="GetRouters",grpc_service="bio.ris.RoutingInformationService",grpc_type="unary"}`: "2", + `grpc_client_started_total{grpc_method="GetRouters",grpc_service="bio.ris.RoutingInformationService",grpc_type="unary"}`: "2", } if diff := helpers.Diff(gotMetrics, expectedMetrics); diff != "" { t.Errorf("Metrics (-got, +want):\n%s", diff)