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 3 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 @@ -57,6 +57,13 @@ public SamplingResult shouldSample(
Attributes attributes,
List<LinkData> parentLinks) {

Attributes extraAttributes =
heyams marked this conversation as resolved.
Show resolved Hide resolved
Attributes.builder()
.putAll(attributes)
.put("thread.id", String.valueOf(Thread.currentThread().getId()))
.put("thread.name", Thread.currentThread().getName())
.build();

if (localParentBased) {
SamplingResult samplingResult = useLocalParentDecisionIfPossible(parentContext);
if (samplingResult != null) {
Expand All @@ -70,7 +77,8 @@ public SamplingResult shouldSample(
sp = requestSamplingPercentage.get();
} else {
SpanContext parentSpanContext = Span.fromContext(parentContext).getSpanContext();
boolean isRequest = RequestChecker.isRequest(spanKind, parentSpanContext, attributes::get);
boolean isRequest =
RequestChecker.isRequest(spanKind, parentSpanContext, extraAttributes::get);

sp =
isRequest
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("/threadIdAndName")) {
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,60 @@
// 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-id-and-name.json")
abstract class SamplingOverridesThreadIdAndName {

@RegisterExtension
static final SmokeTestExtension testing =
SmokeTestExtension.builder().setSelfDiagnosticsLevel("debug").build();

@Test
@TargetUri(value = "/threadIdAndName", callCount = 100)
void testSampling() throws Exception {
// assertThat(testing.mockedIngestion.getCountForType("RequestData")).isZero();
heyams marked this conversation as resolved.
Show resolved Hide resolved
assertThat(testing.mockedIngestion.getCountForType("MessageData")).isZero();
}

@Environment(TOMCAT_8_JAVA_8)
static class Tomcat8Java8Test extends SamplingOverridesThreadIdAndName {}

@Environment(TOMCAT_8_JAVA_8_OPENJ9)
static class Tomcat8Java8OpenJ9Test extends SamplingOverridesThreadIdAndName {}

@Environment(TOMCAT_8_JAVA_11)
static class Tomcat8Java11Test extends SamplingOverridesThreadIdAndName {}

@Environment(TOMCAT_8_JAVA_11_OPENJ9)
static class Tomcat8Java11OpenJ9Test extends SamplingOverridesThreadIdAndName {}

@Environment(TOMCAT_8_JAVA_17)
static class Tomcat8Java17Test extends SamplingOverridesThreadIdAndName {}

@Environment(TOMCAT_8_JAVA_19)
static class Tomcat8Java19Test extends SamplingOverridesThreadIdAndName {}

@Environment(TOMCAT_8_JAVA_20)
static class Tomcat8Java20Test extends SamplingOverridesThreadIdAndName {}

@Environment(WILDFLY_13_JAVA_8)
static class Wildfly13Java8Test extends SamplingOverridesThreadIdAndName {}

@Environment(WILDFLY_13_JAVA_8_OPENJ9)
static class Wildfly13Java8OpenJ9Test extends SamplingOverridesThreadIdAndName {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"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"
}
],
"percentage": 0
},
{
"telemetryType": "trace",
"attributes": [
{
"key": "thread.name",
"value": "http-nio-.*",
"matchType": "regexp"
}
],
"percentage": 0
},
{
"telemetryType": "trace",
"attributes": [
{
"key": "thread.name",
"value": "main",
"matchType": "strict"
}
],
"percentage": 0
heyams marked this conversation as resolved.
Show resolved Hide resolved
}
]
}
}
}
Loading