Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
- add exported comments
- rename `g` to `gatherer` in test
- Format copyright as it as ™️
- Remove TODO
  • Loading branch information
qdm12 committed Jan 15, 2025
1 parent 3c46d37 commit c6a75ea
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
2 changes: 2 additions & 0 deletions metrics/prometheus/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
17 changes: 9 additions & 8 deletions metrics/prometheus/prometheus.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions metrics/prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}

0 comments on commit c6a75ea

Please sign in to comment.