Преглед изворни кода

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.
Will Glynn пре 10 година
родитељ
комит
33c33c55a2
2 измењених фајлова са 20 додато и 0 уклоњено
  1. 7 0
      registry.go
  2. 13 0
      registry_test.go

+ 7 - 0
registry.go

@@ -157,6 +157,13 @@ func NewPrefixedRegistry(prefix string) Registry {
 	}
 }
 
+func NewPrefixedChildRegistry(parent Registry, prefix string) Registry {
+	return &PrefixedRegistry{
+		underlying: parent,
+		prefix:     prefix,
+	}
+}
+
 // Call the given function for each registered metric.
 func (r *PrefixedRegistry) Each(fn func(string, interface{})) {
 	r.underlying.Each(fn)

+ 13 - 0
registry_test.go

@@ -117,6 +117,19 @@ func TestRegistryGetOrRegisterWithLazyInstantiation(t *testing.T) {
 	}
 }
 
+func TestPrefixedChildRegistryGetOrRegister(t *testing.T) {
+	r := NewRegistry()
+	pr := NewPrefixedChildRegistry(r, "prefix.")
+
+	_ = pr.GetOrRegister("foo", NewCounter)
+
+	r.Each(func(name string, m interface{}) {
+		if name != "prefix.foo" {
+			t.Fatal(name)
+		}
+	})
+}
+
 func TestPrefixedRegistryGetOrRegister(t *testing.T) {
 	r := NewPrefixedRegistry("prefix.")