Debian Codegraph @

sdk/trace/internal/observ/simple_span_processor.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package observ // import "go.opentelemetry.io/otel/sdk/trace/internal/observ"
5
6 import (
7 "context"
8 "fmt"
9 "sync"
10
11 "go.opentelemetry.io/otel"
12 "go.opentelemetry.io/otel/attribute"
13 "go.opentelemetry.io/otel/metric"
14 "go.opentelemetry.io/otel/sdk"
15 "go.opentelemetry.io/otel/sdk/internal/x"
16 semconv "go.opentelemetry.io/otel/semconv/v1.40.0"
17 "go.opentelemetry.io/otel/semconv/v1.40.0/otelconv"
18 )
19
20 var measureAttrsPool = sync.Pool{
21 New: func() any {
22 // "component.name" + "component.type" + "error.type"
23 const n = 1 + 1 + 1
24 s := make([]attribute.KeyValue, 0, n)
25 // Return a pointer to a slice instead of a slice itself
26 // to avoid allocations on every call.
27 return &s
28 },
29 }
30
31 // SSP is the instrumentation for an OTel SDK SimpleSpanProcessor.
32 type SSP struct {
33 spansProcessedCounter metric.Int64Counter
34 addOpts []metric.AddOption
35 attrs []attribute.KeyValue
36 }
37
38 // SSPComponentName returns the component name attribute for a
39 // SimpleSpanProcessor with the given ID.
40 func SSPComponentName(id int64) attribute.KeyValue {
41 t := otelconv.ComponentTypeSimpleSpanProcessor
42 name := fmt.Sprintf("%s/%d", t, id)
43 return semconv.OTelComponentName(name)
44 }
45
46 // NewSSP returns instrumentation for an OTel SDK SimpleSpanProcessor with the
47 // provided ID.
48 //
49 // If the experimental observability is disabled, nil is returned.
50 func NewSSP(id int64) (*SSP, error) {
51 if !x.Observability.Enabled() {
52 return nil, nil
53 }
54
55 meter := otel.GetMeterProvider().Meter(
56 ScopeName,
57 metric.WithInstrumentationVersion(sdk.Version()),
58 metric.WithSchemaURL(SchemaURL),
59 )
60 spansProcessedCounter, err := otelconv.NewSDKProcessorSpanProcessed(meter)
61 if err != nil {
62 err = fmt.Errorf("failed to create SSP processed spans metric: %w", err)
63 }
64
65 componentName := SSPComponentName(id)
66 componentType := spansProcessedCounter.AttrComponentType(otelconv.ComponentTypeSimpleSpanProcessor)
67 attrs := []attribute.KeyValue{componentName, componentType}
68 addOpts := []metric.AddOption{metric.WithAttributeSet(attribute.NewSet(attrs...))}
69
70 return &SSP{
71 spansProcessedCounter: spansProcessedCounter.Inst(),
72 addOpts: addOpts,
73 attrs: attrs,
74 }, err
75 }
76
77 // SpanProcessed records that a span has been processed by the SimpleSpanProcessor.
78 // If err is non-nil, it records the processing error as an attribute.
79 func (ssp *SSP) SpanProcessed(ctx context.Context, err error) {
80 ssp.spansProcessedCounter.Add(ctx, 1, ssp.addOption(err)...)
81 }
82
83 func (ssp *SSP) addOption(err error) []metric.AddOption {
84 if err == nil {
85 return ssp.addOpts
86 }
87 attrs := measureAttrsPool.Get().(*[]attribute.KeyValue)
88 defer func() {
89 *attrs = (*attrs)[:0] // reset the slice for reuse
90 measureAttrsPool.Put(attrs)
91 }()
92 *attrs = append(*attrs, ssp.attrs...)
93 *attrs = append(*attrs, semconv.ErrorType(err))
94 // Do not inefficiently make a copy of attrs by using
95 // WithAttributes instead of WithAttributeSet.
96 return []metric.AddOption{metric.WithAttributeSet(attribute.NewSet(*attrs...))}
97 }