/
dt_span_processor.go
278 lines (237 loc) · 8.73 KB
/
dt_span_processor.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
// Copyright 2022 Dynatrace LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package trace
import (
"context"
"errors"
"sync"
"sync/atomic"
"time"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"github.com/dynatrace-oss/opentelemetry-exporter-go/core/configuration"
"github.com/dynatrace-oss/opentelemetry-exporter-go/core/internal/logger"
)
var errInvalidSpanExporter = errors.New("span exporter is invalid")
type flushContext struct {
ctx context.Context
flushRequestFinished chan struct{}
err error
}
type dtSpanProcessor struct {
exporter dtSpanExporter
spanWatchlist dtSpanWatchlist
stopExportingCh chan struct{}
stopExportingWait sync.WaitGroup
exportingStopped int32
shutdownOnce sync.Once
flushRequestCh chan *flushContext
flushRequestLock sync.Mutex
lastFlushRequestContext *flushContext
periodicSendOpTimer *time.Timer
logger *logger.ComponentLogger
config *configuration.DtConfiguration
}
// newDtSpanProcessor creates a Dynatrace span processor that will send spans to Dynatrace Cluster.
func newDtSpanProcessor(config *configuration.DtConfiguration) *dtSpanProcessor {
p := &dtSpanProcessor{
exporter: newDtSpanExporter(config),
spanWatchlist: newDtSpanWatchlist(configuration.DefaultMaxSpansWatchlistSize),
stopExportingCh: make(chan struct{}, 1),
exportingStopped: 0,
flushRequestCh: make(chan *flushContext, 1),
periodicSendOpTimer: time.NewTimer(time.Millisecond * time.Duration(config.SpanProcessingIntervalMs)),
logger: logger.NewComponentLogger("SpanProcessor"),
config: config,
}
p.stopExportingWait.Add(1)
go func() {
defer p.stopExportingWait.Done()
p.runSpanExportingLoop()
}()
return p
}
// onStart adds a newly created span with a corresponding metadata struct to the span watchlist for later processing.
func (p *dtSpanProcessor) onStart(ctx context.Context, s *dtSpan) {
if p.isExportingStopped() {
return
}
// only recording span has to be sent to Dynatrace Cluster
span, ok := s.Span.(sdktrace.ReadWriteSpan)
if !ok {
return
}
p.logger.Debugf("Start span %s", span.Name())
if !p.spanWatchlist.add(s) {
p.logger.Infof("Span watchlist map is full, can not add metadata for started span: %s", span.Name())
}
}
// onEnd adds ended span in a processor map for later processing if it wasn't added upon span start call due to a
// reaching max spans limit of the processor map.
func (p *dtSpanProcessor) onEnd(s *dtSpan) {
if p.isExportingStopped() {
return
}
// only recording span has to be sent to Dynatrace Cluster
span, ok := s.Span.(sdktrace.ReadWriteSpan)
if !ok {
return
}
p.logger.Debugf("End span %s", span.Name())
if !p.spanWatchlist.contains(s) {
// most likely the span watchlist map was full on span start, so try to re-add span
if !p.spanWatchlist.add(s) {
p.logger.Infof("Span watchlist map is full, can not add metadata for ended span: %s", span.Name())
}
}
}
// shutdown stops exporting goroutine and exports all remaining spans to Dynatrace Cluster.
// It executes only once, subsequent call does nothing.
func (p *dtSpanProcessor) shutdown(ctx context.Context) error {
var err error
p.shutdownOnce.Do(func() {
p.logger.Debugf("Shutting down is called")
ctx, cancel := context.WithCancel(ctx)
defer cancel()
waitShutdown := make(chan struct{})
go func() {
p.requestStopSpanExportingLoop()
p.stopExportingWait.Wait()
err = p.sendSpansToExport(ctx, false, exportTypeForceFlush)
if err != nil {
p.logger.Warnf("Shutdown operation has failed: %s", err)
}
p.lastFlushRequestContext = nil
close(waitShutdown)
}()
// wait until shutdown is finished or the context is cancelled
select {
case <-waitShutdown:
p.logger.Debug("Shutdown operation is finished")
case <-time.After(time.Millisecond * configuration.DefaultFlushOrShutdownTimeoutMs):
p.logger.Warn("Shutdown operation timeout is reached")
cancel()
err = ctx.Err()
case <-ctx.Done():
err = ctx.Err()
p.logger.Warnf("Context of shutdown operation has been cancelled: %s", err)
}
})
return err
}
// forceFlush waits for any in-progress send operation to be finished and starts a new send operation to serve this
// flush call.
func (p *dtSpanProcessor) forceFlush(ctx context.Context) error {
if p.isExportingStopped() {
p.logger.Debug("Ignore Flush operation, exporting is stopped")
return nil
}
p.logger.Debug("Force flush is called")
ctx, cancel := context.WithCancel(ctx)
defer cancel()
flushCtx := &flushContext{
flushRequestFinished: make(chan struct{}),
ctx: ctx,
err: nil,
}
p.flushRequestLock.Lock()
select {
case p.flushRequestCh <- flushCtx:
p.lastFlushRequestContext = flushCtx
p.flushRequestLock.Unlock()
p.logger.Debug("Flush operation is scheduled")
default:
lastFlushCtx := p.lastFlushRequestContext
p.flushRequestLock.Unlock()
p.logger.Debug("Wait until the scheduled flush operation is finished")
<-lastFlushCtx.ctx.Done()
return lastFlushCtx.err
}
select {
case <-time.After(time.Millisecond * configuration.DefaultFlushOrShutdownTimeoutMs):
// the flush operation SHOULD abort any in-progress send operation,
// thus cancel flush context to inform exporting goroutine
cancel()
flushCtx.err = ctx.Err()
p.logger.Warn("Flush operation timeout is reached")
case <-flushCtx.flushRequestFinished:
p.logger.Debug("Flush operation is finished")
case <-ctx.Done():
flushCtx.err = ctx.Err()
p.logger.Warnf("Context of flush operation has been cancelled: %s", ctx.Err())
}
return flushCtx.err
}
// runSpanExportingLoop starts exporting loop to process flush and periodic send operations
func (p *dtSpanProcessor) runSpanExportingLoop() {
defer p.periodicSendOpTimer.Stop()
for {
select {
case <-p.stopExportingCh:
atomic.StoreInt32(&p.exportingStopped, 1)
p.logger.Debug("Finish exporting loop")
return
case <-p.periodicSendOpTimer.C:
p.logger.Debug("Execute periodic send operation...")
err := p.sendSpansToExport(context.Background(), true, exportTypePeriodic)
if err != nil {
p.logger.Warnf("Periodic send operation has failed: %s", err)
}
case flushCtx := <-p.flushRequestCh:
p.logger.Debug("Execute flush operation...")
// stop the periodic send operation, the timer will be reset once exporting will be completed
if !p.periodicSendOpTimer.Stop() {
<-p.periodicSendOpTimer.C
}
flushCtx.err = p.sendSpansToExport(flushCtx.ctx, true, exportTypeForceFlush)
if flushCtx.err != nil {
p.logger.Warnf("Flush send operation has failed: %s", flushCtx.err)
}
close(flushCtx.flushRequestFinished)
}
}
}
func (p *dtSpanProcessor) isExportingStopped() bool {
return atomic.LoadInt32(&p.exportingStopped) == 1
}
// requestStopSpanExportingLoop send request to stop span exporting loop
func (p *dtSpanProcessor) requestStopSpanExportingLoop() {
select {
case p.stopExportingCh <- struct{}{}:
p.logger.Debug("Stop exporting is requested")
default:
p.logger.Debug("Stop exporting has been already requested")
}
}
// sendSpansToExport collect spans that are ready to be exported and pass them to Dynatrace Span Exporter
func (p *dtSpanProcessor) sendSpansToExport(ctx context.Context, resetPeriodicSendOpTimer bool, t exportType) error {
p.logger.Debugf("Preparing spans to export. Export type: %d", t)
if resetPeriodicSendOpTimer {
// reset periodic send operation timer at the end of the send operation since
// the time the operation takes must increase the update interval
defer p.periodicSendOpTimer.Reset(time.Millisecond * time.Duration(p.config.SpanProcessingIntervalMs))
}
if p.exporter == nil {
p.logger.Debugf("Can not perform exporting operation, Span Exporter is not initialized")
return errInvalidSpanExporter
}
start := time.Now()
err := p.exporter.export(ctx, t, p.spanWatchlist.getSpansToExport())
p.logger.Debugf("Export operation took %s", time.Since(start))
if err == errNotAuthorizedRequest {
// not authorized request can be fixed only if the auth token is changed, thus stop exporting loop
p.logger.Debugf("Stop exporting loop because span export request is not authorized")
p.requestStopSpanExportingLoop()
}
return err
}