Debian Codegraph @

sdk/trace/internal/observ/batch_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 "errors"
9 "fmt"
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 const (
21 // ScopeName is the name of the instrumentation scope.
22 ScopeName = "go.opentelemetry.io/otel/sdk/trace/internal/observ"
23
24 // SchemaURL is the schema URL of the instrumentation.
25 SchemaURL = semconv.SchemaURL
26 )
27
28 // ErrQueueFull is the attribute value for the "queue_full" error type.
29 var ErrQueueFull = otelconv.SDKProcessorSpanProcessed{}.AttrErrorType(
30 otelconv.ErrorTypeAttr("queue_full"),
31 )
32
33 // BSPComponentName returns the component name attribute for a
34 // BatchSpanProcessor with the given ID.
35 func BSPComponentName(id int64) attribute.KeyValue {
36 t := otelconv.ComponentTypeBatchingSpanProcessor
37 name := fmt.Sprintf("%s/%d", t, id)
38 return semconv.OTelComponentName(name)
39 }
40
41 // BSP is the instrumentation for an OTel SDK BatchSpanProcessor.
42 type BSP struct {
43 reg metric.Registration
44
45 processed metric.Int64Counter
46 processedOpts []metric.AddOption
47 processedQueueFullOpts []metric.AddOption
48 }
49
50 func NewBSP(id int64, qLen func() int64, qMax int64) (*BSP, 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
61 qCap, err := otelconv.NewSDKProcessorSpanQueueCapacity(meter)
62 if err != nil {
63 err = fmt.Errorf("failed to create BSP queue capacity metric: %w", err)
64 }
65 qCapInst := qCap.Inst()
66
67 qSize, e := otelconv.NewSDKProcessorSpanQueueSize(meter)
68 if e != nil {
69 e := fmt.Errorf("failed to create BSP queue size metric: %w", e)
70 err = errors.Join(err, e)
71 }
72 qSizeInst := qSize.Inst()
73
74 cmpntT := semconv.OTelComponentTypeBatchingSpanProcessor
75 cmpnt := BSPComponentName(id)
76 set := attribute.NewSet(cmpnt, cmpntT)
77
78 obsOpts := []metric.ObserveOption{metric.WithAttributeSet(set)}
79 reg, e := meter.RegisterCallback(
80 func(_ context.Context, o metric.Observer) error {
81 o.ObserveInt64(qSizeInst, qLen(), obsOpts...)
82 o.ObserveInt64(qCapInst, qMax, obsOpts...)
83 return nil
84 },
85 qSizeInst,
86 qCapInst,
87 )
88 if e != nil {
89 e := fmt.Errorf("failed to register BSP queue size/capacity callback: %w", e)
90 err = errors.Join(err, e)
91 }
92
93 processed, e := otelconv.NewSDKProcessorSpanProcessed(meter)
94 if e != nil {
95 e := fmt.Errorf("failed to create BSP processed spans metric: %w", e)
96 err = errors.Join(err, e)
97 }
98 processedOpts := []metric.AddOption{metric.WithAttributeSet(set)}
99
100 set = attribute.NewSet(cmpnt, cmpntT, ErrQueueFull)
101 processedQueueFullOpts := []metric.AddOption{metric.WithAttributeSet(set)}
102
103 return &BSP{
104 reg: reg,
105 processed: processed.Inst(),
106 processedOpts: processedOpts,
107 processedQueueFullOpts: processedQueueFullOpts,
108 }, err
109 }
110
111 func (b *BSP) Shutdown() error { return b.reg.Unregister() }
112
113 func (b *BSP) Processed(ctx context.Context, n int64) {
114 b.processed.Add(ctx, n, b.processedOpts...)
115 }
116
117 func (b *BSP) ProcessedQueueFull(ctx context.Context, n int64) {
118 b.processed.Add(ctx, n, b.processedQueueFullOpts...)
119 }