forked from open-telemetry/opentelemetry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch_span_processor_v2.go
225 lines (191 loc) · 5.12 KB
/
batch_span_processor_v2.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
// Copyright The OpenTelemetry Authors
//
// 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 "go.opentelemetry.io/otel/sdk/trace"
import (
"context"
"time"
)
const (
DefaultV2MaxQueueSize = 2048
DefaultV2BatchTimeout = 5000 * time.Millisecond
DefaultV2ExportTimeout = 30000 * time.Millisecond
DefaultV2MaxExportBatchSize = 512
)
type batchSpanProcessorV2 struct {
e SpanExporter
o BatchSpanProcessorOptions
queue chan bspV2State
stopped bool
batch []ReadOnlySpan
timer *time.Timer
}
type bspV2StateKind int
const (
bspV2Msg = iota
bspV2Flush
bspV2Close
)
type bspV2State struct {
kind bspV2StateKind
span ReadOnlySpan
ctx context.Context
sync chan error
}
var _ SpanProcessor = (*batchSpanProcessorV2)(nil)
func NewBatchSpanProcessorV2(exporter SpanExporter, options ...BatchSpanProcessorOption) SpanProcessor {
o := BatchSpanProcessorOptions{
BatchTimeout: DefaultV2BatchTimeout,
ExportTimeout: DefaultV2ExportTimeout,
MaxQueueSize: DefaultV2MaxQueueSize,
MaxExportBatchSize: DefaultV2MaxExportBatchSize,
}
for _, opt := range options {
opt(&o)
}
bsp := &batchSpanProcessorV2{
e: exporter,
o: o,
batch: make([]ReadOnlySpan, 0, o.MaxExportBatchSize),
timer: time.NewTimer(o.BatchTimeout),
queue: make(chan bspV2State, o.MaxQueueSize),
}
go bsp.processQueue()
return bsp
}
// OnStart is called when a span is started. It is called synchronously
// and should not block.
func (bsp *batchSpanProcessorV2) OnStart(parent context.Context, s ReadWriteSpan) {}
// OnEnd is called when span is finished. It is called synchronously and
// hence not block.
func (bsp *batchSpanProcessorV2) OnEnd(s ReadOnlySpan) {
if bsp.e == nil {
return
}
state := bspV2State{
kind: bspV2Msg,
span: s,
}
if bsp.o.BlockOnQueueFull {
bsp.queue <- state
return
}
select {
case bsp.queue <- state:
default:
}
}
// Shutdown is called when the SDK shuts down. Any cleanup or release of
// resources held by the processor should be done in this call.
//
// Calls to OnStart, OnEnd, or ForceFlush after this has been called
// should be ignored.
//
// All timeouts and cancellations contained in ctx must be honored, this
// should not block indefinitely.
func (bsp *batchSpanProcessorV2) Shutdown(ctx context.Context) error {
errCh := make(chan error)
bsp.queue <- bspV2State{
kind: bspV2Close,
sync: errCh,
ctx: ctx,
}
return <-errCh
}
func (bsp *batchSpanProcessorV2) AsyncShutdown(ctx context.Context) error {
bsp.queue <- bspV2State{
kind: bspV2Close,
ctx: ctx,
}
return nil
}
// ForceFlush exports all ended spans to the configured Exporter that have not yet
// been exported. It should only be called when absolutely necessary, such as when
// using a FaaS provider that may suspend the process after an invocation, but before
// the Processor can export the completed spans.
func (bsp *batchSpanProcessorV2) ForceFlush(ctx context.Context) error {
errCh := make(chan error)
bsp.queue <- bspV2State{
kind: bspV2Flush,
sync: errCh,
ctx: ctx,
}
return <-errCh
}
func (bsp *batchSpanProcessorV2) AsyncForceFlush(ctx context.Context) error {
errCh := make(chan error)
bsp.queue <- bspV2State{
kind: bspV2Flush,
sync: errCh,
ctx: ctx,
}
return <-errCh
}
func (bsp *batchSpanProcessorV2) processQueue() {
for {
select {
case <-bsp.timer.C:
bsp.exportSpans(context.Background())
case msg := <-bsp.queue:
switch msg.kind {
case bspV2Msg:
if bsp.stopped {
continue
}
bsp.batch = append(bsp.batch, msg.span)
if len(bsp.batch) >= bsp.o.MaxExportBatchSize {
bsp.exportSpans(context.Background())
}
case bspV2Flush:
err := bsp.exportSpans(msg.ctx)
if msg.sync != nil {
msg.sync <- err
close(msg.sync)
}
case bspV2Close:
err := bsp.exportSpans(msg.ctx)
if msg.sync != nil {
msg.sync <- err
close(msg.sync)
}
if bsp.stopped {
continue
}
bsp.timer.Stop()
bsp.stopped = true
if bsp.e != nil {
bsp.e.Shutdown(msg.ctx)
}
}
}
}
}
func (bsp *batchSpanProcessorV2) exportSpans(ctx context.Context) (err error) {
if bsp.stopped {
return nil
}
if l := len(bsp.batch); l > 0 {
err = bsp.e.ExportSpans(ctx, bsp.batch)
//TODO: handle error
// A new batch is always created after exporting, even if the batch failed to be exported.
//
// It is up to the exporter to implement any type of retry logic if a batch is failing
// to be exported, since it is specific to the protocol and backend being sent to.
bsp.batch = bsp.batch[:0]
}
if !bsp.timer.Stop() {
<-bsp.timer.C
}
bsp.timer.Reset(bsp.o.BatchTimeout)
return err
}