浏览代码

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.")