@@ -28,7 +28,8 @@ import (
28
28
// registered with the wrapped Registerer in a modified way. The modified
29
29
// Collector adds the provided Labels to all Metrics it collects (as
30
30
// 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.
32
33
//
33
34
// WrapRegistererWith provides a way to add fixed labels to a subset of
34
35
// 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 {
51
52
// Registerer. Collectors registered with the returned Registerer will be
52
53
// registered with the wrapped Registerer in a modified way. The modified
53
54
// 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.
54
56
//
55
57
// WrapRegistererWithPrefix is useful to have one place to prefix all metrics of
56
58
// a sub-system. To make this work, register metrics of the sub-system with the
@@ -81,6 +83,9 @@ type wrappingRegisterer struct {
81
83
}
82
84
83
85
func (r * wrappingRegisterer ) Register (c Collector ) error {
86
+ if r .wrappedRegisterer == nil {
87
+ return nil
88
+ }
84
89
return r .wrappedRegisterer .Register (& wrappingCollector {
85
90
wrappedCollector : c ,
86
91
prefix : r .prefix ,
@@ -89,6 +94,9 @@ func (r *wrappingRegisterer) Register(c Collector) error {
89
94
}
90
95
91
96
func (r * wrappingRegisterer ) MustRegister (cs ... Collector ) {
97
+ if r .wrappedRegisterer == nil {
98
+ return
99
+ }
92
100
for _ , c := range cs {
93
101
if err := r .Register (c ); err != nil {
94
102
panic (err )
@@ -97,6 +105,9 @@ func (r *wrappingRegisterer) MustRegister(cs ...Collector) {
97
105
}
98
106
99
107
func (r * wrappingRegisterer ) Unregister (c Collector ) bool {
108
+ if r .wrappedRegisterer == nil {
109
+ return false
110
+ }
100
111
return r .wrappedRegisterer .Unregister (& wrappingCollector {
101
112
wrappedCollector : c ,
102
113
prefix : r .prefix ,
0 commit comments