Procházet zdrojové kódy

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 před 10 roky
rodič
revize
33c33c55a2
2 změnil soubory, kde provedl 20 přidání a 0 odebrání
  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.")