Skip to content

Commit 11b4832

Browse files
committed
up
1 parent 8ce7b0b commit 11b4832

File tree

4 files changed

+179
-4
lines changed

4 files changed

+179
-4
lines changed

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/exporter/AgentLogExporter.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.opentelemetry.sdk.logs.data.LogRecordData;
2424
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
2525
import io.opentelemetry.sdk.trace.samplers.SamplingDecision;
26+
import io.opentelemetry.sdk.trace.samplers.SamplingResult;
2627
import io.opentelemetry.semconv.SemanticAttributes;
2728
import java.util.Collection;
2829
import java.util.List;
@@ -122,12 +123,14 @@ private CompletableResultCode internalExport(Collection<LogRecordData> logs) {
122123

123124
Double sampleRate = parentSpanSampleRate;
124125
if (sampler != null) {
125-
if (sampler.shouldSampleLog(spanContext, parentSpanSampleRate).getDecision()
126-
== SamplingDecision.DROP) {
126+
SamplingResult samplingResult =
127+
sampler.shouldSampleLog(spanContext, parentSpanSampleRate);
128+
if (samplingResult.getDecision() == SamplingDecision.DROP) {
127129
continue;
128130
}
129-
// sampling override percentage takes precedence
130-
sampleRate = sampler.getParentlessDependencySamplingPercentage().get();
131+
if (sampleRate == null) {
132+
sampleRate = samplingResult.getAttributes().get(AiSemanticAttributes.SAMPLE_RATE);
133+
}
131134
}
132135

133136
logger.debug("exporting log: {}", log);

smoke-tests/apps/SamplingOverrides/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SamplingOverrides3Test.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,28 @@ void testSampling() throws Exception {
4747
assertThat(dependencyCount).isLessThanOrEqualTo(20);
4848
assertThat(logCount).isGreaterThanOrEqualTo(2);
4949
assertThat(logCount).isLessThanOrEqualTo(20);
50+
51+
testing
52+
.mockedIngestion
53+
.getItemsEnvelopeDataType("RequestData")
54+
.forEach(
55+
item -> {
56+
assertThat(item.getSampleRate()).isEqualTo(10);
57+
});
58+
testing
59+
.mockedIngestion
60+
.getItemsEnvelopeDataType("RemoteDependencyData")
61+
.forEach(
62+
item -> {
63+
assertThat(item.getSampleRate()).isEqualTo(10);
64+
});
65+
testing
66+
.mockedIngestion
67+
.getItemsEnvelopeDataType("MessageData")
68+
.forEach(
69+
item -> {
70+
assertThat(item.getSampleRate()).isEqualTo(10);
71+
});
5072
}
5173

5274
@Environment(TOMCAT_8_JAVA_8)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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_17_OPENJ9;
10+
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_21;
11+
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_21_OPENJ9;
12+
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_23;
13+
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_23_OPENJ9;
14+
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_8;
15+
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_8_OPENJ9;
16+
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.WILDFLY_13_JAVA_8;
17+
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.WILDFLY_13_JAVA_8_OPENJ9;
18+
import static java.util.concurrent.TimeUnit.NANOSECONDS;
19+
import static java.util.concurrent.TimeUnit.SECONDS;
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
22+
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.extension.RegisterExtension;
24+
25+
@UseAgent("applicationinsights4.json")
26+
abstract class SamplingOverrides4Test {
27+
28+
@RegisterExtension static final SmokeTestExtension testing = SmokeTestExtension.create();
29+
30+
@Test
31+
@TargetUri(value = "/health-check", callCount = 100)
32+
void testSampling() throws Exception {
33+
// super super low chance that number of sampled requests is less than 25
34+
long start = System.nanoTime();
35+
while (testing.mockedIngestion.getCountForType("RequestData") < 25
36+
&& NANOSECONDS.toSeconds(System.nanoTime() - start) < 10) {}
37+
// wait ten more seconds to before checking that we didn't receive too many
38+
Thread.sleep(SECONDS.toMillis(10));
39+
int requestCount = testing.mockedIngestion.getCountForType("RequestData");
40+
int dependencyCount = testing.mockedIngestion.getCountForType("RemoteDependencyData");
41+
int logCount = testing.mockedIngestion.getCountForType("MessageData");
42+
// super super low chance that number of sampled requests/dependencies/traces
43+
// is less than 25 or greater than 75
44+
assertThat(requestCount).isGreaterThanOrEqualTo(25);
45+
assertThat(requestCount).isLessThanOrEqualTo(75);
46+
// super super low chance that number of sampled dependencies/traces
47+
// is less than 2 or greater than 20
48+
assertThat(dependencyCount).isGreaterThanOrEqualTo(2);
49+
assertThat(dependencyCount).isLessThanOrEqualTo(20);
50+
assertThat(logCount).isGreaterThanOrEqualTo(2);
51+
assertThat(logCount).isLessThanOrEqualTo(20);
52+
53+
testing
54+
.mockedIngestion
55+
.getItemsEnvelopeDataType("RequestData")
56+
.forEach(
57+
item -> {
58+
assertThat(item.getSampleRate()).isEqualTo(2);
59+
});
60+
testing
61+
.mockedIngestion
62+
.getItemsEnvelopeDataType("RemoteDependencyData")
63+
.forEach(
64+
item -> {
65+
assertThat(item.getSampleRate()).isEqualTo(10);
66+
});
67+
testing
68+
.mockedIngestion
69+
.getItemsEnvelopeDataType("MessageData")
70+
.forEach(
71+
item -> {
72+
assertThat(item.getSampleRate()).isEqualTo(10);
73+
});
74+
}
75+
76+
@Environment(TOMCAT_8_JAVA_8)
77+
static class Tomcat8Java8Test extends SamplingOverrides4Test {}
78+
79+
@Environment(TOMCAT_8_JAVA_8_OPENJ9)
80+
static class Tomcat8Java8OpenJ9Test extends SamplingOverrides4Test {}
81+
82+
@Environment(TOMCAT_8_JAVA_11)
83+
static class Tomcat8Java11Test extends SamplingOverrides4Test {}
84+
85+
@Environment(TOMCAT_8_JAVA_11_OPENJ9)
86+
static class Tomcat8Java11OpenJ9Test extends SamplingOverrides4Test {}
87+
88+
@Environment(TOMCAT_8_JAVA_17)
89+
static class Tomcat8Java17Test extends SamplingOverrides4Test {}
90+
91+
@Environment(TOMCAT_8_JAVA_17_OPENJ9)
92+
static class Tomcat8Java17OpenJ9Test extends SamplingOverrides4Test {}
93+
94+
@Environment(TOMCAT_8_JAVA_21)
95+
static class Tomcat8Java21Test extends SamplingOverrides4Test {}
96+
97+
@Environment(TOMCAT_8_JAVA_21_OPENJ9)
98+
static class Tomcat8Java21OpenJ9Test extends SamplingOverrides4Test {}
99+
100+
@Environment(TOMCAT_8_JAVA_23)
101+
static class Tomcat8Java23Test extends SamplingOverrides4Test {}
102+
103+
@Environment(TOMCAT_8_JAVA_23_OPENJ9)
104+
static class Tomcat8Java23OpenJ9Test extends SamplingOverrides4Test {}
105+
106+
@Environment(WILDFLY_13_JAVA_8)
107+
static class Wildfly13Java8Test extends SamplingOverrides4Test {}
108+
109+
@Environment(WILDFLY_13_JAVA_8_OPENJ9)
110+
static class Wildfly13Java8OpenJ9Test extends SamplingOverrides4Test {}
111+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"role": {
3+
"name": "testrolename",
4+
"instance": "testroleinstance"
5+
},
6+
"sampling": {
7+
"percentage": 100,
8+
"overrides": [
9+
{
10+
"telemetryType": "request",
11+
"attributes": [
12+
{
13+
"key": "http.url",
14+
"value": ".*/health-check",
15+
"matchType": "regexp"
16+
}
17+
],
18+
"percentage": 50,
19+
"id": "filter out health check"
20+
},
21+
{
22+
"telemetryType": "dependency",
23+
"attributes": [
24+
{
25+
"key": "db.statement",
26+
"value": "select count(*) from abc",
27+
"matchType": "strict"
28+
}
29+
],
30+
"percentage": 10,
31+
"id": "filter out noisy jdbc"
32+
},
33+
{
34+
"telemetryType": "trace",
35+
"percentage": 10
36+
}
37+
]
38+
}
39+
}

0 commit comments

Comments
 (0)