-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter.go
79 lines (62 loc) · 1.7 KB
/
counter.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package em
import (
"context"
"reflect"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
)
// Counter is a synchronous Instrument which supports non-negative increments.
// Complete docs:
// https://opentelemetry.io/docs/specs/otel/metrics/api/#counter
type Counter[T n64] interface {
// Add records a change of n to the counter.
Add(n T, opts ...metric.AddOption)
// AddCtx records a change of n to the counter.
AddCtx(ctx context.Context, n T, opts ...metric.AddOption)
// Inc records a change of 1 to the counter.
Inc(opts ...metric.AddOption)
// IncCtx records a change of 1 to the counter.
IncCtx(ctx context.Context, opts ...metric.AddOption)
}
type counter[T n64] struct {
attrs []attribute.KeyValue
baseAdd[T]
}
func (c *counter[T]) Add(n T, opts ...metric.AddOption) {
c.AddCtx(context.Background(), n, opts...)
}
func (c *counter[T]) AddCtx(ctx context.Context, n T, opts ...metric.AddOption) {
c.baseAdd.Add(ctx, n, append(opts, metric.WithAttributes(c.attrs...))...)
}
func (c *counter[T]) Inc(opts ...metric.AddOption) {
c.AddCtx(context.Background(), 1, opts...)
}
func (c *counter[T]) IncCtx(ctx context.Context, opts ...metric.AddOption) {
c.AddCtx(ctx, 1, opts...)
}
func newCounter[T n64]() buildable { return new(counter[T]) }
func (c *counter[T]) init(field reflect.StructField, attrs ...attribute.KeyValue) error {
if p == nil {
c.baseAdd = dummy[T]{}
return nil
}
var (
base any
err error
)
id, err := getID(field)
if err != nil {
return err
}
if useFloat[T]() {
base, err = p.m.Float64Counter(id)
} else {
base, err = p.m.Int64Counter(id)
}
if err != nil {
return err
}
c.baseAdd = base.(baseAdd[T])
c.attrs = attrs
return nil
}