Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/27567-tailsampler-fix-metrics.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: 'bug_fix'

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: processor/tailsamplingprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Publish tail_sampler count_traces_sampled metric differently per individual policy

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [27567]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: Fixes tail_sampler count_traces_sampled metric to report sampled true/false count per policy. Previously all policies received the same count based on the sampling "final decision"

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
31 changes: 18 additions & 13 deletions processor/tailsamplingprocessor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"math"
"runtime"
"strconv"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -252,18 +253,22 @@ func (tsp *tailSamplingSpanProcessor) makeDecision(id pcommon.TraceID, trace *sa
switch decision {
case sampling.Sampled:
samplingDecision[sampling.Sampled] = true
tsp.recordPolicySampledDecisionMetric(p, true)
trace.Decisions[i] = decision

case sampling.NotSampled:
samplingDecision[sampling.NotSampled] = true
tsp.recordPolicySampledDecisionMetric(p, false)
trace.Decisions[i] = decision

case sampling.InvertSampled:
samplingDecision[sampling.InvertSampled] = true
tsp.recordPolicySampledDecisionMetric(p, true)
trace.Decisions[i] = sampling.Sampled

case sampling.InvertNotSampled:
samplingDecision[sampling.InvertNotSampled] = true
tsp.recordPolicySampledDecisionMetric(p, false)
trace.Decisions[i] = sampling.NotSampled
}
}
Expand All @@ -279,28 +284,17 @@ func (tsp *tailSamplingSpanProcessor) makeDecision(id pcommon.TraceID, trace *sa
finalDecision = sampling.Sampled
}

for i, p := range tsp.policies {
switch trace.Decisions[i] {
for _, p := range tsp.policies {
switch finalDecision {
case sampling.Sampled:
// 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
if matchingPolicy == nil {
matchingPolicy = p
}

_ = stats.RecordWithTags(
p.ctx,
[]tag.Mutator{tag.Upsert(tagSampledKey, "true")},
statCountTracesSampled.M(int64(1)),
)
metrics.decisionSampled++

case sampling.NotSampled:
_ = stats.RecordWithTags(
p.ctx,
[]tag.Mutator{tag.Upsert(tagSampledKey, "false")},
statCountTracesSampled.M(int64(1)),
)
metrics.decisionNotSampled++
}
}
Expand All @@ -323,6 +317,17 @@ func (tsp *tailSamplingSpanProcessor) makeDecision(id pcommon.TraceID, trace *sa
return finalDecision, matchingPolicy
}

func (tsp *tailSamplingSpanProcessor) recordPolicySampledDecisionMetric(policy *policy, sampled bool) {
_ = stats.RecordWithTags(
policy.ctx,
[]tag.Mutator{
tag.Upsert(tagPolicyKey, policy.name),
tag.Upsert(tagSampledKey, strconv.FormatBool(sampled)),
},
statCountTracesSampled.M(int64(1)),
)
}

// ConsumeTraces is required by the processor.Traces interface.
func (tsp *tailSamplingSpanProcessor) ConsumeTraces(_ context.Context, td ptrace.Traces) error {
resourceSpans := td.ResourceSpans()
Expand Down