Skip to content

Commit

Permalink
[processor/tailsampling] Fixed sampling policy evaluation debug loggi…
Browse files Browse the repository at this point in the history
…ng batch metrics (#37040)

#### Description

Currently, the processor always logs (at debug) `sampled=0` and
`notSampled=0` for every batch processed. This pull-request fixes those
metrics.

---------

Signed-off-by: Sean Porter <[email protected]>
  • Loading branch information
portertech authored Jan 8, 2025
1 parent d3f65e7 commit 617b0bb
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .chloggen/tailsamplingprocessor-fixed-batch-debug-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: tailsamplingprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fixed sampling policy evaluation debug logging batch metrics (e.g. sampled).

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

# (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:

# 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]
7 changes: 7 additions & 0 deletions processor/tailsamplingprocessor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,13 @@ func (tsp *tailSamplingSpanProcessor) makeDecision(id pcommon.TraceID, trace *sa
finalDecision = sampling.Sampled
}

switch finalDecision {
case sampling.Sampled:
metrics.decisionSampled++
case sampling.NotSampled:
metrics.decisionNotSampled++
}

return finalDecision
}

Expand Down
40 changes: 40 additions & 0 deletions processor/tailsamplingprocessor/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,46 @@ func TestDuplicatePolicyName(t *testing.T) {
assert.Equal(t, err, errors.New(`duplicate policy name "always_sample"`))
}

func TestDecisionPolicyMetrics(t *testing.T) {
traceIDs, batches := generateIDsAndBatches(10)
policy := []PolicyCfg{
{
sharedPolicyCfg: sharedPolicyCfg{
Name: "test-policy",
Type: Probabilistic,
ProbabilisticCfg: ProbabilisticCfg{SamplingPercentage: 50},
},
},
}
cfg := Config{
DecisionWait: defaultTestDecisionWait,
NumTraces: uint64(2 * len(traceIDs)),
ExpectedNewTracesPerSec: 64,
PolicyCfgs: policy,
}
sp, _ := newTracesProcessor(context.Background(), processortest.NewNopSettings(), consumertest.NewNop(), cfg)
tsp := sp.(*tailSamplingSpanProcessor)
require.NoError(t, tsp.Start(context.Background(), componenttest.NewNopHost()))
defer func() {
require.NoError(t, tsp.Shutdown(context.Background()))
}()
metrics := &policyMetrics{}

for i, id := range traceIDs {
sb := &sampling.TraceData{
ArrivalTime: time.Now(),
ReceivedBatches: batches[i],
}

_ = tsp.makeDecision(id, sb, metrics)
}

assert.EqualValues(t, 5, metrics.decisionSampled)
assert.EqualValues(t, 5, metrics.decisionNotSampled)
assert.EqualValues(t, 0, metrics.idNotFoundOnMapCount)
assert.EqualValues(t, 0, metrics.evaluateErrorCount)
}

func collectSpanIDs(trace ptrace.Traces) []pcommon.SpanID {
var spanIDs []pcommon.SpanID

Expand Down

0 comments on commit 617b0bb

Please sign in to comment.