Skip to content

Commit 4775be2

Browse files
authored
Use service.name resource attribute instead of span name for service name matcher. (#138)
1 parent 335c773 commit 4775be2

File tree

8 files changed

+118
-158
lines changed

8 files changed

+118
-158
lines changed

aws-xray/src/main/java/io/opentelemetry/contrib/awsxray/SamplingRuleApplier.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private SamplingRuleApplier(
144144
this.nextSnapshotTimeNanos = nextSnapshotTimeNanos;
145145
}
146146

147-
boolean matches(String name, Attributes attributes, Resource resource) {
147+
boolean matches(Attributes attributes, Resource resource) {
148148
int matchedAttributes = 0;
149149
String httpTarget = null;
150150
String httpMethod = null;
@@ -175,7 +175,7 @@ boolean matches(String name, Attributes attributes, Resource resource) {
175175
}
176176

177177
return urlPathMatcher.matches(httpTarget)
178-
&& serviceNameMatcher.matches(name)
178+
&& serviceNameMatcher.matches(resource.getAttribute(ResourceAttributes.SERVICE_NAME))
179179
&& httpMethodMatcher.matches(httpMethod)
180180
&& hostMatcher.matches(host)
181181
&& serviceTypeMatcher.matches(getServiceType(resource))

aws-xray/src/main/java/io/opentelemetry/contrib/awsxray/XrayRulesSampler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public SamplingResult shouldSample(
7676
Attributes attributes,
7777
List<LinkData> parentLinks) {
7878
for (SamplingRuleApplier applier : ruleAppliers) {
79-
if (applier.matches(name, attributes, resource)) {
79+
if (applier.matches(attributes, resource)) {
8080
return applier.shouldSample(
8181
parentContext, traceId, name, spanKind, attributes, parentLinks);
8282
}

aws-xray/src/test/java/io/opentelemetry/contrib/awsxray/AwsXrayRemoteSamplerTest.java

Lines changed: 21 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
import static org.assertj.core.api.Assertions.assertThat;
1010
import static org.awaitility.Awaitility.await;
1111

12-
import com.fasterxml.jackson.databind.ObjectMapper;
1312
import com.google.common.io.ByteStreams;
1413
import com.linecorp.armeria.common.HttpResponse;
1514
import com.linecorp.armeria.common.HttpStatus;
1615
import com.linecorp.armeria.common.MediaType;
1716
import com.linecorp.armeria.server.ServerBuilder;
1817
import com.linecorp.armeria.testing.junit5.server.ServerExtension;
18+
import io.opentelemetry.api.common.AttributeKey;
1919
import io.opentelemetry.api.common.Attributes;
2020
import io.opentelemetry.api.trace.SpanKind;
2121
import io.opentelemetry.api.trace.TraceId;
@@ -35,8 +35,6 @@
3535

3636
class AwsXrayRemoteSamplerTest {
3737

38-
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
39-
4038
private static final byte[] RULE_RESPONSE_1;
4139
private static final byte[] RULE_RESPONSE_2;
4240
private static final byte[] TARGETS_RESPONE;
@@ -121,57 +119,18 @@ void tearDown() {
121119
@Test
122120
void getAndUpdate() throws Exception {
123121
// Initial Sampler allows all.
124-
assertThat(
125-
sampler
126-
.shouldSample(
127-
Context.root(),
128-
TRACE_ID,
129-
"cat-service",
130-
SpanKind.SERVER,
131-
Attributes.empty(),
132-
Collections.emptyList())
133-
.getDecision())
134-
.isEqualTo(SamplingDecision.RECORD_AND_SAMPLE);
135-
assertThat(
136-
sampler
137-
.shouldSample(
138-
Context.root(),
139-
TRACE_ID,
140-
"dog-service",
141-
SpanKind.SERVER,
142-
Attributes.empty(),
143-
Collections.emptyList())
144-
.getDecision())
145-
.isEqualTo(SamplingDecision.RECORD_AND_SAMPLE);
122+
assertThat(doSample(sampler, "cat-service")).isEqualTo(SamplingDecision.RECORD_AND_SAMPLE);
123+
assertThat(doSample(sampler, "dog-service")).isEqualTo(SamplingDecision.RECORD_AND_SAMPLE);
146124

147125
rulesResponse.set(RULE_RESPONSE_1);
148126

149127
// cat-service allowed, others dropped
150128
await()
151129
.untilAsserted(
152130
() -> {
153-
assertThat(
154-
sampler
155-
.shouldSample(
156-
Context.root(),
157-
TRACE_ID,
158-
"cat-service",
159-
SpanKind.SERVER,
160-
Attributes.empty(),
161-
Collections.emptyList())
162-
.getDecision())
131+
assertThat(doSample(sampler, "cat-service"))
163132
.isEqualTo(SamplingDecision.RECORD_AND_SAMPLE);
164-
assertThat(
165-
sampler
166-
.shouldSample(
167-
Context.root(),
168-
TRACE_ID,
169-
"dog-service",
170-
SpanKind.SERVER,
171-
Attributes.empty(),
172-
Collections.emptyList())
173-
.getDecision())
174-
.isEqualTo(SamplingDecision.DROP);
133+
assertThat(doSample(sampler, "dog-service")).isEqualTo(SamplingDecision.DROP);
175134
});
176135

177136
rulesResponse.set(RULE_RESPONSE_2);
@@ -180,27 +139,8 @@ void getAndUpdate() throws Exception {
180139
await()
181140
.untilAsserted(
182141
() -> {
183-
assertThat(
184-
sampler
185-
.shouldSample(
186-
Context.root(),
187-
TRACE_ID,
188-
"cat-service",
189-
SpanKind.SERVER,
190-
Attributes.empty(),
191-
Collections.emptyList())
192-
.getDecision())
193-
.isEqualTo(SamplingDecision.DROP);
194-
assertThat(
195-
sampler
196-
.shouldSample(
197-
Context.root(),
198-
TRACE_ID,
199-
"dog-service",
200-
SpanKind.SERVER,
201-
Attributes.empty(),
202-
Collections.emptyList())
203-
.getDecision())
142+
assertThat(doSample(sampler, "cat-service")).isEqualTo(SamplingDecision.DROP);
143+
assertThat(doSample(sampler, "dog-service"))
204144
.isEqualTo(SamplingDecision.RECORD_AND_SAMPLE);
205145
});
206146

@@ -210,27 +150,9 @@ void getAndUpdate() throws Exception {
210150
await()
211151
.untilAsserted(
212152
() -> {
213-
assertThat(
214-
sampler
215-
.shouldSample(
216-
Context.root(),
217-
TRACE_ID,
218-
"cat-service",
219-
SpanKind.SERVER,
220-
Attributes.empty(),
221-
Collections.emptyList())
222-
.getDecision())
153+
assertThat(doSample(sampler, "cat-service"))
223154
.isEqualTo(SamplingDecision.RECORD_AND_SAMPLE);
224-
assertThat(
225-
sampler
226-
.shouldSample(
227-
Context.root(),
228-
TRACE_ID,
229-
"dog-service",
230-
SpanKind.SERVER,
231-
Attributes.empty(),
232-
Collections.emptyList())
233-
.getDecision())
155+
assertThat(doSample(sampler, "dog-service"))
234156
.isEqualTo(SamplingDecision.RECORD_AND_SAMPLE);
235157
});
236158
}
@@ -245,4 +167,16 @@ void defaultInitialSampler() {
245167
+ "first:RateLimitingSampler{1}, second:TraceIdRatioBased{0.050000}");
246168
}
247169
}
170+
171+
private static SamplingDecision doSample(Sampler sampler, String name) {
172+
return sampler
173+
.shouldSample(
174+
Context.root(),
175+
TRACE_ID,
176+
"span",
177+
SpanKind.SERVER,
178+
Attributes.of(AttributeKey.stringKey("test"), name),
179+
Collections.emptyList())
180+
.getDecision();
181+
}
248182
}

0 commit comments

Comments
 (0)