Skip to content

Commit 9c8ba1f

Browse files
committed
Review feedback: add comment and tests for WrapRegistererWith.
Signed-off-by: Tom Wilkie <[email protected]>
1 parent 614377c commit 9c8ba1f

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

prometheus/promauto/auto_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ import (
1919
"github.com/prometheus/client_golang/prometheus"
2020
)
2121

22-
func TestWrapNil(t *testing.T) {
23-
// A nil registerer should be treated as a no-op by promauto, even when wrapped.
24-
registerer := prometheus.WrapRegistererWith(prometheus.Labels{"foo": "bar"}, nil)
25-
c := With(registerer).NewCounter(prometheus.CounterOpts{Name: "test"})
26-
c.Inc()
22+
func TestNil(t *testing.T) {
23+
// A nil registerer should be treated as a no-op by promauto.
24+
With(nil).NewCounter(prometheus.CounterOpts{Name: "test"}).Inc()
2725
}

prometheus/wrap.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ import (
2828
// registered with the wrapped Registerer in a modified way. The modified
2929
// Collector adds the provided Labels to all Metrics it collects (as
3030
// ConstLabels). The Metrics collected by the unmodified Collector must not
31-
// duplicate any of those labels.
31+
// duplicate any of those labels. Wrapping a nil value is valid, resulting
32+
// in a no-op Registerer.
3233
//
3334
// WrapRegistererWith provides a way to add fixed labels to a subset of
3435
// Collectors. It should not be used to add fixed labels to all metrics exposed.
@@ -51,6 +52,7 @@ func WrapRegistererWith(labels Labels, reg Registerer) Registerer {
5152
// Registerer. Collectors registered with the returned Registerer will be
5253
// registered with the wrapped Registerer in a modified way. The modified
5354
// Collector adds the provided prefix to the name of all Metrics it collects.
55+
// Wrapping a nil value is valid, resulting in a no-op Registerer.
5456
//
5557
// WrapRegistererWithPrefix is useful to have one place to prefix all metrics of
5658
// a sub-system. To make this work, register metrics of the sub-system with the

prometheus/wrap_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,12 @@ func TestWrap(t *testing.T) {
321321
}
322322

323323
}
324+
325+
func TestNil(t *testing.T) {
326+
// A wrapped nil registerer should be treated as a no-op, and not panic.
327+
c := NewCounter(CounterOpts{Name: "test"})
328+
err := WrapRegistererWith(Labels{"foo": "bar"}, nil).Register(c)
329+
if err != nil {
330+
t.Fatal("registering failed:", err)
331+
}
332+
}

0 commit comments

Comments
 (0)