From d8b0e644007964ce18e53c42e01f6b0b92abc3de Mon Sep 17 00:00:00 2001 From: heyams Date: Mon, 11 Mar 2024 12:21:00 -0700 Subject: [PATCH 1/4] Fix http.target with url.query --- .../internal/sampling/SamplingOverrides.java | 4 +- .../apps/SamplingOverrides/build.gradle.kts | 1 + .../SamplingOverridesServlet.java | 4 ++ .../UrlQuerySamplingOverridesTest.java | 66 +++++++++++++++++++ .../applicationinsights-urlquery.json | 22 +++++++ 5 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 smoke-tests/apps/SamplingOverrides/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/UrlQuerySamplingOverridesTest.java create mode 100644 smoke-tests/apps/SamplingOverrides/src/smokeTest/resources/applicationinsights-urlquery.json diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/SamplingOverrides.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/SamplingOverrides.java index b075bba5cca..08742ca1245 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/SamplingOverrides.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/SamplingOverrides.java @@ -291,10 +291,12 @@ private LazyHttpTarget(Attributes attributes) { this.attributes = attributes; } + @SuppressWarnings("SystemOut") private String get() { if (!initialized) { String urlQuery = attributes.get(SemanticAttributes.URL_QUERY); - value = attributes.get(SemanticAttributes.URL_PATH) + (urlQuery != null ? urlQuery : ""); + value = + attributes.get(SemanticAttributes.URL_PATH) + (urlQuery != null ? "?" + urlQuery : ""); initialized = true; } return value; diff --git a/smoke-tests/apps/SamplingOverrides/build.gradle.kts b/smoke-tests/apps/SamplingOverrides/build.gradle.kts index 1b1cfb106e9..db3a726ce23 100644 --- a/smoke-tests/apps/SamplingOverrides/build.gradle.kts +++ b/smoke-tests/apps/SamplingOverrides/build.gradle.kts @@ -4,4 +4,5 @@ plugins { dependencies { implementation("org.hsqldb:hsqldb:2.5.1") + implementation("ch.qos.logback:logback-classic") } diff --git a/smoke-tests/apps/SamplingOverrides/src/main/java/com/microsoft/applicationinsights/smoketestapp/SamplingOverridesServlet.java b/smoke-tests/apps/SamplingOverrides/src/main/java/com/microsoft/applicationinsights/smoketestapp/SamplingOverridesServlet.java index 41b03ff496d..445414e71f6 100644 --- a/smoke-tests/apps/SamplingOverrides/src/main/java/com/microsoft/applicationinsights/smoketestapp/SamplingOverridesServlet.java +++ b/smoke-tests/apps/SamplingOverrides/src/main/java/com/microsoft/applicationinsights/smoketestapp/SamplingOverridesServlet.java @@ -73,6 +73,10 @@ private int doGetInternal(HttpServletRequest req) throws Exception { executeStatement(connection); connection.close(); return 200; + } else if (pathInfo.equals("/test")) { + Connection connection = getHsqldbConnection(); + connection.clearWarnings(); + return 200; } else { throw new ServletException("Unexpected url: " + pathInfo); } diff --git a/smoke-tests/apps/SamplingOverrides/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/UrlQuerySamplingOverridesTest.java b/smoke-tests/apps/SamplingOverrides/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/UrlQuerySamplingOverridesTest.java new file mode 100644 index 00000000000..be5e62a608f --- /dev/null +++ b/smoke-tests/apps/SamplingOverrides/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/UrlQuerySamplingOverridesTest.java @@ -0,0 +1,66 @@ +// 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 static org.assertj.core.api.AssertionsForClassTypes.entry; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +@UseAgent("applicationinsights-urlquery.json") +abstract class UrlQuerySamplingOverridesTest { + + @RegisterExtension static final SmokeTestExtension testing = SmokeTestExtension.create(); + + @Test + @TargetUri(value = "/test?query") + void testUrlQuery() throws Exception { + Telemetry telemetry = testing.getTelemetry(0); + + assertThat(telemetry.rd.getName()).isEqualTo("GET /SamplingOverrides/*"); + assertThat(telemetry.rd.getUrl()).endsWith("http://localhost/SamplingOverrides/test?query"); + assertThat(telemetry.rd.getResponseCode()).isEqualTo("200"); + assertThat(telemetry.rd.getSuccess()).isTrue(); + assertThat(telemetry.rd.getSource()).isNull(); + assertThat(telemetry.rd.getProperties()) + .containsExactly(entry("_MS.ProcessedByMetricExtractors", "True")); + } + + @Environment(TOMCAT_8_JAVA_8) + static class Tomcat8Java8Test extends UrlQuerySamplingOverridesTest {} + + @Environment(TOMCAT_8_JAVA_8_OPENJ9) + static class Tomcat8Java8OpenJ9Test extends UrlQuerySamplingOverridesTest {} + + @Environment(TOMCAT_8_JAVA_11) + static class Tomcat8Java11Test extends UrlQuerySamplingOverridesTest {} + + @Environment(TOMCAT_8_JAVA_11_OPENJ9) + static class Tomcat8Java11OpenJ9Test extends UrlQuerySamplingOverridesTest {} + + @Environment(TOMCAT_8_JAVA_17) + static class Tomcat8Java17Test extends UrlQuerySamplingOverridesTest {} + + @Environment(TOMCAT_8_JAVA_19) + static class Tomcat8Java19Test extends UrlQuerySamplingOverridesTest {} + + @Environment(TOMCAT_8_JAVA_20) + static class Tomcat8Java20Test extends UrlQuerySamplingOverridesTest {} + + @Environment(WILDFLY_13_JAVA_8) + static class Wildfly13Java8Test extends UrlQuerySamplingOverridesTest {} + + @Environment(WILDFLY_13_JAVA_8_OPENJ9) + static class Wildfly13Java8OpenJ9Test extends UrlQuerySamplingOverridesTest {} +} diff --git a/smoke-tests/apps/SamplingOverrides/src/smokeTest/resources/applicationinsights-urlquery.json b/smoke-tests/apps/SamplingOverrides/src/smokeTest/resources/applicationinsights-urlquery.json new file mode 100644 index 00000000000..c50a0804dd4 --- /dev/null +++ b/smoke-tests/apps/SamplingOverrides/src/smokeTest/resources/applicationinsights-urlquery.json @@ -0,0 +1,22 @@ +{ + "role": { + "name": "testrolename", + "instance": "testroleinstance" + }, + "sampling": { + "percentage": 50, + "overrides": [ + { + "telemetryType": "request", + "attributes": [ + { + "key": "http.target", + "value": "/SamplingOverrides/test?query", + "matchType": "strict" + } + ], + "percentage": 100 + } + ] + } +} From ec1f72803382b8b6787447a19eb29af831a55ea7 Mon Sep 17 00:00:00 2001 From: heyams Date: Mon, 11 Mar 2024 12:26:24 -0700 Subject: [PATCH 2/4] Remove std --- .../agent/internal/sampling/SamplingOverrides.java | 1 - 1 file changed, 1 deletion(-) diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/SamplingOverrides.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/SamplingOverrides.java index 08742ca1245..780c648e6a1 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/SamplingOverrides.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/SamplingOverrides.java @@ -291,7 +291,6 @@ private LazyHttpTarget(Attributes attributes) { this.attributes = attributes; } - @SuppressWarnings("SystemOut") private String get() { if (!initialized) { String urlQuery = attributes.get(SemanticAttributes.URL_QUERY); From e4044db9e02722248861a85d20e6cf22440f64a7 Mon Sep 17 00:00:00 2001 From: heyams Date: Mon, 11 Mar 2024 12:27:23 -0700 Subject: [PATCH 3/4] Remove unnecessary dependency --- smoke-tests/apps/SamplingOverrides/build.gradle.kts | 1 - 1 file changed, 1 deletion(-) diff --git a/smoke-tests/apps/SamplingOverrides/build.gradle.kts b/smoke-tests/apps/SamplingOverrides/build.gradle.kts index db3a726ce23..1b1cfb106e9 100644 --- a/smoke-tests/apps/SamplingOverrides/build.gradle.kts +++ b/smoke-tests/apps/SamplingOverrides/build.gradle.kts @@ -4,5 +4,4 @@ plugins { dependencies { implementation("org.hsqldb:hsqldb:2.5.1") - implementation("ch.qos.logback:logback-classic") } From 6d4a2e70740cc159e27db2d3bfaf885705c328e2 Mon Sep 17 00:00:00 2001 From: heyams Date: Mon, 11 Mar 2024 16:57:26 -0700 Subject: [PATCH 4/4] Fix --- .../smoketest/UrlQuerySamplingOverridesTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/smoke-tests/apps/SamplingOverrides/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/UrlQuerySamplingOverridesTest.java b/smoke-tests/apps/SamplingOverrides/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/UrlQuerySamplingOverridesTest.java index be5e62a608f..e89af3b1279 100644 --- a/smoke-tests/apps/SamplingOverrides/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/UrlQuerySamplingOverridesTest.java +++ b/smoke-tests/apps/SamplingOverrides/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/UrlQuerySamplingOverridesTest.java @@ -21,7 +21,9 @@ @UseAgent("applicationinsights-urlquery.json") abstract class UrlQuerySamplingOverridesTest { - @RegisterExtension static final SmokeTestExtension testing = SmokeTestExtension.create(); + @RegisterExtension + static final SmokeTestExtension testing = + SmokeTestExtension.builder().setUseDefaultHttpPort().build(); @Test @TargetUri(value = "/test?query")