-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathctor.go
45 lines (37 loc) · 936 Bytes
/
ctor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package metrics
import (
"context"
"errors"
)
var ErrImplemented = errors.New("there is implemenation already injected")
var ctorImpl InternalNew = nil
// New returns a Creator. Name is a dot separated path which must be unique.
// Examples:
// ipfs.blockstore.bloomcache.bloom.miss.total
// ipfs.routing.dht.notresuingstream.total
//
// Both arguemnts are mandatory.
func New(name, helptext string) Creator {
if ctorImpl == nil {
return &noop{}
} else {
return ctorImpl(name, helptext)
}
}
// NewCtx is like New but obtains the metric scope from the given
// context.
func NewCtx(ctx context.Context, name, helptext string) Creator {
return New(CtxGetScope(ctx)+"."+name, helptext)
}
type InternalNew func(string, string) Creator
func InjectImpl(newimpl InternalNew) error {
if ctorImpl != nil {
return ErrImplemented
} else {
ctorImpl = newimpl
return nil
}
}
func Active() bool {
return ctorImpl != nil
}