Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve metrics error msging #1598

Merged
merged 9 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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