From 0a6184590ff30997b14d72a5331829d792717851 Mon Sep 17 00:00:00 2001 From: Helen <56097766+heyams@users.noreply.github.com> Date: Mon, 18 Sep 2023 13:44:25 -0700 Subject: [PATCH] Support `thread.name` attributes in RuleBasedRoutingSampler (#1030) Co-authored-by: Trask Stalnaker --- .../contrib/sampler/RuleBasedRoutingSampler.java | 8 +++++++- .../contrib/sampler/RuleBasedRoutingSamplerTest.java | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/samplers/src/main/java/io/opentelemetry/contrib/sampler/RuleBasedRoutingSampler.java b/samplers/src/main/java/io/opentelemetry/contrib/sampler/RuleBasedRoutingSampler.java index 19f748353..e29ffd3ba 100644 --- a/samplers/src/main/java/io/opentelemetry/contrib/sampler/RuleBasedRoutingSampler.java +++ b/samplers/src/main/java/io/opentelemetry/contrib/sampler/RuleBasedRoutingSampler.java @@ -5,6 +5,7 @@ package io.opentelemetry.contrib.sampler; +import static io.opentelemetry.semconv.SemanticAttributes.THREAD_NAME; import static java.util.Objects.requireNonNull; import io.opentelemetry.api.common.Attributes; @@ -60,7 +61,12 @@ public SamplingResult shouldSample( return fallback.shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks); } for (SamplingRule samplingRule : rules) { - String attributeValue = attributes.get(samplingRule.attributeKey); + String attributeValue; + if (samplingRule.attributeKey.getKey().equals(THREAD_NAME.getKey())) { + attributeValue = Thread.currentThread().getName(); + } else { + attributeValue = attributes.get(samplingRule.attributeKey); + } if (attributeValue == null) { continue; } diff --git a/samplers/src/test/java/io/opentelemetry/contrib/sampler/RuleBasedRoutingSamplerTest.java b/samplers/src/test/java/io/opentelemetry/contrib/sampler/RuleBasedRoutingSamplerTest.java index 6027c6415..30cf25871 100644 --- a/samplers/src/test/java/io/opentelemetry/contrib/sampler/RuleBasedRoutingSamplerTest.java +++ b/samplers/src/test/java/io/opentelemetry/contrib/sampler/RuleBasedRoutingSamplerTest.java @@ -5,6 +5,7 @@ package io.opentelemetry.contrib.sampler; +import static io.opentelemetry.semconv.SemanticAttributes.THREAD_NAME; import static io.opentelemetry.semconv.SemanticAttributes.URL_FULL; import static io.opentelemetry.semconv.SemanticAttributes.URL_PATH; import static java.util.Collections.emptyList; @@ -187,6 +188,16 @@ void customSampler() { .isEqualTo(SamplingDecision.RECORD_AND_SAMPLE); } + @Test + void testThreadNameSampler() { + patterns.add(new SamplingRule(THREAD_NAME, "Test.*", Sampler.alwaysOff())); + Attributes attributes = Attributes.of(THREAD_NAME, "Test worker"); + RuleBasedRoutingSampler sampler = new RuleBasedRoutingSampler(patterns, SPAN_KIND, delegate); + SamplingResult samplingResult = + sampler.shouldSample(parentContext, traceId, SPAN_NAME, SPAN_KIND, attributes, emptyList()); + assertThat(samplingResult.getDecision()).isEqualTo(SamplingDecision.DROP); + } + private SamplingResult shouldSample(Sampler sampler, String url) { Attributes attributes = Attributes.of(URL_FULL, url); return sampler.shouldSample(