Skip to content

Commit

Permalink
Support thread.name attributes in RuleBasedRoutingSampler (open-tel…
Browse files Browse the repository at this point in the history
…emetry#1030)

Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com>
  • Loading branch information
heyams and trask committed Sep 18, 2023
1 parent 0f2112f commit 0a61845
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 0a61845

Please sign in to comment.