-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathmetrics.go
216 lines (185 loc) · 5.12 KB
/
metrics.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package telemetry // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/deltatocumulativeprocessor/internal/telemetry"
import (
"context"
"errors"
"time"
"go.opentelemetry.io/collector/processor/processorhelper"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/deltatocumulativeprocessor/internal/delta"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/deltatocumulativeprocessor/internal/metadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/deltatocumulativeprocessor/internal/streams"
)
type Telemetry struct {
Metrics
}
func New(meter metric.Meter) Telemetry {
return Telemetry{
Metrics: metrics(meter),
}
}
type Streams struct {
tracked metric.Int64UpDownCounter
limit metric.Int64ObservableGauge
evicted metric.Int64Counter
stale metric.Int64ObservableGauge
}
type Datapoints struct {
total metric.Int64Counter
dropped metric.Int64Counter
}
type Metrics struct {
streams Streams
dps Datapoints
gaps metric.Int64Counter
}
func metrics(meter metric.Meter) Metrics {
var (
count = use(meter.Int64Counter)
updown = use(meter.Int64UpDownCounter)
gauge = use(meter.Int64ObservableGauge)
)
return Metrics{
streams: Streams{
tracked: updown("streams.tracked",
metric.WithDescription("number of streams tracked"),
metric.WithUnit("{stream}"),
),
limit: gauge("streams.limit",
metric.WithDescription("upper limit of tracked streams"),
metric.WithUnit("{stream}"),
),
evicted: count("streams.evicted",
metric.WithDescription("number of streams evicted"),
metric.WithUnit("{stream}"),
),
stale: gauge("streams.max_stale",
metric.WithDescription("duration without new samples after which streams are dropped"),
metric.WithUnit("s"),
),
},
dps: Datapoints{
total: count("datapoints.processed",
metric.WithDescription("number of datapoints processed"),
metric.WithUnit("{datapoint}"),
),
dropped: count("datapoints.dropped",
metric.WithDescription("number of dropped datapoints due to given 'reason'"),
metric.WithUnit("{datapoint}"),
),
},
gaps: count("gaps.length",
metric.WithDescription("total duration where data was expected but not received"),
metric.WithUnit("s"),
),
}
}
func (m Metrics) WithLimit(meter metric.Meter, max int64) {
then := metric.Callback(func(_ context.Context, o metric.Observer) error {
o.ObserveInt64(m.streams.limit, max)
return nil
})
_, err := meter.RegisterCallback(then, m.streams.limit)
if err != nil {
panic(err)
}
}
func (m Metrics) WithStale(meter metric.Meter, max time.Duration) {
then := metric.Callback(func(_ context.Context, o metric.Observer) error {
o.ObserveInt64(m.streams.stale, int64(max.Seconds()))
return nil
})
_, err := meter.RegisterCallback(then, m.streams.stale)
if err != nil {
panic(err)
}
}
func ObserveItems[T any](items streams.Map[T], metrics *Metrics) Items[T] {
return Items[T]{
Map: items,
Metrics: metrics,
}
}
func ObserveNonFatal[T any](items streams.Map[T], metrics *Metrics) Faults[T] {
return Faults[T]{
Map: items,
Metrics: metrics,
}
}
type Items[T any] struct {
streams.Map[T]
*Metrics
}
func (i Items[T]) Store(id streams.Ident, v T) error {
inc(i.dps.total)
_, old := i.Map.Load(id)
err := i.Map.Store(id, v)
if err == nil && !old {
inc(i.streams.tracked)
}
return err
}
func (i Items[T]) Delete(id streams.Ident) {
dec(i.streams.tracked)
i.Map.Delete(id)
}
type Faults[T any] struct {
streams.Map[T]
*Metrics
}
func (f Faults[T]) Store(id streams.Ident, v T) error {
var (
olderStart delta.ErrOlderStart
outOfOrder delta.ErrOutOfOrder
gap delta.ErrGap
limit streams.ErrLimit
evict streams.ErrEvicted
)
err := f.Map.Store(id, v)
switch {
default:
return err
case errors.As(err, &olderStart):
inc(f.dps.dropped, reason("older-start"))
case errors.As(err, &outOfOrder):
inc(f.dps.dropped, reason("out-of-order"))
case errors.As(err, &limit):
inc(f.dps.dropped, reason("stream-limit"))
case errors.As(err, &evict):
inc(f.streams.evicted)
case errors.As(err, &gap):
from := gap.From.AsTime()
to := gap.To.AsTime()
lost := to.Sub(from).Seconds()
f.gaps.Add(context.TODO(), int64(lost))
}
return nil
}
var (
_ streams.Map[any] = (*Items[any])(nil)
_ streams.Map[any] = (*Faults[any])(nil)
)
type addable[Opts any] interface {
Add(context.Context, int64, ...Opts)
}
func inc[A addable[O], O any](a A, opts ...O) {
a.Add(context.Background(), 1, opts...)
}
func dec[A addable[O], O any](a A, opts ...O) {
a.Add(context.Background(), -1, opts...)
}
func reason(reason string) metric.AddOption {
return metric.WithAttributes(attribute.String("reason", reason))
}
func use[F func(string, ...O) (M, error), M any, O any](f F) func(string, ...O) M {
return func(name string, opts ...O) M {
name = processorhelper.BuildCustomMetricName(metadata.Type.String(), name)
m, err := f(name, opts...)
if err != nil {
panic(err)
}
return m
}
}