From 617b0bbdcda99ab4b2b12539f678ee41cab0bb09 Mon Sep 17 00:00:00 2001 From: Sean Porter Date: Wed, 8 Jan 2025 03:08:20 -0800 Subject: [PATCH] [processor/tailsampling] Fixed sampling policy evaluation debug logging 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 --- ...ngprocessor-fixed-batch-debug-metrics.yaml | 27 +++++++++++++ processor/tailsamplingprocessor/processor.go | 7 ++++ .../tailsamplingprocessor/processor_test.go | 40 +++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 .chloggen/tailsamplingprocessor-fixed-batch-debug-metrics.yaml diff --git a/.chloggen/tailsamplingprocessor-fixed-batch-debug-metrics.yaml b/.chloggen/tailsamplingprocessor-fixed-batch-debug-metrics.yaml new file mode 100644 index 000000000000..abf0a69ad30c --- /dev/null +++ b/.chloggen/tailsamplingprocessor-fixed-batch-debug-metrics.yaml @@ -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] diff --git a/processor/tailsamplingprocessor/processor.go b/processor/tailsamplingprocessor/processor.go index e993212e85d7..99b8a50152d7 100644 --- a/processor/tailsamplingprocessor/processor.go +++ b/processor/tailsamplingprocessor/processor.go @@ -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 } diff --git a/processor/tailsamplingprocessor/processor_test.go b/processor/tailsamplingprocessor/processor_test.go index 49e126260130..d86a1fcea948 100644 --- a/processor/tailsamplingprocessor/processor_test.go +++ b/processor/tailsamplingprocessor/processor_test.go @@ -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