From 90d4ada18abb771b3b5b7b6ea40190533506cc5c Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Mon, 13 Jan 2025 20:59:20 -0800 Subject: [PATCH] up --- .../internal/exporter/AgentLogExporter.java | 11 +- .../smoketest/SamplingOverrides3Test.java | 22 ++++ .../smoketest/SamplingOverrides4Test.java | 111 ++++++++++++++++++ .../resources/applicationinsights4.json | 39 ++++++ 4 files changed, 179 insertions(+), 4 deletions(-) create mode 100644 smoke-tests/apps/SamplingOverrides/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SamplingOverrides4Test.java create mode 100644 smoke-tests/apps/SamplingOverrides/src/smokeTest/resources/applicationinsights4.json diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/exporter/AgentLogExporter.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/exporter/AgentLogExporter.java index 0e86e9603c..0c79be02cc 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/exporter/AgentLogExporter.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/exporter/AgentLogExporter.java @@ -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; @@ -122,12 +123,14 @@ private CompletableResultCode internalExport(Collection 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); diff --git a/smoke-tests/apps/SamplingOverrides/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SamplingOverrides3Test.java b/smoke-tests/apps/SamplingOverrides/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SamplingOverrides3Test.java index 9310bdeb3d..50367e73cb 100644 --- a/smoke-tests/apps/SamplingOverrides/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SamplingOverrides3Test.java +++ b/smoke-tests/apps/SamplingOverrides/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SamplingOverrides3Test.java @@ -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) diff --git a/smoke-tests/apps/SamplingOverrides/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SamplingOverrides4Test.java b/smoke-tests/apps/SamplingOverrides/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SamplingOverrides4Test.java new file mode 100644 index 0000000000..ed217879f9 --- /dev/null +++ b/smoke-tests/apps/SamplingOverrides/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SamplingOverrides4Test.java @@ -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 {} +} diff --git a/smoke-tests/apps/SamplingOverrides/src/smokeTest/resources/applicationinsights4.json b/smoke-tests/apps/SamplingOverrides/src/smokeTest/resources/applicationinsights4.json new file mode 100644 index 0000000000..bcfee4ef2e --- /dev/null +++ b/smoke-tests/apps/SamplingOverrides/src/smokeTest/resources/applicationinsights4.json @@ -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 + } + ] + } +}