/
processor.go
398 lines (358 loc) · 12.3 KB
/
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Copyright 2019, 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 tailsamplingprocessor
import (
"context"
"fmt"
"runtime"
"sync"
"sync/atomic"
"time"
tracepb "github.com/census-instrumentation/opencensus-proto/gen-go/trace/v1"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector/component"
"github.com/open-telemetry/opentelemetry-collector/consumer"
"github.com/open-telemetry/opentelemetry-collector/consumer/consumerdata"
"github.com/open-telemetry/opentelemetry-collector/oterr"
"github.com/open-telemetry/opentelemetry-collector/processor/samplingprocessor/tailsamplingprocessor/idbatcher"
"github.com/open-telemetry/opentelemetry-collector/processor/samplingprocessor/tailsamplingprocessor/sampling"
)
// Policy combines a sampling policy evaluator with the destinations to be
// used for that policy.
type Policy struct {
// Name used to identify this policy instance.
Name string
// Evaluator that decides if a trace is sampled or not by this policy instance.
Evaluator sampling.PolicyEvaluator
// ctx used to carry metric tags of each policy.
ctx context.Context
}
// traceKey is defined since sync.Map requires a comparable type, isolating it on its own
// type to help track usage.
type traceKey string
// tailSamplingSpanProcessor handles the incoming trace data and uses the given sampling
// policy to sample traces.
type tailSamplingSpanProcessor struct {
ctx context.Context
nextConsumer consumer.TraceConsumerOld
start sync.Once
maxNumTraces uint64
policies []*Policy
logger *zap.Logger
idToTrace sync.Map
policyTicker tTicker
decisionBatcher idbatcher.Batcher
deleteChan chan traceKey
numTracesOnMap uint64
}
const (
sourceFormat = "tail_sampling"
)
// NewTraceProcessor returns a processor.TraceProcessor that will perform tail sampling according to the given
// configuration.
func NewTraceProcessor(logger *zap.Logger, nextConsumer consumer.TraceConsumerOld, cfg Config) (component.TraceProcessorOld, error) {
if nextConsumer == nil {
return nil, oterr.ErrNilNextConsumer
}
numDecisionBatches := uint64(cfg.DecisionWait.Seconds())
inBatcher, err := idbatcher.New(numDecisionBatches, cfg.ExpectedNewTracesPerSec, uint64(2*runtime.NumCPU()))
if err != nil {
return nil, err
}
ctx := context.Background()
policies := []*Policy{}
for i := range cfg.PolicyCfgs {
policyCfg := &cfg.PolicyCfgs[i]
policyCtx, err := tag.New(ctx, tag.Upsert(tagPolicyKey, policyCfg.Name), tag.Upsert(tagSourceFormat, sourceFormat))
if err != nil {
return nil, err
}
eval, err := getPolicyEvaluator(logger, policyCfg)
if err != nil {
return nil, err
}
policy := &Policy{
Name: policyCfg.Name,
Evaluator: eval,
ctx: policyCtx,
}
policies = append(policies, policy)
}
tsp := &tailSamplingSpanProcessor{
ctx: ctx,
nextConsumer: nextConsumer,
maxNumTraces: cfg.NumTraces,
logger: logger,
decisionBatcher: inBatcher,
policies: policies,
}
tsp.policyTicker = &policyTicker{onTick: tsp.samplingPolicyOnTick}
tsp.deleteChan = make(chan traceKey, cfg.NumTraces)
return tsp, nil
}
func getPolicyEvaluator(logger *zap.Logger, cfg *PolicyCfg) (sampling.PolicyEvaluator, error) {
switch cfg.Type {
case AlwaysSample:
return sampling.NewAlwaysSample(logger), nil
case NumericAttribute:
nafCfg := cfg.NumericAttributeCfg
return sampling.NewNumericAttributeFilter(logger, nafCfg.Key, nafCfg.MinValue, nafCfg.MaxValue), nil
case StringAttribute:
safCfg := cfg.StringAttributeCfg
return sampling.NewStringAttributeFilter(logger, safCfg.Key, safCfg.Values), nil
case RateLimiting:
rlfCfg := cfg.RateLimitingCfg
return sampling.NewRateLimiting(logger, rlfCfg.SpansPerSecond), nil
default:
return nil, fmt.Errorf("unknown sampling policy type %s", cfg.Type)
}
}
func (tsp *tailSamplingSpanProcessor) samplingPolicyOnTick() {
var idNotFoundOnMapCount, evaluateErrorCount, decisionSampled, decisionNotSampled int64
startTime := time.Now()
batch, _ := tsp.decisionBatcher.CloseCurrentAndTakeFirstBatch()
batchLen := len(batch)
tsp.logger.Debug("Sampling Policy Evaluation ticked")
for _, id := range batch {
d, ok := tsp.idToTrace.Load(traceKey(id))
if !ok {
idNotFoundOnMapCount++
continue
}
trace := d.(*sampling.TraceData)
trace.DecisionTime = time.Now()
for i, policy := range tsp.policies {
policyEvaluateStartTime := time.Now()
decision, err := policy.Evaluator.Evaluate(id, trace)
stats.Record(
policy.ctx,
statDecisionLatencyMicroSec.M(int64(time.Since(policyEvaluateStartTime)/time.Microsecond)))
if err != nil {
trace.Decisions[i] = sampling.NotSampled
evaluateErrorCount++
tsp.logger.Error("Sampling policy error", zap.Error(err))
continue
}
trace.Decisions[i] = decision
switch decision {
case sampling.Sampled:
stats.RecordWithTags(
policy.ctx,
[]tag.Mutator{tag.Insert(tagSampledKey, "true")},
statCountTracesSampled.M(int64(1)),
)
decisionSampled++
trace.Lock()
traceBatches := trace.ReceivedBatches
trace.Unlock()
for j := 0; j < len(traceBatches); j++ {
tsp.nextConsumer.ConsumeTraceData(policy.ctx, traceBatches[j])
}
case sampling.NotSampled:
stats.RecordWithTags(
policy.ctx,
[]tag.Mutator{tag.Insert(tagSampledKey, "false")},
statCountTracesSampled.M(int64(1)),
)
decisionNotSampled++
}
}
// Sampled or not, remove the batches
trace.Lock()
trace.ReceivedBatches = nil
trace.Unlock()
}
stats.Record(tsp.ctx,
statOverallDecisionLatencyµs.M(int64(time.Since(startTime)/time.Microsecond)),
statDroppedTooEarlyCount.M(idNotFoundOnMapCount),
statPolicyEvaluationErrorCount.M(evaluateErrorCount),
statTracesOnMemoryGauge.M(int64(atomic.LoadUint64(&tsp.numTracesOnMap))))
tsp.logger.Debug("Sampling policy evaluation completed",
zap.Int("batch.len", batchLen),
zap.Int64("sampled", decisionSampled),
zap.Int64("notSampled", decisionNotSampled),
zap.Int64("droppedPriorToEvaluation", idNotFoundOnMapCount),
zap.Int64("policyEvaluationErrors", evaluateErrorCount),
)
}
// ConsumeTraceData is required by the SpanProcessor interface.
func (tsp *tailSamplingSpanProcessor) ConsumeTraceData(ctx context.Context, td consumerdata.TraceData) error {
tsp.start.Do(func() {
tsp.logger.Info("First trace data arrived, starting tail_sampling timers")
tsp.policyTicker.Start(1 * time.Second)
})
// Groupd spans per their traceId to minimize contention on idToTrace
idToSpans := make(map[traceKey][]*tracepb.Span)
for _, span := range td.Spans {
if len(span.TraceId) != 16 {
tsp.logger.Warn("Span without valid TraceId", zap.String("SourceFormat", td.SourceFormat))
continue
}
tk := traceKey(span.TraceId)
idToSpans[tk] = append(idToSpans[tk], span)
}
var newTraceIDs int64
singleTrace := len(idToSpans) == 1
for id, spans := range idToSpans {
lenSpans := int64(len(spans))
lenPolicies := len(tsp.policies)
initialDecisions := make([]sampling.Decision, lenPolicies)
for i := 0; i < lenPolicies; i++ {
initialDecisions[i] = sampling.Pending
}
initialTraceData := &sampling.TraceData{
Decisions: initialDecisions,
ArrivalTime: time.Now(),
SpanCount: lenSpans,
}
d, loaded := tsp.idToTrace.LoadOrStore(traceKey(id), initialTraceData)
actualData := d.(*sampling.TraceData)
if loaded {
atomic.AddInt64(&actualData.SpanCount, lenSpans)
} else {
newTraceIDs++
tsp.decisionBatcher.AddToCurrentBatch([]byte(id))
atomic.AddUint64(&tsp.numTracesOnMap, 1)
postDeletion := false
currTime := time.Now()
for !postDeletion {
select {
case tsp.deleteChan <- id:
postDeletion = true
default:
traceKeyToDrop := <-tsp.deleteChan
tsp.dropTrace(traceKeyToDrop, currTime)
}
}
}
for i, policy := range tsp.policies {
actualData.Lock()
actualDecision := actualData.Decisions[i]
// If decision is pending, we want to add the new spans still under the lock, so the decision doesn't happen
// in between the transition from pending.
if actualDecision == sampling.Pending {
// Add the spans to the trace, but only once for all policy, otherwise same spans will
// be duplicated in the final trace.
traceTd := prepareTraceBatch(spans, singleTrace, td)
actualData.ReceivedBatches = append(actualData.ReceivedBatches, traceTd)
actualData.Unlock()
break
}
actualData.Unlock()
switch actualDecision {
case sampling.Pending:
// All process for pending done above, keep the case so it doesn't go to default.
case sampling.Sampled:
// Forward the spans to the policy destinations
traceTd := prepareTraceBatch(spans, singleTrace, td)
if err := tsp.nextConsumer.ConsumeTraceData(policy.ctx, traceTd); err != nil {
tsp.logger.Warn("Error sending late arrived spans to destination",
zap.String("policy", policy.Name),
zap.Error(err))
}
fallthrough // so OnLateArrivingSpans is also called for decision Sampled.
case sampling.NotSampled:
policy.Evaluator.OnLateArrivingSpans(actualDecision, spans)
stats.Record(tsp.ctx, statLateSpanArrivalAfterDecision.M(int64(time.Since(actualData.DecisionTime)/time.Second)))
default:
tsp.logger.Warn("Encountered unexpected sampling decision",
zap.String("policy", policy.Name),
zap.Int("decision", int(actualDecision)))
}
}
}
stats.Record(tsp.ctx, statNewTraceIDReceivedCount.M(newTraceIDs))
return nil
}
func (tsp *tailSamplingSpanProcessor) GetCapabilities() component.ProcessorCapabilities {
return component.ProcessorCapabilities{MutatesConsumedData: false}
}
// Start is invoked during service startup.
func (tsp *tailSamplingSpanProcessor) Start(host component.Host) error {
return nil
}
// Shutdown is invoked during service shutdown.
func (tsp *tailSamplingSpanProcessor) Shutdown() error {
return nil
}
func (tsp *tailSamplingSpanProcessor) dropTrace(traceID traceKey, deletionTime time.Time) {
var trace *sampling.TraceData
if d, ok := tsp.idToTrace.Load(traceID); ok {
trace = d.(*sampling.TraceData)
tsp.idToTrace.Delete(traceID)
// Subtract one from numTracesOnMap per https://godoc.org/sync/atomic#AddUint64
atomic.AddUint64(&tsp.numTracesOnMap, ^uint64(0))
}
if trace == nil {
tsp.logger.Error("Attempt to delete traceID not on table")
return
}
policiesLen := len(tsp.policies)
stats.Record(tsp.ctx, statTraceRemovalAgeSec.M(int64(deletionTime.Sub(trace.ArrivalTime)/time.Second)))
for j := 0; j < policiesLen; j++ {
if trace.Decisions[j] == sampling.Pending {
policy := tsp.policies[j]
if decision, err := policy.Evaluator.OnDroppedSpans([]byte(traceID), trace); err != nil {
tsp.logger.Warn("OnDroppedSpans",
zap.String("policy", policy.Name),
zap.Int("decision", int(decision)),
zap.Error(err))
}
}
}
}
func prepareTraceBatch(spans []*tracepb.Span, singleTrace bool, td consumerdata.TraceData) consumerdata.TraceData {
var traceTd consumerdata.TraceData
if singleTrace {
// Special case no need to prepare a batch
traceTd = td
} else {
traceTd = consumerdata.TraceData{
Node: td.Node,
Resource: td.Resource,
Spans: spans,
}
}
return traceTd
}
// tTicker interface allows easier testing of ticker related functionality used by tailSamplingProcessor
type tTicker interface {
// Start sets the frequency of the ticker and starts the periodic calls to OnTick.
Start(d time.Duration)
// OnTick is called when the ticker fires.
OnTick()
// Stops firing the ticker.
Stop()
}
type policyTicker struct {
ticker *time.Ticker
onTick func()
}
func (pt *policyTicker) Start(d time.Duration) {
pt.ticker = time.NewTicker(d)
go func() {
for range pt.ticker.C {
pt.OnTick()
}
}()
}
func (pt *policyTicker) OnTick() {
pt.onTick()
}
func (pt *policyTicker) Stop() {
pt.ticker.Stop()
}
var _ tTicker = (*policyTicker)(nil)