Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
trask committed Jan 22, 2025
1 parent 8ce7b0b commit 90d4ada
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.opentelemetry.sdk.logs.data.LogRecordData;
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
import io.opentelemetry.sdk.trace.samplers.SamplingDecision;
import io.opentelemetry.sdk.trace.samplers.SamplingResult;
import io.opentelemetry.semconv.SemanticAttributes;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -122,12 +123,14 @@ private CompletableResultCode internalExport(Collection<LogRecordData> logs) {

Double sampleRate = parentSpanSampleRate;
if (sampler != null) {
if (sampler.shouldSampleLog(spanContext, parentSpanSampleRate).getDecision()
== SamplingDecision.DROP) {
SamplingResult samplingResult =
sampler.shouldSampleLog(spanContext, parentSpanSampleRate);
if (samplingResult.getDecision() == SamplingDecision.DROP) {
continue;
}
// sampling override percentage takes precedence
sampleRate = sampler.getParentlessDependencySamplingPercentage().get();
if (sampleRate == null) {
sampleRate = samplingResult.getAttributes().get(AiSemanticAttributes.SAMPLE_RATE);
}
}

logger.debug("exporting log: {}", log);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ void testSampling() throws Exception {
assertThat(dependencyCount).isLessThanOrEqualTo(20);
assertThat(logCount).isGreaterThanOrEqualTo(2);
assertThat(logCount).isLessThanOrEqualTo(20);

testing
.mockedIngestion
.getItemsEnvelopeDataType("RequestData")
.forEach(
item -> {
assertThat(item.getSampleRate()).isEqualTo(10);
});
testing
.mockedIngestion
.getItemsEnvelopeDataType("RemoteDependencyData")
.forEach(
item -> {
assertThat(item.getSampleRate()).isEqualTo(10);
});
testing
.mockedIngestion
.getItemsEnvelopeDataType("MessageData")
.forEach(
item -> {
assertThat(item.getSampleRate()).isEqualTo(10);
});
}

@Environment(TOMCAT_8_JAVA_8)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// 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_17_OPENJ9;
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_21;
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_21_OPENJ9;
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_23;
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_23_OPENJ9;
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 java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

@UseAgent("applicationinsights4.json")
abstract class SamplingOverrides4Test {

@RegisterExtension static final SmokeTestExtension testing = SmokeTestExtension.create();

@Test
@TargetUri(value = "/health-check", callCount = 100)
void testSampling() throws Exception {
// super super low chance that number of sampled requests is less than 25
long start = System.nanoTime();
while (testing.mockedIngestion.getCountForType("RequestData") < 25
&& NANOSECONDS.toSeconds(System.nanoTime() - start) < 10) {}
// wait ten more seconds to before checking that we didn't receive too many
Thread.sleep(SECONDS.toMillis(10));
int requestCount = testing.mockedIngestion.getCountForType("RequestData");
int dependencyCount = testing.mockedIngestion.getCountForType("RemoteDependencyData");
int logCount = testing.mockedIngestion.getCountForType("MessageData");
// super super low chance that number of sampled requests/dependencies/traces
// is less than 25 or greater than 75
assertThat(requestCount).isGreaterThanOrEqualTo(25);
assertThat(requestCount).isLessThanOrEqualTo(75);
// super super low chance that number of sampled dependencies/traces
// is less than 2 or greater than 20
assertThat(dependencyCount).isGreaterThanOrEqualTo(2);
assertThat(dependencyCount).isLessThanOrEqualTo(20);
assertThat(logCount).isGreaterThanOrEqualTo(2);
assertThat(logCount).isLessThanOrEqualTo(20);

testing
.mockedIngestion
.getItemsEnvelopeDataType("RequestData")
.forEach(
item -> {
assertThat(item.getSampleRate()).isEqualTo(10);
});
testing
.mockedIngestion
.getItemsEnvelopeDataType("RemoteDependencyData")
.forEach(
item -> {
assertThat(item.getSampleRate()).isEqualTo(10);
});
testing
.mockedIngestion
.getItemsEnvelopeDataType("MessageData")
.forEach(
item -> {
assertThat(item.getSampleRate()).isEqualTo(10);
});
}

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

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

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

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

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

@Environment(TOMCAT_8_JAVA_17_OPENJ9)
static class Tomcat8Java17OpenJ9Test extends SamplingOverrides4Test {}

@Environment(TOMCAT_8_JAVA_21)
static class Tomcat8Java21Test extends SamplingOverrides4Test {}

@Environment(TOMCAT_8_JAVA_21_OPENJ9)
static class Tomcat8Java21OpenJ9Test extends SamplingOverrides4Test {}

@Environment(TOMCAT_8_JAVA_23)
static class Tomcat8Java23Test extends SamplingOverrides4Test {}

@Environment(TOMCAT_8_JAVA_23_OPENJ9)
static class Tomcat8Java23OpenJ9Test extends SamplingOverrides4Test {}

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

@Environment(WILDFLY_13_JAVA_8_OPENJ9)
static class Wildfly13Java8OpenJ9Test extends SamplingOverrides4Test {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"role": {
"name": "testrolename",
"instance": "testroleinstance"
},
"sampling": {
"percentage": 100,
"overrides": [
{
"telemetryType": "request",
"attributes": [
{
"key": "http.url",
"value": ".*/health-check",
"matchType": "regexp"
}
],
"percentage": 50,
"id": "filter out health check"
},
{
"telemetryType": "dependency",
"attributes": [
{
"key": "db.statement",
"value": "select count(*) from abc",
"matchType": "strict"
}
],
"percentage": 10,
"id": "filter out noisy jdbc"
},
{
"telemetryType": "trace",
"percentage": 10
}
]
}
}

0 comments on commit 90d4ada

Please sign in to comment.