Debian Codegraph @

sdk/trace/simple_span_processor.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package trace // import "go.opentelemetry.io/otel/sdk/trace"
5
6 import (
7 "context"
8 "sync"
9 "sync/atomic"
10
11 "go.opentelemetry.io/otel"
12 "go.opentelemetry.io/otel/internal/global"
13 "go.opentelemetry.io/otel/sdk/trace/internal/observ"
14 "go.opentelemetry.io/otel/trace"
15 )
16
17 // simpleSpanProcessor is a SpanProcessor that synchronously sends all
18 // completed Spans to a trace.Exporter immediately.
19 type simpleSpanProcessor struct {
20 exporterMu sync.Mutex
21 exporter SpanExporter
22 stopOnce sync.Once
23
24 inst *observ.SSP
25 }
26
27 var _ SpanProcessor = (*simpleSpanProcessor)(nil)
28
29 // NewSimpleSpanProcessor returns a new SpanProcessor that will synchronously
30 // send completed spans to the exporter immediately.
31 //
32 // This SpanProcessor is not recommended for production use. The synchronous
33 // nature of this SpanProcessor makes it good for testing, debugging, or showing
34 // examples of other features, but it will be slow and have a high computation
35 // resource usage overhead. The BatchSpanProcessor is recommended for production
36 // use instead.
37 func NewSimpleSpanProcessor(exporter SpanExporter) SpanProcessor {
38 ssp := &simpleSpanProcessor{
39 exporter: exporter,
40 }
41
42 var err error
43 ssp.inst, err = observ.NewSSP(nextSimpleProcessorID())
44 if err != nil {
45 otel.Handle(err)
46 }
47
48 global.Warn("SimpleSpanProcessor is not recommended for production use, consider using BatchSpanProcessor instead.")
49
50 return ssp
51 }
52
53 var simpleProcessorIDCounter atomic.Int64
54
55 // nextSimpleProcessorID returns an identifier for this simple span processor,
56 // starting with 0 and incrementing by 1 each time it is called.
57 func nextSimpleProcessorID() int64 {
58 return simpleProcessorIDCounter.Add(1) - 1
59 }
60
61 // OnStart does nothing.
62 func (*simpleSpanProcessor) OnStart(context.Context, ReadWriteSpan) {}
63
64 // OnEnd immediately exports a ReadOnlySpan.
65 func (ssp *simpleSpanProcessor) OnEnd(s ReadOnlySpan) {
66 ssp.exporterMu.Lock()
67 defer ssp.exporterMu.Unlock()
68
69 var err error
70 if ssp.exporter != nil && s.SpanContext().TraceFlags().IsSampled() {
71 err = ssp.exporter.ExportSpans(context.Background(), []ReadOnlySpan{s})
72 if err != nil {
73 otel.Handle(err)
74 }
75 }
76
77 if ssp.inst != nil {
78 // Add the span to the context to ensure the metric is recorded
79 // with the correct span context.
80 ctx := trace.ContextWithSpanContext(context.Background(), s.SpanContext())
81 ssp.inst.SpanProcessed(ctx, err)
82 }
83 }
84
85 // Shutdown shuts down the exporter this SimpleSpanProcessor exports to.
86 func (ssp *simpleSpanProcessor) Shutdown(ctx context.Context) error {
87 var err error
88 ssp.stopOnce.Do(func() {
89 stopFunc := func(exp SpanExporter) (<-chan error, func()) {
90 done := make(chan error, 1)
91 return done, func() { done <- exp.Shutdown(ctx) }
92 }
93
94 // The exporter field of the simpleSpanProcessor needs to be zeroed to
95 // signal it is shut down, meaning all subsequent calls to OnEnd will
96 // be gracefully ignored. This needs to be done synchronously to avoid
97 // any race condition.
98 //
99 // A closure is used to keep reference to the exporter and then the
100 // field is zeroed. This ensures the simpleSpanProcessor is shut down
101 // before the exporter. This order is important as it avoids a potential
102 // deadlock. If the exporter shut down operation generates a span, that
103 // span would need to be exported. Meaning, OnEnd would be called and
104 // try acquiring the lock that is held here.
105 ssp.exporterMu.Lock()
106 done, shutdown := stopFunc(ssp.exporter)
107 ssp.exporter = nil
108 ssp.exporterMu.Unlock()
109
110 go shutdown()
111
112 // Wait for the exporter to shut down or the deadline to expire.
113 select {
114 case err = <-done:
115 case <-ctx.Done():
116 // It is possible for the exporter to have immediately shut down and
117 // the context to be done simultaneously. In that case this outer
118 // select statement will randomly choose a case. This will result in
119 // a different returned error for similar scenarios. Instead, double
120 // check if the exporter shut down at the same time and return that
121 // error if so. This will ensure consistency as well as ensure
122 // the caller knows the exporter shut down successfully (they can
123 // already determine if the deadline is expired given they passed
124 // the context).
125 select {
126 case err = <-done:
127 default:
128 err = ctx.Err()
129 }
130 }
131 })
132 return err
133 }
134
135 // ForceFlush does nothing as there is no data to flush.
136 func (*simpleSpanProcessor) ForceFlush(context.Context) error {
137 return nil
138 }
139
140 // MarshalLog is the marshaling function used by the logging system to represent
141 // this Span Processor.
142 func (ssp *simpleSpanProcessor) MarshalLog() any {
143 return struct {
144 Type string
145 Exporter SpanExporter
146 }{
147 Type: "SimpleSpanProcessor",
148 Exporter: ssp.exporter,
149 }
150 }