Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit 33c33c5

Browse files
committed
Add NewPrefixedChildRegistry()
PrefixedRegistry supports chaining one registry off another, but the public API did not. NewPrefixedChildRegistry() is exactly like NewPrefixedRegistry() except it allows an existing Registry to be used instead of creating a new registry.
1 parent 1ce93ef commit 33c33c5

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

registry.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ func NewPrefixedRegistry(prefix string) Registry {
157157
}
158158
}
159159

160+
func NewPrefixedChildRegistry(parent Registry, prefix string) Registry {
161+
return &PrefixedRegistry{
162+
underlying: parent,
163+
prefix: prefix,
164+
}
165+
}
166+
160167
// Call the given function for each registered metric.
161168
func (r *PrefixedRegistry) Each(fn func(string, interface{})) {
162169
r.underlying.Each(fn)

registry_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@ func TestRegistryGetOrRegisterWithLazyInstantiation(t *testing.T) {
117117
}
118118
}
119119

120+
func TestPrefixedChildRegistryGetOrRegister(t *testing.T) {
121+
r := NewRegistry()
122+
pr := NewPrefixedChildRegistry(r, "prefix.")
123+
124+
_ = pr.GetOrRegister("foo", NewCounter)
125+
126+
r.Each(func(name string, m interface{}) {
127+
if name != "prefix.foo" {
128+
t.Fatal(name)
129+
}
130+
})
131+
}
132+
120133
func TestPrefixedRegistryGetOrRegister(t *testing.T) {
121134
r := NewPrefixedRegistry("prefix.")
122135

0 commit comments

Comments
 (0)