Skip to content

Commit

Permalink
Improve metrics error message (ava-labs#1598)
Browse files Browse the repository at this point in the history
  • Loading branch information
anusha-ctrl committed Jun 8, 2023
1 parent 7b9912d commit 26242ce
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 13 deletions.
8 changes: 4 additions & 4 deletions api/metrics/multi_gatherer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (
)

var (
errDuplicatedPrefix = errors.New("duplicated prefix")

_ MultiGatherer = (*multiGatherer)(nil)

errReregisterGatherer = errors.New("attempt to register existing gatherer")
)

// MultiGatherer extends the Gatherer interface by allowing additional gatherers
Expand Down Expand Up @@ -77,8 +77,8 @@ func (g *multiGatherer) Register(namespace string, gatherer prometheus.Gatherer)
g.lock.Lock()
defer g.lock.Unlock()

if _, exists := g.gatherers[namespace]; exists {
return errDuplicatedPrefix
if existingGatherer, exists := g.gatherers[namespace]; exists {
return fmt.Errorf("%w for namespace %q failed; existing: %#v", errReregisterGatherer, namespace, existingGatherer)
}

g.gatherers[namespace] = gatherer
Expand Down
2 changes: 1 addition & 1 deletion api/metrics/multi_gatherer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestMultiGathererDuplicatedPrefix(t *testing.T) {
require.NoError(g.Register("", og))

err := g.Register("", og)
require.ErrorIs(err, errDuplicatedPrefix)
require.ErrorIs(err, errReregisterGatherer)

require.NoError(g.Register("lol", og))
}
Expand Down
10 changes: 3 additions & 7 deletions api/metrics/optional_gatherer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@
package metrics

import (
"errors"
"fmt"
"sync"

"github.com/prometheus/client_golang/prometheus"

dto "github.com/prometheus/client_model/go"
)

var (
errDuplicatedRegister = errors.New("duplicated register")

_ OptionalGatherer = (*optionalGatherer)(nil)
)
var _ OptionalGatherer = (*optionalGatherer)(nil)

// OptionalGatherer extends the Gatherer interface by allowing the optional
// registration of a single gatherer. If no gatherer is registered, Gather will
Expand Down Expand Up @@ -54,7 +50,7 @@ func (g *optionalGatherer) Register(gatherer prometheus.Gatherer) error {
defer g.lock.Unlock()

if g.gatherer != nil {
return errDuplicatedRegister
return fmt.Errorf("%w; %#v", errReregisterGatherer, g.gatherer)
}
g.gatherer = gatherer
return nil
Expand Down
2 changes: 1 addition & 1 deletion api/metrics/optional_gatherer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestOptionalGathererDuplicated(t *testing.T) {

require.NoError(g.Register(og))
err := g.Register(og)
require.ErrorIs(err, errDuplicatedRegister)
require.ErrorIs(err, errReregisterGatherer)
}

func TestOptionalGathererAddedError(t *testing.T) {
Expand Down

0 comments on commit 26242ce

Please sign in to comment.