Skip to content

Commit 03575ca

Browse files
authored
Merge pull request #764 from prometheus/wrap-nil
Ensure that nil registers are treat as a no-op, even when wrapping.
2 parents 84c6b9d + 9c8ba1f commit 03575ca

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

prometheus/promauto/auto_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2020 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package promauto
15+
16+
import (
17+
"testing"
18+
19+
"github.com/prometheus/client_golang/prometheus"
20+
)
21+
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()
25+
}

prometheus/wrap.go

Lines changed: 12 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
@@ -81,6 +83,9 @@ type wrappingRegisterer struct {
8183
}
8284

8385
func (r *wrappingRegisterer) Register(c Collector) error {
86+
if r.wrappedRegisterer == nil {
87+
return nil
88+
}
8489
return r.wrappedRegisterer.Register(&wrappingCollector{
8590
wrappedCollector: c,
8691
prefix: r.prefix,
@@ -89,6 +94,9 @@ func (r *wrappingRegisterer) Register(c Collector) error {
8994
}
9095

9196
func (r *wrappingRegisterer) MustRegister(cs ...Collector) {
97+
if r.wrappedRegisterer == nil {
98+
return
99+
}
92100
for _, c := range cs {
93101
if err := r.Register(c); err != nil {
94102
panic(err)
@@ -97,6 +105,9 @@ func (r *wrappingRegisterer) MustRegister(cs ...Collector) {
97105
}
98106

99107
func (r *wrappingRegisterer) Unregister(c Collector) bool {
108+
if r.wrappedRegisterer == nil {
109+
return false
110+
}
100111
return r.wrappedRegisterer.Unregister(&wrappingCollector{
101112
wrappedCollector: c,
102113
prefix: r.prefix,

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)