diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/AiOverrideSampler.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/AiOverrideSampler.java index 2b91fe06717..977f489490a 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/AiOverrideSampler.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/AiOverrideSampler.java @@ -38,7 +38,6 @@ public SamplingResult shouldSample( SpanKind spanKind, Attributes attributes, List parentLinks) { - SpanContext parentSpanContext = Span.fromContext(parentContext).getSpanContext(); boolean isRequest = RequestChecker.isRequest(spanKind, parentSpanContext, attributes::get); diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/AiSampler.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/AiSampler.java index 1fef698d34f..221ef36b548 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/AiSampler.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/AiSampler.java @@ -56,7 +56,6 @@ public SamplingResult shouldSample( SpanKind spanKind, Attributes attributes, List parentLinks) { - if (localParentBased) { SamplingResult samplingResult = useLocalParentDecisionIfPossible(parentContext); if (samplingResult != null) { @@ -71,7 +70,6 @@ public SamplingResult shouldSample( } else { SpanContext parentSpanContext = Span.fromContext(parentContext).getSpanContext(); boolean isRequest = RequestChecker.isRequest(spanKind, parentSpanContext, attributes::get); - sp = isRequest ? requestSamplingPercentage.get() diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/SamplingOverrides.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/SamplingOverrides.java index 6b1280ab1bd..6bf1bc568f7 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/SamplingOverrides.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/SamplingOverrides.java @@ -83,6 +83,15 @@ private boolean matches(Attributes attributes, @Nullable LazyHttpUrl lazyHttpUrl return true; } + static String getValueIncludingThreadName( + Attributes attributes, AttributeKey attributeKey) { + if (attributeKey.getKey().equals(SemanticAttributes.THREAD_NAME.getKey())) { + return Thread.currentThread().getName(); + } else { + return attributes.get(attributeKey); + } + } + private static TempPredicate toPredicate(SamplingOverrideAttribute attribute) { if (attribute.matchType == MatchType.STRICT) { if (isHttpHeaderAttribute(attribute)) { @@ -121,7 +130,7 @@ private StrictMatcher(String key, String value) { @Override public boolean test(Attributes attributes, LazyHttpUrl lazyHttpUrl) { - String val = attributes.get(key); + String val = MatcherGroup.getValueIncludingThreadName(attributes, key); if (val == null && key.getKey().equals(SemanticAttributes.HTTP_URL.getKey())) { val = lazyHttpUrl.get(); } @@ -156,7 +165,7 @@ private RegexpMatcher(String key, String value) { @Override public boolean test(Attributes attributes, @Nullable LazyHttpUrl lazyHttpUrl) { - String val = attributes.get(key); + String val = MatcherGroup.getValueIncludingThreadName(attributes, key); if (val == null && key.getKey().equals(SemanticAttributes.HTTP_URL.getKey()) && lazyHttpUrl != null) { @@ -199,7 +208,7 @@ private KeyOnlyMatcher(String key) { @Override public boolean test(Attributes attributes, @Nullable LazyHttpUrl lazyHttpUrl) { - String val = attributes.get(key); + String val = MatcherGroup.getValueIncludingThreadName(attributes, key); if (val == null && key.getKey().equals(SemanticAttributes.HTTP_URL.getKey()) && lazyHttpUrl != null) { diff --git a/smoke-tests/apps/SamplingOverrides/src/main/java/com/microsoft/applicationinsights/smoketestapp/SamplingOverridesServlet.java b/smoke-tests/apps/SamplingOverrides/src/main/java/com/microsoft/applicationinsights/smoketestapp/SamplingOverridesServlet.java index 7a4742bf228..41b03ff496d 100644 --- a/smoke-tests/apps/SamplingOverrides/src/main/java/com/microsoft/applicationinsights/smoketestapp/SamplingOverridesServlet.java +++ b/smoke-tests/apps/SamplingOverrides/src/main/java/com/microsoft/applicationinsights/smoketestapp/SamplingOverridesServlet.java @@ -68,6 +68,11 @@ private int doGetInternal(HttpServletRequest req) throws Exception { executeStatement(connection); connection.close(); return 200; + } else if (pathInfo.equals("/thread-name")) { + Connection connection = getHsqldbConnection(); + executeStatement(connection); + connection.close(); + return 200; } else { throw new ServletException("Unexpected url: " + pathInfo); } diff --git a/smoke-tests/apps/SamplingOverrides/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SamplingOverridesThreadName.java b/smoke-tests/apps/SamplingOverrides/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SamplingOverridesThreadName.java new file mode 100644 index 00000000000..47c42761a89 --- /dev/null +++ b/smoke-tests/apps/SamplingOverrides/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SamplingOverridesThreadName.java @@ -0,0 +1,58 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.applicationinsights.smoketest; + +import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_11; +import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_11_OPENJ9; +import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_17; +import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_19; +import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_20; +import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_8; +import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_8_OPENJ9; +import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.WILDFLY_13_JAVA_8; +import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.WILDFLY_13_JAVA_8_OPENJ9; +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +@UseAgent("applicationinsights-thread-name.json") +abstract class SamplingOverridesThreadName { + + @RegisterExtension static final SmokeTestExtension testing = SmokeTestExtension.create(); + + @Test + @TargetUri(value = "/thread-name", callCount = 100) + void testSampling() throws Exception { + assertThat(testing.mockedIngestion.getCountForType("RequestData")).isZero(); + assertThat(testing.mockedIngestion.getCountForType("MessageData")).isZero(); + } + + @Environment(TOMCAT_8_JAVA_8) + static class Tomcat8Java8Test extends SamplingOverridesThreadName {} + + @Environment(TOMCAT_8_JAVA_8_OPENJ9) + static class Tomcat8Java8OpenJ9Test extends SamplingOverridesThreadName {} + + @Environment(TOMCAT_8_JAVA_11) + static class Tomcat8Java11Test extends SamplingOverridesThreadName {} + + @Environment(TOMCAT_8_JAVA_11_OPENJ9) + static class Tomcat8Java11OpenJ9Test extends SamplingOverridesThreadName {} + + @Environment(TOMCAT_8_JAVA_17) + static class Tomcat8Java17Test extends SamplingOverridesThreadName {} + + @Environment(TOMCAT_8_JAVA_19) + static class Tomcat8Java19Test extends SamplingOverridesThreadName {} + + @Environment(TOMCAT_8_JAVA_20) + static class Tomcat8Java20Test extends SamplingOverridesThreadName {} + + @Environment(WILDFLY_13_JAVA_8) + static class Wildfly13Java8Test extends SamplingOverridesThreadName {} + + @Environment(WILDFLY_13_JAVA_8_OPENJ9) + static class Wildfly13Java8OpenJ9Test extends SamplingOverridesThreadName {} +} diff --git a/smoke-tests/apps/SamplingOverrides/src/smokeTest/resources/applicationinsights-thread-name.json b/smoke-tests/apps/SamplingOverrides/src/smokeTest/resources/applicationinsights-thread-name.json new file mode 100644 index 00000000000..e14fc39029b --- /dev/null +++ b/smoke-tests/apps/SamplingOverrides/src/smokeTest/resources/applicationinsights-thread-name.json @@ -0,0 +1,75 @@ +{ + "connectionString": "InstrumentationKey=12345678-0000-0000-0000-0FEEDDADBEEF;IngestionEndpoint=http://host.testcontainers.internal:6060/", + "role": { + "name": "testrolename", + "instance": "testroleinstance" + }, + "sampling": { + "percentage": 50 + }, + "preview": { + "sampling": { + "overrides": [ + { + "telemetryType": "request", + "attributes": [ + { + "key": "thread.name", + "value": "http-nio-.*", + "matchType": "regexp" + }, + { + "key": "http.target", + "value": "/SamplingOverrides/health-check", + "matchType": "strict" + } + ], + "percentage": 100 + }, + { + "telemetryType": "request", + "attributes": [ + { + "key": "thread.name", + "value": "http-nio-.*", + "matchType": "regexp" + }, + { + "key": "http.target", + "value": "/SamplingOverrides/", + "matchType": "strict" + } + ], + "percentage": 100 + }, + { + "telemetryType": "request", + "attributes": [ + { + "key": "thread.name", + "value": "http-nio-.*", + "matchType": "regexp" + }, + { + "key": "http.target", + "value": "/SamplingOverrides/thread-name", + "matchType": "strict" + } + ], + "percentage": 0 + }, + { + "telemetryType": "trace", + "attributes": [ + { + "key": "thread.name", + "value": "http-nio-.*", + "matchType": "regexp" + } + ], + "percentage": 0 + } + ] + } + } +}