-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tailsampling: only send to next consumer once #1735
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fb6bf8c
tailsampling: when multiple policies choose to sample, only send to n…
chris-smith-zocdoc 0449030
move the decision making loop out of the onTick loop to make the code…
chris-smith-zocdoc 430f144
add test
chris-smith-zocdoc 3facf2a
add coverage for error case
chris-smith-zocdoc cba2248
move metric counters into a struct
chris-smith-zocdoc fd7e40c
change log to debug
chris-smith-zocdoc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,80 +138,102 @@ func getPolicyEvaluator(logger *zap.Logger, cfg *PolicyCfg) (sampling.PolicyEval | |
} | ||
} | ||
|
||
type policyMetrics struct { | ||
idNotFoundOnMapCount, evaluateErrorCount, decisionSampled, decisionNotSampled int64 | ||
} | ||
|
||
func (tsp *tailSamplingSpanProcessor) samplingPolicyOnTick() { | ||
var idNotFoundOnMapCount, evaluateErrorCount, decisionSampled, decisionNotSampled int64 | ||
metrics := policyMetrics{} | ||
|
||
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.Bytes())) | ||
if !ok { | ||
idNotFoundOnMapCount++ | ||
metrics.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 | ||
|
||
decision, policy := tsp.makeDecision(id, trace, &metrics) | ||
|
||
// Sampled or not, remove the batches | ||
trace.Lock() | ||
traceBatches := trace.ReceivedBatches | ||
trace.ReceivedBatches = nil | ||
trace.Unlock() | ||
|
||
if decision == sampling.Sampled { | ||
for j := 0; j < len(traceBatches); j++ { | ||
_ = tsp.nextConsumer.ConsumeTraces(policy.ctx, internaldata.OCToTraceData(traceBatches[j])) | ||
} | ||
} | ||
} | ||
|
||
stats.Record(tsp.ctx, | ||
statOverallDecisionLatencyµs.M(int64(time.Since(startTime)/time.Microsecond)), | ||
statDroppedTooEarlyCount.M(metrics.idNotFoundOnMapCount), | ||
statPolicyEvaluationErrorCount.M(metrics.evaluateErrorCount), | ||
statTracesOnMemoryGauge.M(int64(atomic.LoadUint64(&tsp.numTracesOnMap)))) | ||
|
||
tsp.logger.Debug("Sampling policy evaluation completed", | ||
zap.Int("batch.len", batchLen), | ||
zap.Int64("sampled", metrics.decisionSampled), | ||
zap.Int64("notSampled", metrics.decisionNotSampled), | ||
zap.Int64("droppedPriorToEvaluation", metrics.idNotFoundOnMapCount), | ||
zap.Int64("policyEvaluationErrors", metrics.evaluateErrorCount), | ||
) | ||
} | ||
|
||
func (tsp *tailSamplingSpanProcessor) makeDecision(id pdata.TraceID, trace *sampling.TraceData, metrics *policyMetrics) (sampling.Decision, *Policy) { | ||
finalDecision := sampling.NotSampled | ||
var matchingPolicy *Policy = nil | ||
|
||
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 | ||
metrics.evaluateErrorCount++ | ||
tsp.logger.Debug("Sampling policy error", zap.Error(err)) | ||
} else { | ||
trace.Decisions[i] = decision | ||
|
||
switch decision { | ||
case sampling.Sampled: | ||
stats.RecordWithTags( | ||
// any single policy that decides to sample will cause the decision to be sampled | ||
// the nextConsumer will get the context from the first matching policy | ||
finalDecision = sampling.Sampled | ||
if matchingPolicy == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only reason this is necessary is to get the matching context from the policy which contains tags for the policy name |
||
matchingPolicy = policy | ||
} | ||
|
||
_ = stats.RecordWithTags( | ||
policy.ctx, | ||
[]tag.Mutator{tag.Insert(tagSampledKey, "true")}, | ||
statCountTracesSampled.M(int64(1)), | ||
) | ||
decisionSampled++ | ||
|
||
trace.Lock() | ||
traceBatches := trace.ReceivedBatches | ||
trace.Unlock() | ||
metrics.decisionSampled++ | ||
|
||
for j := 0; j < len(traceBatches); j++ { | ||
tsp.nextConsumer.ConsumeTraces(policy.ctx, internaldata.OCToTraceData(traceBatches[j])) | ||
} | ||
case sampling.NotSampled: | ||
stats.RecordWithTags( | ||
_ = stats.RecordWithTags( | ||
policy.ctx, | ||
[]tag.Mutator{tag.Insert(tagSampledKey, "false")}, | ||
statCountTracesSampled.M(int64(1)), | ||
) | ||
decisionNotSampled++ | ||
metrics.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), | ||
) | ||
return finalDecision, matchingPolicy | ||
} | ||
|
||
// ConsumeTraceData is required by the SpanProcessor interface. | ||
|
@@ -296,8 +318,6 @@ func (tsp *tailSamplingSpanProcessor) processTraces(td consumerdata.TraceData) e | |
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) | ||
|
@@ -316,6 +336,12 @@ func (tsp *tailSamplingSpanProcessor) processTraces(td consumerdata.TraceData) e | |
zap.String("policy", policy.Name), | ||
zap.Int("decision", int(actualDecision))) | ||
} | ||
|
||
// At this point the late arrival has been passed to nextConsumer. Need to break out of the policy loop | ||
// so that it isn't sent to nextConsumer more than once when multiple policies chose to sample | ||
if actualDecision == sampling.Sampled { | ||
break | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously the lock was taken twice when a policy matched, I just combined them into a single acquisition