-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
connector.go
453 lines (391 loc) · 13.7 KB
/
connector.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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package spanmetricsconnector // import "github.com/open-telemetry/opentelemetry-collector-contrib/connector/spanmetricsconnector"
import (
"bytes"
"context"
"sync"
"time"
"github.com/lightstep/go-expohisto/structure"
"github.com/tilinna/clock"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/connector/spanmetricsconnector/internal/cache"
"github.com/open-telemetry/opentelemetry-collector-contrib/connector/spanmetricsconnector/internal/metrics"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/traceutil"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil"
)
const (
serviceNameKey = conventions.AttributeServiceName
spanNameKey = "span.name" // OpenTelemetry non-standard constant.
spanKindKey = "span.kind" // OpenTelemetry non-standard constant.
statusCodeKey = "status.code" // OpenTelemetry non-standard constant.
metricKeySeparator = string(byte(0))
defaultDimensionsCacheSize = 1000
metricNameDuration = "duration"
metricNameCalls = "calls"
defaultUnit = metrics.Milliseconds
)
type connectorImp struct {
lock sync.Mutex
logger *zap.Logger
config Config
metricsConsumer consumer.Metrics
// Additional dimensions to add to metrics.
dimensions []dimension
// The starting time of the data points.
startTimestamp pcommon.Timestamp
resourceMetrics map[resourceKey]*resourceMetrics
keyBuf *bytes.Buffer
// An LRU cache of dimension key-value maps keyed by a unique identifier formed by a concatenation of its values:
// e.g. { "foo/barOK": { "serviceName": "foo", "span.name": "/bar", "status_code": "OK" }}
metricKeyToDimensions *cache.Cache[metrics.Key, pcommon.Map]
ticker *clock.Ticker
done chan struct{}
started bool
shutdownOnce sync.Once
}
type resourceMetrics struct {
histograms metrics.HistogramMetrics
sums metrics.SumMetrics
attributes pcommon.Map
}
type dimension struct {
name string
value *pcommon.Value
}
func newDimensions(cfgDims []Dimension) []dimension {
if len(cfgDims) == 0 {
return nil
}
dims := make([]dimension, len(cfgDims))
for i := range cfgDims {
dims[i].name = cfgDims[i].Name
if cfgDims[i].Default != nil {
val := pcommon.NewValueStr(*cfgDims[i].Default)
dims[i].value = &val
}
}
return dims
}
func newConnector(logger *zap.Logger, config component.Config, ticker *clock.Ticker) (*connectorImp, error) {
logger.Info("Building spanmetrics connector")
cfg := config.(*Config)
metricKeyToDimensionsCache, err := cache.NewCache[metrics.Key, pcommon.Map](cfg.DimensionsCacheSize)
if err != nil {
return nil, err
}
return &connectorImp{
logger: logger,
config: *cfg,
startTimestamp: pcommon.NewTimestampFromTime(time.Now()),
resourceMetrics: make(map[resourceKey]*resourceMetrics),
dimensions: newDimensions(cfg.Dimensions),
keyBuf: bytes.NewBuffer(make([]byte, 0, 1024)),
metricKeyToDimensions: metricKeyToDimensionsCache,
ticker: ticker,
done: make(chan struct{}),
}, nil
}
func initHistogramMetrics(cfg Config) metrics.HistogramMetrics {
if cfg.Histogram.Disable {
return nil
}
if cfg.Histogram.Exponential != nil {
maxSize := structure.DefaultMaxSize
if cfg.Histogram.Exponential.MaxSize != 0 {
maxSize = cfg.Histogram.Exponential.MaxSize
}
return metrics.NewExponentialHistogramMetrics(maxSize)
}
var bounds []float64
if cfg.Histogram.Explicit != nil && cfg.Histogram.Explicit.Buckets != nil {
bounds = durationsToUnits(cfg.Histogram.Explicit.Buckets, unitDivider(cfg.Histogram.Unit))
} else {
switch cfg.Histogram.Unit {
case metrics.Milliseconds:
bounds = defaultHistogramBucketsMs
case metrics.Seconds:
bounds = make([]float64, len(defaultHistogramBucketsMs))
for i, v := range defaultHistogramBucketsMs {
bounds[i] = v / float64(time.Second.Milliseconds())
}
}
}
return metrics.NewExplicitHistogramMetrics(bounds)
}
// unitDivider returns a unit divider to convert nanoseconds to milliseconds or seconds.
func unitDivider(u metrics.Unit) int64 {
return map[metrics.Unit]int64{
metrics.Seconds: time.Second.Nanoseconds(),
metrics.Milliseconds: time.Millisecond.Nanoseconds(),
}[u]
}
func durationsToUnits(vs []time.Duration, unitDivider int64) []float64 {
vsm := make([]float64, len(vs))
for i, v := range vs {
vsm[i] = float64(v.Nanoseconds()) / float64(unitDivider)
}
return vsm
}
// Start implements the component.Component interface.
func (p *connectorImp) Start(ctx context.Context, _ component.Host) error {
p.logger.Info("Starting spanmetrics connector")
p.started = true
go func() {
for {
select {
case <-p.done:
return
case <-p.ticker.C:
p.exportMetrics(ctx)
}
}
}()
return nil
}
// Shutdown implements the component.Component interface.
func (p *connectorImp) Shutdown(context.Context) error {
p.shutdownOnce.Do(func() {
p.logger.Info("Shutting down spanmetrics connector")
if p.started {
p.logger.Info("Stopping ticker")
p.ticker.Stop()
p.done <- struct{}{}
p.started = false
}
})
return nil
}
// Capabilities implements the consumer interface.
func (p *connectorImp) Capabilities() consumer.Capabilities {
return consumer.Capabilities{MutatesData: false}
}
// ConsumeTraces implements the consumer.Traces interface.
// It aggregates the trace data to generate metrics.
func (p *connectorImp) ConsumeTraces(_ context.Context, traces ptrace.Traces) error {
p.lock.Lock()
p.aggregateMetrics(traces)
p.lock.Unlock()
return nil
}
func (p *connectorImp) exportMetrics(ctx context.Context) {
p.lock.Lock()
m := p.buildMetrics()
p.resetState()
// This component no longer needs to read the metrics once built, so it is safe to unlock.
p.lock.Unlock()
if err := p.metricsConsumer.ConsumeMetrics(ctx, m); err != nil {
p.logger.Error("Failed ConsumeMetrics", zap.Error(err))
return
}
}
// buildMetrics collects the computed raw metrics data and builds OTLP metrics.
func (p *connectorImp) buildMetrics() pmetric.Metrics {
m := pmetric.NewMetrics()
for _, rawMetrics := range p.resourceMetrics {
rm := m.ResourceMetrics().AppendEmpty()
rawMetrics.attributes.CopyTo(rm.Resource().Attributes())
sm := rm.ScopeMetrics().AppendEmpty()
sm.Scope().SetName("spanmetricsconnector")
sums := rawMetrics.sums
metric := sm.Metrics().AppendEmpty()
metric.SetName(buildMetricName(p.config.Namespace, metricNameCalls))
sums.BuildMetrics(metric, p.startTimestamp, p.config.GetAggregationTemporality())
if !p.config.Histogram.Disable {
histograms := rawMetrics.histograms
metric = sm.Metrics().AppendEmpty()
metric.SetName(buildMetricName(p.config.Namespace, metricNameDuration))
metric.SetUnit(p.config.Histogram.Unit.String())
histograms.BuildMetrics(metric, p.startTimestamp, p.config.GetAggregationTemporality())
}
}
return m
}
func (p *connectorImp) resetState() {
// If delta metrics, reset accumulated data
if p.config.GetAggregationTemporality() == pmetric.AggregationTemporalityDelta {
p.resourceMetrics = make(map[resourceKey]*resourceMetrics)
p.metricKeyToDimensions.Purge()
p.startTimestamp = pcommon.NewTimestampFromTime(time.Now())
} else {
p.metricKeyToDimensions.RemoveEvictedItems()
// Exemplars are only relevant to this batch of traces, so must be cleared within the lock
if p.config.Histogram.Disable {
return
}
for _, m := range p.resourceMetrics {
m.histograms.Reset(true)
}
}
}
// aggregateMetrics aggregates the raw metrics from the input trace data.
//
// Metrics are grouped by resource attributes.
// Each metric is identified by a key that is built from the service name
// and span metadata such as name, kind, status_code and any additional
// dimensions the user has configured.
func (p *connectorImp) aggregateMetrics(traces ptrace.Traces) {
for i := 0; i < traces.ResourceSpans().Len(); i++ {
rspans := traces.ResourceSpans().At(i)
resourceAttr := rspans.Resource().Attributes()
serviceAttr, ok := resourceAttr.Get(conventions.AttributeServiceName)
if !ok {
continue
}
rm := p.getOrCreateResourceMetrics(resourceAttr)
sums := rm.sums
histograms := rm.histograms
unitDivider := unitDivider(p.config.Histogram.Unit)
serviceName := serviceAttr.Str()
ilsSlice := rspans.ScopeSpans()
for j := 0; j < ilsSlice.Len(); j++ {
ils := ilsSlice.At(j)
spans := ils.Spans()
for k := 0; k < spans.Len(); k++ {
span := spans.At(k)
// Protect against end timestamps before start timestamps. Assume 0 duration.
duration := float64(0)
startTime := span.StartTimestamp()
endTime := span.EndTimestamp()
if endTime > startTime {
duration = float64(endTime-startTime) / float64(unitDivider)
}
key := p.buildKey(serviceName, span, p.dimensions, resourceAttr)
attributes, ok := p.metricKeyToDimensions.Get(key)
if !ok {
attributes = p.buildAttributes(serviceName, span, resourceAttr)
p.metricKeyToDimensions.Add(key, attributes)
}
if !p.config.Histogram.Disable {
// aggregate histogram metrics
h := histograms.GetOrCreate(key, attributes)
p.addExemplar(span, duration, h)
h.Observe(duration)
}
// aggregate sums metrics
s := sums.GetOrCreate(key, attributes)
s.Add(1)
}
}
}
}
func (p *connectorImp) addExemplar(span ptrace.Span, duration float64, h metrics.Histogram) {
if !p.config.Exemplars.Enabled {
return
}
if span.TraceID().IsEmpty() {
return
}
h.AddExemplar(span.TraceID(), span.SpanID(), duration)
}
type resourceKey [16]byte
func (p *connectorImp) getOrCreateResourceMetrics(attr pcommon.Map) *resourceMetrics {
key := resourceKey(pdatautil.MapHash(attr))
v, ok := p.resourceMetrics[key]
if !ok {
v = &resourceMetrics{
histograms: initHistogramMetrics(p.config),
sums: metrics.NewSumMetrics(),
attributes: attr,
}
p.resourceMetrics[key] = v
}
return v
}
// contains checks if string slice contains a string value
func contains(elements []string, value string) bool {
for _, element := range elements {
if value == element {
return true
}
}
return false
}
func (p *connectorImp) buildAttributes(serviceName string, span ptrace.Span, resourceAttrs pcommon.Map) pcommon.Map {
attr := pcommon.NewMap()
attr.EnsureCapacity(4 + len(p.dimensions))
if !contains(p.config.ExcludeDimensions, serviceNameKey) {
attr.PutStr(serviceNameKey, serviceName)
}
if !contains(p.config.ExcludeDimensions, spanNameKey) {
attr.PutStr(spanNameKey, span.Name())
}
if !contains(p.config.ExcludeDimensions, spanKindKey) {
attr.PutStr(spanKindKey, traceutil.SpanKindStr(span.Kind()))
}
if !contains(p.config.ExcludeDimensions, statusCodeKey) {
attr.PutStr(statusCodeKey, traceutil.StatusCodeStr(span.Status().Code()))
}
for _, d := range p.dimensions {
if v, ok := getDimensionValue(d, span.Attributes(), resourceAttrs); ok {
v.CopyTo(attr.PutEmpty(d.name))
}
}
return attr
}
func concatDimensionValue(dest *bytes.Buffer, value string, prefixSep bool) {
if prefixSep {
dest.WriteString(metricKeySeparator)
}
dest.WriteString(value)
}
// buildKey builds the metric key from the service name and span metadata such as name, kind, status_code and
// will attempt to add any additional dimensions the user has configured that match the span's attributes
// or resource attributes. If the dimension exists in both, the span's attributes, being the most specific, takes precedence.
//
// The metric key is a simple concatenation of dimension values, delimited by a null character.
func (p *connectorImp) buildKey(serviceName string, span ptrace.Span, optionalDims []dimension, resourceAttrs pcommon.Map) metrics.Key {
p.keyBuf.Reset()
if !contains(p.config.ExcludeDimensions, serviceNameKey) {
concatDimensionValue(p.keyBuf, serviceName, false)
}
if !contains(p.config.ExcludeDimensions, spanNameKey) {
concatDimensionValue(p.keyBuf, span.Name(), true)
}
if !contains(p.config.ExcludeDimensions, spanKindKey) {
concatDimensionValue(p.keyBuf, traceutil.SpanKindStr(span.Kind()), true)
}
if !contains(p.config.ExcludeDimensions, statusCodeKey) {
concatDimensionValue(p.keyBuf, traceutil.StatusCodeStr(span.Status().Code()), true)
}
for _, d := range optionalDims {
if v, ok := getDimensionValue(d, span.Attributes(), resourceAttrs); ok {
concatDimensionValue(p.keyBuf, v.AsString(), true)
}
}
return metrics.Key(p.keyBuf.String())
}
// getDimensionValue gets the dimension value for the given configured dimension.
// It searches through the span's attributes first, being the more specific;
// falling back to searching in resource attributes if it can't be found in the span.
// Finally, falls back to the configured default value if provided.
//
// The ok flag indicates if a dimension value was fetched in order to differentiate
// an empty string value from a state where no value was found.
func getDimensionValue(d dimension, spanAttr pcommon.Map, resourceAttr pcommon.Map) (v pcommon.Value, ok bool) {
// The more specific span attribute should take precedence.
if attr, exists := spanAttr.Get(d.name); exists {
return attr, true
}
if attr, exists := resourceAttr.Get(d.name); exists {
return attr, true
}
// Set the default if configured, otherwise this metric will have no value set for the dimension.
if d.value != nil {
return *d.value, true
}
return v, ok
}
// buildMetricName builds the namespace prefix for the metric name.
func buildMetricName(namespace string, name string) string {
if namespace != "" {
return namespace + "." + name
}
return name
}