-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathmetrics_internal.go
More file actions
387 lines (335 loc) · 14 KB
/
Copy pathmetrics_internal.go
File metadata and controls
387 lines (335 loc) · 14 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package otel // import "go.opentelemetry.io/obi/pkg/export/otel"
import (
"context"
"log/slog"
"runtime"
"time"
"github.com/google/uuid"
"go.opentelemetry.io/otel/attribute"
instrument "go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.41.0"
"go.opentelemetry.io/obi/pkg/appolly/meta"
"go.opentelemetry.io/obi/pkg/buildinfo"
attr "go.opentelemetry.io/obi/pkg/export/attributes/names"
"go.opentelemetry.io/obi/pkg/export/imetrics"
"go.opentelemetry.io/obi/pkg/export/otel/otelcfg"
"go.opentelemetry.io/obi/pkg/internal/avoidedsvc"
"go.opentelemetry.io/obi/pkg/pipe/global"
)
// InternalMetricsReporter is an internal metrics Reporter that exports to OTEL
type InternalMetricsReporter struct {
ctx context.Context
tracerFlushes instrument.Float64Histogram
otelMetricExports instrument.Float64Counter
otelMetricExportErrs instrument.Float64Counter
otelTraceExports instrument.Float64Counter
otelTraceExportErrs instrument.Float64Counter
instrumentedProcesses instrument.Int64UpDownCounter
instrumentationErrors instrument.Int64Counter
avoidedServices instrument.Int64Gauge
avoidedServicesLimiter *avoidedsvc.Limiter
buildInfo instrument.Int64Gauge
bpfProbeExecutions instrument.Int64Counter
bpfProbeLatencySum instrument.Float64Counter
bpfMapEntries instrument.Int64Gauge
bpfMapMaxEntries instrument.Int64Gauge
bpfInternalMetricsScrapeInterval time.Duration
informerLag instrument.Float64Histogram
// used for calculating deltas from an absolute value
totalPackets uint64
totalIgnoredPackets uint64
bpfPacketCount instrument.Int64Counter
bpfIgnoredPacketCount instrument.Int64Counter
queueCapacityRatio instrument.Float64Gauge
}
func imlog() *slog.Logger {
return slog.With("component", "otel.InternalMetricsReporter")
}
func NewInternalMetricsReporter(ctx context.Context, ctxInfo *global.ContextInfo, metrics *otelcfg.MetricsConfig, internalMetrics *imetrics.InternalMetricsConfig) (*InternalMetricsReporter, error) {
log := imlog()
log.Debug("instantiating internal metrics exporter provider")
exporter, err := ctxInfo.OTELMetricsExporter.Instantiate(ctx)
if err != nil {
log.Error("can't instantiate metrics exporter", "error", err)
return nil, err
}
res := newResourceInternal(&ctxInfo.NodeMeta)
provider := newInternalMeterProvider(res, &exporter, metrics.Interval)
meter := provider.Meter("obi_internal")
tracerFlushes, err := meter.Float64Histogram(
attr.VendorPrefix+".ebpf.tracer.flushes",
instrument.WithDescription("Length of the groups of traces flushed from the eBPF tracer to the next pipeline stage"),
instrument.WithUnit("1"),
)
if err != nil {
return nil, err
}
otelMetricExports, err := meter.Float64Counter(
attr.VendorPrefix+".otel.metric.exports",
instrument.WithDescription("Length of the metric batches submitted to the remote OTEL collector"),
)
if err != nil {
return nil, err
}
otelMetricExportErrs, err := meter.Float64Counter(
attr.VendorPrefix+".otel.metric.export.errors",
instrument.WithDescription("Error count on each failed OTEL metric export"),
)
if err != nil {
return nil, err
}
otelTraceExports, err := meter.Float64Counter(
attr.VendorPrefix+".otel.trace.exports",
instrument.WithDescription("Length of the trace batches submitted to the remote OTEL collector"),
)
if err != nil {
return nil, err
}
otelTraceExportErrs, err := meter.Float64Counter(
attr.VendorPrefix+".otel.trace.export.errors",
instrument.WithDescription("Error count on each failed OTEL trace export"),
)
if err != nil {
return nil, err
}
instrumentedProcesses, err := meter.Int64UpDownCounter(
attr.VendorPrefix+".instrumented.processes",
instrument.WithDescription("Total number of instrumented processes by process name"),
)
if err != nil {
return nil, err
}
instrumentationErrors, err := meter.Int64Counter(
attr.VendorPrefix+".instrumentation.errors",
instrument.WithDescription("Total number of instrumentation errors by process name and error type"),
)
if err != nil {
return nil, err
}
var avoidedServices instrument.Int64Gauge
var avoidedServicesLimiter *avoidedsvc.Limiter
if !internalMetrics.AvoidedServices.Disabled {
avoidedServices, err = meter.Int64Gauge(
attr.VendorPrefix+".avoided.services",
instrument.WithDescription("Services avoided due to existing OpenTelemetry instrumentation"),
)
if err != nil {
return nil, err
}
avoidedServicesLimiter = avoidedsvc.NewLimiter(internalMetrics.AvoidedServices.Limit)
}
buildInfo, err := meter.Int64Gauge(
attr.VendorPrefix+".internal.build.info",
instrument.WithDescription("A metric with a constant '1' value labeled by version, revision, branch, goversion, goos and goarch during build."),
)
if err != nil {
return nil, err
}
bpfProbeExecutions, err := meter.Int64Counter(
attr.VendorPrefix+".bpf.probe.executions",
instrument.WithDescription("Total number of eBPF probe executions"),
instrument.WithUnit("{call}"),
)
if err != nil {
return nil, err
}
bpfProbeLatencySum, err := meter.Float64Counter(
attr.VendorPrefix+".bpf.probe.latency_seconds_total",
instrument.WithDescription("Total latency of the eBPF probe in seconds"),
instrument.WithUnit("s"),
)
if err != nil {
return nil, err
}
bpfMapEntries, err := meter.Int64Gauge(
attr.VendorPrefix+".bpf.map.entries_total",
instrument.WithDescription("Number of entries in the eBPF map"),
)
if err != nil {
return nil, err
}
bpfMapMaxEntries, err := meter.Int64Gauge(
attr.VendorPrefix+".bpf.map.max_entries_total",
instrument.WithDescription("Max number of entries in the eBPF map"),
)
if err != nil {
return nil, err
}
informerLag, err := meter.Float64Histogram(
attr.VendorPrefix+".kube.cache.forward.lag",
instrument.WithDescription("How long, in seconds, it takes since a Kubernetes event happens until it is forwarded to the subscribers"),
instrument.WithUnit("s"),
instrument.WithExplicitBucketBoundaries(
imetrics.InformerLagBuckets...,
),
)
if err != nil {
return nil, err
}
bpfIgnoredPacketCount, err := meter.Int64Counter(
attr.VendorPrefix+".bpf.network.ignored.packets.total",
instrument.WithDescription("How many network packets have been internally ignored due to collisions in the internal eBPF cache"),
instrument.WithUnit("{packet}"),
)
if err != nil {
return nil, err
}
bpfPacketCount, err := meter.Int64Counter(
attr.VendorPrefix+".bpf.network.packets.total",
instrument.WithDescription("How many network packets have been internally accounted"),
instrument.WithUnit("{packet}"),
)
if err != nil {
return nil, err
}
queueCapacityRatio, err := meter.Float64Gauge(
attr.VendorPrefix+".queue.capacity.ratio",
instrument.WithUnit("Ratio [0-1] between the unread messages of an internal Go channel and its total capacity"))
if err != nil {
return nil, err
}
return &InternalMetricsReporter{
ctx: ctx,
tracerFlushes: tracerFlushes,
otelMetricExports: otelMetricExports,
otelMetricExportErrs: otelMetricExportErrs,
otelTraceExports: otelTraceExports,
otelTraceExportErrs: otelTraceExportErrs,
instrumentedProcesses: instrumentedProcesses,
instrumentationErrors: instrumentationErrors,
avoidedServices: avoidedServices,
avoidedServicesLimiter: avoidedServicesLimiter,
buildInfo: buildInfo,
bpfProbeExecutions: bpfProbeExecutions,
bpfProbeLatencySum: bpfProbeLatencySum,
bpfMapEntries: bpfMapEntries,
bpfMapMaxEntries: bpfMapMaxEntries,
bpfInternalMetricsScrapeInterval: internalMetrics.BpfMetricScrapeInterval,
informerLag: informerLag,
bpfPacketCount: bpfPacketCount,
bpfIgnoredPacketCount: bpfIgnoredPacketCount,
queueCapacityRatio: queueCapacityRatio,
}, nil
}
func newInternalMeterProvider(res *resource.Resource, exporter *metric.Exporter, interval time.Duration) *metric.MeterProvider {
return metric.NewMeterProvider(
metric.WithResource(res),
metric.WithReader(metric.NewPeriodicReader(*exporter, metric.WithInterval(interval))),
)
}
func (p *InternalMetricsReporter) Start(ctx context.Context) {
p.buildInfo.Record(ctx, 1, instrument.WithAttributes(attribute.String("goarch", runtime.GOARCH), attribute.String("goos", runtime.GOOS), attribute.String("goversion", runtime.Version()), attribute.String("version", buildinfo.Version), attribute.String("revision", buildinfo.Revision)))
}
func (p *InternalMetricsReporter) TracerFlush(length int) {
p.tracerFlushes.Record(p.ctx, float64(length))
}
func (p *InternalMetricsReporter) OTELMetricExport(length int) {
p.otelMetricExports.Add(p.ctx, float64(length))
}
func (p *InternalMetricsReporter) OTELMetricExportError(err error) {
p.otelMetricExportErrs.Add(p.ctx, 1, instrument.WithAttributes(attribute.String("error", err.Error())))
}
func (p *InternalMetricsReporter) OTELTraceExport(length int) {
p.otelTraceExports.Add(p.ctx, float64(length))
}
func (p *InternalMetricsReporter) OTELTraceExportError(err error) {
p.otelTraceExportErrs.Add(p.ctx, 1, instrument.WithAttributes(attribute.String("error", err.Error())))
}
func (p *InternalMetricsReporter) PrometheusRequest(_, _ string) {
}
func (p *InternalMetricsReporter) InstrumentProcess(processName string) {
p.instrumentedProcesses.Add(p.ctx, 1, instrument.WithAttributes(attribute.String("process_name", processName)))
}
func (p *InternalMetricsReporter) UninstrumentProcess(processName string) {
p.instrumentedProcesses.Add(p.ctx, -1, instrument.WithAttributes(attribute.String("process_name", processName)))
}
func (p *InternalMetricsReporter) InstrumentationError(processName, errorType string) {
p.instrumentationErrors.Add(p.ctx, 1, instrument.WithAttributes(
attribute.String("process_name", processName),
attribute.String("error_type", errorType),
))
}
func newResourceInternal(nodeMeta *meta.NodeMeta) *resource.Resource {
attrs := []attribute.KeyValue{
semconv.ServiceName(attr.TelemetryDistroName),
semconv.ServiceInstanceID(uuid.New().String()),
semconv.TelemetrySDKLanguageKey.String(semconv.TelemetrySDKLanguageGo.Value.AsString()),
semconv.TelemetrySDKNameKey.String(attr.VendorSDKName),
semconv.TelemetrySDKVersion(attr.VendorSDKVersion),
semconv.TelemetryDistroName(attr.TelemetryDistroName),
semconv.TelemetryDistroVersion(attr.TelemetryDistroVersion),
semconv.HostID(nodeMeta.HostID),
}
for _, event := range nodeMeta.Metadata {
attrs = append(attrs, event.Key.OTEL().String(event.Value))
}
return resource.NewWithAttributes(semconv.SchemaURL, attrs...)
}
func (p *InternalMetricsReporter) recordAvoidedService(serviceName, serviceNamespace, serviceInstanceID, telemetryType string) {
if p.avoidedServices == nil {
return
}
labels := p.avoidedServicesLimiter.Labels(serviceName, serviceNamespace, serviceInstanceID, telemetryType)
var attrs []attribute.KeyValue
if labels.Overflow {
attrs = []attribute.KeyValue{
attribute.Bool(avoidedsvc.OverflowAttribute, true),
}
} else {
attrs = []attribute.KeyValue{
semconv.ServiceName(labels.ServiceName),
semconv.ServiceNamespace(labels.ServiceNamespace),
attribute.String("telemetry.type", labels.TelemetryType),
}
}
p.avoidedServices.Record(p.ctx, 1, instrument.WithAttributes(attrs...))
}
func (p *InternalMetricsReporter) AvoidInstrumentationMetrics(serviceName, serviceNamespace, serviceInstanceID string) {
p.recordAvoidedService(serviceName, serviceNamespace, serviceInstanceID, "metrics")
}
func (p *InternalMetricsReporter) AvoidInstrumentationTraces(serviceName, serviceNamespace, serviceInstanceID string) {
p.recordAvoidedService(serviceName, serviceNamespace, serviceInstanceID, "traces")
}
func (p *InternalMetricsReporter) BpfProbeStats(probeID, probeType, probeName string, count uint64, latencySumSeconds float64) {
attrs := []attribute.KeyValue{
attribute.String("bpf.probe.id", probeID),
attribute.String("bpf.probe.type", probeType),
attribute.String("bpf.probe.name", probeName),
}
p.bpfProbeExecutions.Add(p.ctx, int64(count), instrument.WithAttributes(attrs...))
p.bpfProbeLatencySum.Add(p.ctx, latencySumSeconds, instrument.WithAttributes(attrs...))
}
func (p *InternalMetricsReporter) BpfMapEntries(mapID, mapName, mapType string, entriesTotal int) {
attrs := []attribute.KeyValue{
attribute.String("bpf.map.id", mapID),
attribute.String("bpf.map.type", mapType),
attribute.String("bpf.map.name", mapName),
}
p.bpfMapEntries.Record(p.ctx, int64(entriesTotal), instrument.WithAttributes(attrs...))
}
func (p *InternalMetricsReporter) BpfMapMaxEntries(mapID, mapName, mapType string, maxEntries int) {
attrs := []attribute.KeyValue{
attribute.String("bpf.map.id", mapID),
attribute.String("bpf.map.type", mapType),
attribute.String("bpf.map.name", mapName),
}
p.bpfMapMaxEntries.Record(p.ctx, int64(maxEntries), instrument.WithAttributes(attrs...))
}
func (p *InternalMetricsReporter) BpfInternalMetricsScrapeInterval() time.Duration {
return p.bpfInternalMetricsScrapeInterval
}
func (p *InternalMetricsReporter) InformerLag(seconds float64) {
p.informerLag.Record(p.ctx, seconds)
}
func (p *InternalMetricsReporter) BPFPacketStats(count, ignored uint64) {
p.bpfPacketCount.Add(p.ctx, int64(count-p.totalPackets))
p.bpfIgnoredPacketCount.Add(p.ctx, int64(ignored-p.totalIgnoredPackets))
p.totalPackets, p.totalIgnoredPackets = count, ignored
}
func (p *InternalMetricsReporter) QueueBufferUtilization(subscriber string, ratio float64) {
p.queueCapacityRatio.Record(p.ctx, ratio, instrument.WithAttributes(attribute.String("subscriber", subscriber)))
}