소스 검색

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