Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable sampling override using thread.name #3285

Merged
merged 11 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public SamplingResult shouldSample(
SpanKind spanKind,
Attributes attributes,
List<LinkData> parentLinks) {

SpanContext parentSpanContext = Span.fromContext(parentContext).getSpanContext();
boolean isRequest = RequestChecker.isRequest(spanKind, parentSpanContext, attributes::get);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public SamplingResult shouldSample(
SpanKind spanKind,
Attributes attributes,
List<LinkData> parentLinks) {

if (localParentBased) {
SamplingResult samplingResult = useLocalParentDecisionIfPossible(parentContext);
if (samplingResult != null) {
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ private boolean matches(Attributes attributes, @Nullable LazyHttpUrl lazyHttpUrl
return true;
}

static String getValueIncludingThreadAttributes(
heyams marked this conversation as resolved.
Show resolved Hide resolved
Attributes attributes, AttributeKey<String> 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)) {
Expand Down Expand Up @@ -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.getValueIncludingThreadAttributes(attributes, key);
if (val == null && key.getKey().equals(SemanticAttributes.HTTP_URL.getKey())) {
val = lazyHttpUrl.get();
}
Expand Down Expand Up @@ -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.getValueIncludingThreadAttributes(attributes, key);
if (val == null
&& key.getKey().equals(SemanticAttributes.HTTP_URL.getKey())
&& lazyHttpUrl != null) {
Expand Down Expand Up @@ -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.getValueIncludingThreadAttributes(attributes, key);
if (val == null
&& key.getKey().equals(SemanticAttributes.HTTP_URL.getKey())
&& lazyHttpUrl != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {}
}
Original file line number Diff line number Diff line change
@@ -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
}
]
}
}
}
Loading