Skip to content

Commit 1788aac

Browse files
committed
Feat: Add CounterVec type.
Also, fix comments and staticcheck warnings.
1 parent 830797e commit 1788aac

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

Diff for: ctor.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ var ErrImplemented = errors.New("there is implemenation already injected")
99

1010
var ctorImpl InternalNew = nil
1111

12-
// name is dot spearated path
13-
// must be uniqe, use system naming, and unit postfix, examples:
12+
// New returns a Creator. Name is a dot separated path which must be unique.
13+
// Examples:
14+
// ipfs.blockstore.bloomcache.bloom.miss.total
15+
// ipfs.routing.dht.notresuingstream.total
1416
//
15-
// ipfs.blockstore.bloomcache.bloom.miss.total
16-
// ipfs.routing.dht.notresuingstream.total
17-
//
18-
// both arguemnts are obligatory
17+
// Both arguemnts are mandatory.
1918
func New(name, helptext string) Creator {
2019
if ctorImpl == nil {
2120
return &noop{}
@@ -24,6 +23,8 @@ func New(name, helptext string) Creator {
2423
}
2524
}
2625

26+
// NewCtx is like New but obtains the metric scope from the given
27+
// context.
2728
func NewCtx(ctx context.Context, name, helptext string) Creator {
2829
return New(CtxGetScope(ctx)+"."+name, helptext)
2930
}

Diff for: interface.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1+
// Package metrics provides a unified interface for different metrics
2+
// backends (prometheus etc.).
13
package metrics
24

35
import (
46
"time"
57
)
68

7-
// Increment only metric
9+
// Counter is a metric that can only be incremented.
810
type Counter interface {
911
Inc()
1012
Add(float64) // Only positive
1113
}
1214

13-
// Increse and decrese metric
15+
// CounterVec is a counter with tags.
16+
type CounterVec interface {
17+
IncWithLabelValues(labels ...string)
18+
AddWithLabelValues(n float64, labels ...string)
19+
}
20+
21+
// Gauge is a metric that can be increased and decreased.
1422
type Gauge interface {
1523
Set(float64) // Introduced discontinuity
1624
Inc()
@@ -19,6 +27,7 @@ type Gauge interface {
1927
Sub(float64)
2028
}
2129

30+
// Histogram metric.
2231
type Histogram interface {
2332
Observe(float64) // Adds observation to Histogram
2433
}
@@ -27,16 +36,18 @@ type Summary interface {
2736
Observe(float64) // Adds observation to Summary
2837
}
2938

30-
// Consult http://godoc.org/github.com/prometheus/client_golang/prometheus#SummaryOpts
39+
// SummaryOpts allow specifying options for Summary. See: http://godoc.org/github.com/prometheus/client_golang/prometheus#SummaryOpts .
3140
type SummaryOpts struct {
3241
Objectives map[float64]float64
3342
MaxAge time.Duration
3443
AgeBuckets uint32
3544
BufCap uint32
3645
}
3746

47+
// Creator can be used to create different types of metrics.
3848
type Creator interface {
3949
Counter() Counter
50+
CounterVec() CounterVec
4051
Gauge() Gauge
4152
Histogram(buckets []float64) Histogram
4253

Diff for: noop.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package metrics
22

3-
// Also implements the Counter interface
3+
// Also implements the Counter and CounterVec interfaces
44
type noop struct{}
55

66
func (g *noop) Set(v float64) {
@@ -27,12 +27,24 @@ func (g *noop) Observe(v float64) {
2727
// Noop
2828
}
2929

30+
func (g *noop) IncWithLabelValues(labels ...string) {
31+
// Noop
32+
}
33+
34+
func (g *noop) AddWithLabelValues(v float64, labels ...string) {
35+
// Noop
36+
}
37+
3038
// Creator functions
3139

3240
func (g *noop) Counter() Counter {
3341
return g
3442
}
3543

44+
func (g *noop) CounterVec() CounterVec {
45+
return g
46+
}
47+
3648
func (g *noop) Gauge() Gauge {
3749
return g
3850
}

Diff for: noop_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package metrics
22

33
var _ Counter = (*noop)(nil)
4+
var _ CounterVec = (*noop)(nil)
45
var _ Gauge = (*noop)(nil)
56
var _ Histogram = (*noop)(nil)
67
var _ Summary = (*noop)(nil)

0 commit comments

Comments
 (0)