-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply sampling override to Exceptions (#3022)
- Loading branch information
Showing
8 changed files
with
173 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...rc/main/java/com/microsoft/applicationinsights/agent/internal/exporter/ExporterUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.microsoft.applicationinsights.agent.internal.exporter; | ||
|
||
import com.microsoft.applicationinsights.agent.internal.sampling.AiSampler; | ||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
import io.opentelemetry.api.trace.SpanContext; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
public final class ExporterUtils { | ||
|
||
@SuppressFBWarnings( | ||
value = "SECPR", // Predictable pseudorandom number generator | ||
justification = "Predictable random is ok for sampling decision") | ||
public static boolean shouldSample(SpanContext spanContext, double percentage) { | ||
if (percentage == 100) { | ||
// optimization, no need to calculate score | ||
return true; | ||
} | ||
if (percentage == 0) { | ||
// optimization, no need to calculate score | ||
return false; | ||
} | ||
if (spanContext.isValid()) { | ||
return AiSampler.shouldRecordAndSample(spanContext.getTraceId(), percentage); | ||
} | ||
// this is a standalone log (not part of a trace), so randomly sample at the given percentage | ||
return ThreadLocalRandom.current().nextDouble() < percentage / 100; | ||
} | ||
|
||
private ExporterUtils() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...ava/com/microsoft/applicationinsights/smoketestapp/ExceptionSamplingOverridesServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.microsoft.applicationinsights.smoketestapp; | ||
|
||
import javax.servlet.annotation.WebServlet; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
@WebServlet("/trackException") | ||
public class ExceptionSamplingOverridesServlet extends HttpServlet { | ||
|
||
protected void doGet(HttpServletRequest request, HttpServletResponse response) { | ||
throw new RuntimeException("this is an expected exception"); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...Test/java/com/microsoft/applicationinsights/smoketest/ExceptionSamplingOverridesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// 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 com.microsoft.applicationinsights.smoketest.schemav2.Envelope; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
@UseAgent("applicationinsights3.json") | ||
abstract class ExceptionSamplingOverridesTest { | ||
|
||
@RegisterExtension static final SmokeTestExtension testing = SmokeTestExtension.create(); | ||
|
||
@Test | ||
@TargetUri(value = "/trackException") | ||
void testExceptionSamplingOverrides() throws Exception { | ||
List<Envelope> rdList = testing.mockedIngestion.waitForItems("RequestData", 1); | ||
Envelope rdEnvelope = rdList.get(0); | ||
assertThat(rdEnvelope.getTags().get("ai.operation.name")) | ||
.isEqualTo("GET /SamplingOverrides/trackException"); | ||
assertThat(testing.mockedIngestion.getCountForType("ExceptionData")).isZero(); | ||
} | ||
|
||
@Environment(TOMCAT_8_JAVA_8) | ||
static class Tomcat8Java8Test extends ExceptionSamplingOverridesTest {} | ||
|
||
@Environment(TOMCAT_8_JAVA_8_OPENJ9) | ||
static class Tomcat8Java8OpenJ9Test extends ExceptionSamplingOverridesTest {} | ||
|
||
@Environment(TOMCAT_8_JAVA_11) | ||
static class Tomcat8Java11Test extends ExceptionSamplingOverridesTest {} | ||
|
||
@Environment(TOMCAT_8_JAVA_11_OPENJ9) | ||
static class Tomcat8Java11OpenJ9Test extends ExceptionSamplingOverridesTest {} | ||
|
||
@Environment(TOMCAT_8_JAVA_17) | ||
static class Tomcat8Java17Test extends ExceptionSamplingOverridesTest {} | ||
|
||
@Environment(TOMCAT_8_JAVA_19) | ||
static class Tomcat8Java19Test extends ExceptionSamplingOverridesTest {} | ||
|
||
@Environment(TOMCAT_8_JAVA_20) | ||
static class Tomcat8Java20Test extends ExceptionSamplingOverridesTest {} | ||
|
||
@Environment(WILDFLY_13_JAVA_8) | ||
static class Wildfly13Java8Test extends ExceptionSamplingOverridesTest {} | ||
|
||
@Environment(WILDFLY_13_JAVA_8_OPENJ9) | ||
static class Wildfly13Java8OpenJ9Test extends ExceptionSamplingOverridesTest {} | ||
} |
31 changes: 31 additions & 0 deletions
31
smoke-tests/apps/SamplingOverrides/src/smokeTest/resources/applicationinsights3.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"role": { | ||
"name": "testrolename", | ||
"instance": "testroleinstance" | ||
}, | ||
"sampling": { | ||
"percentage": 100 | ||
}, | ||
"preview": { | ||
"sampling": { | ||
"overrides": [ | ||
{ | ||
"telemetryType": "exception", | ||
"attributes": [ | ||
{ | ||
"key": "exception.message", | ||
"value": "this is an expected exception", | ||
"matchType": "strict" | ||
}, | ||
{ | ||
"key": "exception.type", | ||
"value": "java.lang.RuntimeException", | ||
"matchType": "strict" | ||
} | ||
], | ||
"percentage": 0 | ||
} | ||
] | ||
} | ||
} | ||
} |