From 2ef34bf29a3d8a7d8e25e4eb5d1baf99b09c4704 Mon Sep 17 00:00:00 2001 From: Quentin Mc Gaw Date: Wed, 15 Jan 2025 11:12:22 +0100 Subject: [PATCH] =?UTF-8?q?Minor=20changes=20-=20add=20exported=20comments?= =?UTF-8?q?=20-=20rename=20`g`=20to=20`gatherer`=20in=20test=20-=20Format?= =?UTF-8?q?=20copyright=20as=20it=20as=20=E2=84=A2=EF=B8=8F=20-=20Remove?= =?UTF-8?q?=20TODO?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- metrics/prometheus/interfaces.go | 2 ++ metrics/prometheus/prometheus.go | 17 +++++++++-------- metrics/prometheus/prometheus_test.go | 8 ++++---- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/metrics/prometheus/interfaces.go b/metrics/prometheus/interfaces.go index 234627d862..b9484722dd 100644 --- a/metrics/prometheus/interfaces.go +++ b/metrics/prometheus/interfaces.go @@ -2,6 +2,8 @@ // See the file LICENSE for licensing terms. package prometheus +// Registry is a narrower interface of [prometheus.Registry] containing +// only the required functions for the [Gatherer]. type Registry interface { // Call the given function for each registered metric. Each(func(string, any)) diff --git a/metrics/prometheus/prometheus.go b/metrics/prometheus/prometheus.go index d8e9d86095..07c14004ff 100644 --- a/metrics/prometheus/prometheus.go +++ b/metrics/prometheus/prometheus.go @@ -1,4 +1,4 @@ -// (c) 2021-2025 Ava Labs, Inc. All rights reserved. +// (c) 2021-2025, Ava Labs, Inc. All rights reserved. // See the file LICENSE for licensing terms. package prometheus @@ -16,20 +16,23 @@ import ( dto "github.com/prometheus/client_model/go" ) +// Gatherer implements [prometheus.Gatherer] interface by +// gathering all metrics from the given Prometheus registry. type Gatherer struct { registry Registry } var _ prometheus.Gatherer = (*Gatherer)(nil) -// NewGatherer returns a gatherer using the given registry. -// Note this gatherer implements the [prometheus.Gatherer] interface. +// NewGatherer returns a [Gatherer] using the given registry. func NewGatherer(registry Registry) *Gatherer { return &Gatherer{ registry: registry, } } +// Gather gathers metrics from the registry and converts them to +// a slice of metric families. func (g *Gatherer) Gather() (mfs []*dto.MetricFamily, err error) { // Gather and pre-sort the metrics to avoid random listings var names []string @@ -42,8 +45,8 @@ func (g *Gatherer) Gather() (mfs []*dto.MetricFamily, err error) { for _, name := range names { mf, err := metricFamily(g.registry, name) if err != nil { - if errors.Is(err, errMetricSkip) { - continue + if errors.Is(err, errMetricSkip) { + continue } return nil, err } @@ -184,9 +187,7 @@ func metricFamily(registry Registry, name string) (mf *dto.MetricFamily, err err Metric: []*dto.Metric{{ Summary: &dto.Summary{ SampleCount: ptrTo(uint64(snapshot.Count())), //nolint:gosec - // TODO: do we need to specify SampleSum here? and if so - // what should that be? - Quantile: dtoQuantiles, + Quantile: dtoQuantiles, }, }}, }, nil diff --git a/metrics/prometheus/prometheus_test.go b/metrics/prometheus/prometheus_test.go index 9c59142208..e58f0790c5 100644 --- a/metrics/prometheus/prometheus_test.go +++ b/metrics/prometheus/prometheus_test.go @@ -1,4 +1,4 @@ -// (c) 2021-2025 Ava Labs, Inc. All rights reserved. +// (c) 2021-2025, Ava Labs, Inc. All rights reserved. // See the file LICENSE for licensing terms. package prometheus @@ -69,9 +69,9 @@ func TestGatherer_Gather(t *testing.T) { emptyResettingTimer.Update(time.Second) // no effect because of snapshot below register(t, "test/empty_resetting_timer_snapshot", emptyResettingTimer.Snapshot()) - g := NewGatherer(registry) + gatherer := NewGatherer(registry) - families, err := g.Gather() + families, err := gatherer.Gather() require.NoError(t, err) familyStrings := make([]string, len(families)) for i := range families { @@ -90,7 +90,7 @@ func TestGatherer_Gather(t *testing.T) { assert.Equal(t, want, familyStrings) register(t, "unsupported", metrics.NewGaugeInfo()) - families, err = g.Gather() + families, err = gatherer.Gather() assert.EqualError(t, err, "metric \"unsupported\": type is not supported: *metrics.StandardGaugeInfo") assert.Empty(t, families) }