Skip to content

Commit 9235244

Browse files
authored
Enable sampling override using thread.id and thread.name (#3285)
1 parent 265b23c commit 9235244

File tree

6 files changed

+150
-6
lines changed

6 files changed

+150
-6
lines changed

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/AiOverrideSampler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public SamplingResult shouldSample(
3838
SpanKind spanKind,
3939
Attributes attributes,
4040
List<LinkData> parentLinks) {
41-
4241
SpanContext parentSpanContext = Span.fromContext(parentContext).getSpanContext();
4342
boolean isRequest = RequestChecker.isRequest(spanKind, parentSpanContext, attributes::get);
4443

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/AiSampler.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public SamplingResult shouldSample(
5656
SpanKind spanKind,
5757
Attributes attributes,
5858
List<LinkData> parentLinks) {
59-
6059
if (localParentBased) {
6160
SamplingResult samplingResult = useLocalParentDecisionIfPossible(parentContext);
6261
if (samplingResult != null) {
@@ -71,7 +70,6 @@ public SamplingResult shouldSample(
7170
} else {
7271
SpanContext parentSpanContext = Span.fromContext(parentContext).getSpanContext();
7372
boolean isRequest = RequestChecker.isRequest(spanKind, parentSpanContext, attributes::get);
74-
7573
sp =
7674
isRequest
7775
? requestSamplingPercentage.get()

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/SamplingOverrides.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ private boolean matches(Attributes attributes, @Nullable LazyHttpUrl lazyHttpUrl
8383
return true;
8484
}
8585

86+
static String getValueIncludingThreadName(
87+
Attributes attributes, AttributeKey<String> attributeKey) {
88+
if (attributeKey.getKey().equals(SemanticAttributes.THREAD_NAME.getKey())) {
89+
return Thread.currentThread().getName();
90+
} else {
91+
return attributes.get(attributeKey);
92+
}
93+
}
94+
8695
private static TempPredicate toPredicate(SamplingOverrideAttribute attribute) {
8796
if (attribute.matchType == MatchType.STRICT) {
8897
if (isHttpHeaderAttribute(attribute)) {
@@ -121,7 +130,7 @@ private StrictMatcher(String key, String value) {
121130

122131
@Override
123132
public boolean test(Attributes attributes, LazyHttpUrl lazyHttpUrl) {
124-
String val = attributes.get(key);
133+
String val = MatcherGroup.getValueIncludingThreadName(attributes, key);
125134
if (val == null && key.getKey().equals(SemanticAttributes.HTTP_URL.getKey())) {
126135
val = lazyHttpUrl.get();
127136
}
@@ -156,7 +165,7 @@ private RegexpMatcher(String key, String value) {
156165

157166
@Override
158167
public boolean test(Attributes attributes, @Nullable LazyHttpUrl lazyHttpUrl) {
159-
String val = attributes.get(key);
168+
String val = MatcherGroup.getValueIncludingThreadName(attributes, key);
160169
if (val == null
161170
&& key.getKey().equals(SemanticAttributes.HTTP_URL.getKey())
162171
&& lazyHttpUrl != null) {
@@ -199,7 +208,7 @@ private KeyOnlyMatcher(String key) {
199208

200209
@Override
201210
public boolean test(Attributes attributes, @Nullable LazyHttpUrl lazyHttpUrl) {
202-
String val = attributes.get(key);
211+
String val = MatcherGroup.getValueIncludingThreadName(attributes, key);
203212
if (val == null
204213
&& key.getKey().equals(SemanticAttributes.HTTP_URL.getKey())
205214
&& lazyHttpUrl != null) {

smoke-tests/apps/SamplingOverrides/src/main/java/com/microsoft/applicationinsights/smoketestapp/SamplingOverridesServlet.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ private int doGetInternal(HttpServletRequest req) throws Exception {
6868
executeStatement(connection);
6969
connection.close();
7070
return 200;
71+
} else if (pathInfo.equals("/thread-name")) {
72+
Connection connection = getHsqldbConnection();
73+
executeStatement(connection);
74+
connection.close();
75+
return 200;
7176
} else {
7277
throw new ServletException("Unexpected url: " + pathInfo);
7378
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.applicationinsights.smoketest;
5+
6+
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_11;
7+
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_11_OPENJ9;
8+
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_17;
9+
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_19;
10+
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_20;
11+
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_8;
12+
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_8_OPENJ9;
13+
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.WILDFLY_13_JAVA_8;
14+
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.WILDFLY_13_JAVA_8_OPENJ9;
15+
import static org.assertj.core.api.Assertions.assertThat;
16+
17+
import org.junit.jupiter.api.Test;
18+
import org.junit.jupiter.api.extension.RegisterExtension;
19+
20+
@UseAgent("applicationinsights-thread-name.json")
21+
abstract class SamplingOverridesThreadName {
22+
23+
@RegisterExtension static final SmokeTestExtension testing = SmokeTestExtension.create();
24+
25+
@Test
26+
@TargetUri(value = "/thread-name", callCount = 100)
27+
void testSampling() throws Exception {
28+
assertThat(testing.mockedIngestion.getCountForType("RequestData")).isZero();
29+
assertThat(testing.mockedIngestion.getCountForType("MessageData")).isZero();
30+
}
31+
32+
@Environment(TOMCAT_8_JAVA_8)
33+
static class Tomcat8Java8Test extends SamplingOverridesThreadName {}
34+
35+
@Environment(TOMCAT_8_JAVA_8_OPENJ9)
36+
static class Tomcat8Java8OpenJ9Test extends SamplingOverridesThreadName {}
37+
38+
@Environment(TOMCAT_8_JAVA_11)
39+
static class Tomcat8Java11Test extends SamplingOverridesThreadName {}
40+
41+
@Environment(TOMCAT_8_JAVA_11_OPENJ9)
42+
static class Tomcat8Java11OpenJ9Test extends SamplingOverridesThreadName {}
43+
44+
@Environment(TOMCAT_8_JAVA_17)
45+
static class Tomcat8Java17Test extends SamplingOverridesThreadName {}
46+
47+
@Environment(TOMCAT_8_JAVA_19)
48+
static class Tomcat8Java19Test extends SamplingOverridesThreadName {}
49+
50+
@Environment(TOMCAT_8_JAVA_20)
51+
static class Tomcat8Java20Test extends SamplingOverridesThreadName {}
52+
53+
@Environment(WILDFLY_13_JAVA_8)
54+
static class Wildfly13Java8Test extends SamplingOverridesThreadName {}
55+
56+
@Environment(WILDFLY_13_JAVA_8_OPENJ9)
57+
static class Wildfly13Java8OpenJ9Test extends SamplingOverridesThreadName {}
58+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
"connectionString": "InstrumentationKey=12345678-0000-0000-0000-0FEEDDADBEEF;IngestionEndpoint=http://host.testcontainers.internal:6060/",
3+
"role": {
4+
"name": "testrolename",
5+
"instance": "testroleinstance"
6+
},
7+
"sampling": {
8+
"percentage": 50
9+
},
10+
"preview": {
11+
"sampling": {
12+
"overrides": [
13+
{
14+
"telemetryType": "request",
15+
"attributes": [
16+
{
17+
"key": "thread.name",
18+
"value": "http-nio-.*",
19+
"matchType": "regexp"
20+
},
21+
{
22+
"key": "http.target",
23+
"value": "/SamplingOverrides/health-check",
24+
"matchType": "strict"
25+
}
26+
],
27+
"percentage": 100
28+
},
29+
{
30+
"telemetryType": "request",
31+
"attributes": [
32+
{
33+
"key": "thread.name",
34+
"value": "http-nio-.*",
35+
"matchType": "regexp"
36+
},
37+
{
38+
"key": "http.target",
39+
"value": "/SamplingOverrides/",
40+
"matchType": "strict"
41+
}
42+
],
43+
"percentage": 100
44+
},
45+
{
46+
"telemetryType": "request",
47+
"attributes": [
48+
{
49+
"key": "thread.name",
50+
"value": "http-nio-.*",
51+
"matchType": "regexp"
52+
},
53+
{
54+
"key": "http.target",
55+
"value": "/SamplingOverrides/thread-name",
56+
"matchType": "strict"
57+
}
58+
],
59+
"percentage": 0
60+
},
61+
{
62+
"telemetryType": "trace",
63+
"attributes": [
64+
{
65+
"key": "thread.name",
66+
"value": "http-nio-.*",
67+
"matchType": "regexp"
68+
}
69+
],
70+
"percentage": 0
71+
}
72+
]
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)