From bb35191d80901aa87f3a0f82c00dc7ff7cb25937 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 23 Sep 2022 12:56:51 -0700 Subject: [PATCH] Make 3.4 Classic SDK work with prior 3.x Agent versions (#2531) Resolves https://github.com/microsoft/ApplicationInsights-Java/issues/2522#issuecomment-1252688528 TODO add smoke tests --- .../src/main/kotlin/ai.smoke-test.gradle.kts | 5 + .../applicationinsights/TelemetryClient.java | 10 + .../TelemetryConfiguration.java | 13 + .../telemetry/ExceptionTelemetry.java | 6 + .../telemetry/TelemetryContext.java | 7 + settings.gradle | 2 + .../build.gradle.kts | 12 + .../smoketestapp/SpringBootApp.java | 16 + .../smoketestapp/TestController.java | 34 ++ ...sicSdkWebInterop3xUsingOld3xAgentTest.java | 72 +++ .../src/smokeTest/resources/logback-test.xml | 11 + .../smoketest/CoreAndFilter2xTest.java | 2 +- .../smoketest/CoreAndFilter3xTest.java | 33 -- .../build.gradle.kts | 7 + .../smoketestapp/HealthCheckServlet.java | 17 + ...SimpleTestRequestSlowWithResponseTime.java | 32 + .../SimpleThrowExceptionServlet.java | 19 + .../SimpleTrackAvailabilityServlet.java | 30 + .../SimpleTrackDependencyServlet.java | 21 + .../smoketestapp/SimpleTrackEventServlet.java | 38 ++ .../SimpleTrackExceptionServlet.java | 45 ++ .../SimpleTrackHttpRequestServlet.java | 38 ++ .../SimpleTrackMetricServlet.java | 20 + ...SimpleTrackMetricWithNamespaceServlet.java | 27 + .../SimpleTrackPageViewServlet.java | 89 +++ .../smoketestapp/SimpleTrackTraceServlet.java | 32 + .../SlowRequestCpuBoundServlet.java | 48 ++ .../src/main/webapp/doPageView.jsp | 17 + .../CoreAndFilter3xUsingOld3xAgentTest.java | 551 ++++++++++++++++++ .../src/smokeTest/resources/logback-test.xml | 11 + .../smoketest/SmokeTestExtension.java | 36 +- .../smoketest/SmokeTestExtensionBuilder.java | 9 +- 32 files changed, 1261 insertions(+), 49 deletions(-) create mode 100644 classic-sdk/core/src/main/java/com/microsoft/applicationinsights/TelemetryConfiguration.java create mode 100644 smoke-tests/apps/ClassicSdkWebInterop3xUsingOld3xAgent/build.gradle.kts create mode 100644 smoke-tests/apps/ClassicSdkWebInterop3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SpringBootApp.java create mode 100644 smoke-tests/apps/ClassicSdkWebInterop3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/TestController.java create mode 100644 smoke-tests/apps/ClassicSdkWebInterop3xUsingOld3xAgent/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/ClassicSdkWebInterop3xUsingOld3xAgentTest.java create mode 100644 smoke-tests/apps/ClassicSdkWebInterop3xUsingOld3xAgent/src/smokeTest/resources/logback-test.xml create mode 100644 smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/build.gradle.kts create mode 100644 smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/HealthCheckServlet.java create mode 100644 smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTestRequestSlowWithResponseTime.java create mode 100644 smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleThrowExceptionServlet.java create mode 100644 smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackAvailabilityServlet.java create mode 100644 smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackDependencyServlet.java create mode 100644 smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackEventServlet.java create mode 100644 smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackExceptionServlet.java create mode 100644 smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackHttpRequestServlet.java create mode 100644 smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackMetricServlet.java create mode 100644 smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackMetricWithNamespaceServlet.java create mode 100644 smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackPageViewServlet.java create mode 100644 smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackTraceServlet.java create mode 100644 smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SlowRequestCpuBoundServlet.java create mode 100644 smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/webapp/doPageView.jsp create mode 100644 smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/CoreAndFilter3xUsingOld3xAgentTest.java create mode 100644 smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/smokeTest/resources/logback-test.xml diff --git a/buildSrc/src/main/kotlin/ai.smoke-test.gradle.kts b/buildSrc/src/main/kotlin/ai.smoke-test.gradle.kts index c99b9428296..3f23cacbb18 100644 --- a/buildSrc/src/main/kotlin/ai.smoke-test.gradle.kts +++ b/buildSrc/src/main/kotlin/ai.smoke-test.gradle.kts @@ -57,6 +57,7 @@ afterEvaluate { } val agent by configurations.creating +val old3xAgent by configurations.creating dependencies { // FIXME (trask) copy-pasted from ai.java-conventions.gradle @@ -70,6 +71,8 @@ dependencies { smokeTestImplementation("org.assertj:assertj-core") agent(project(":agent:agent", configuration = "shadow")) + + old3xAgent("com.microsoft.azure:applicationinsights-agent:3.2.11") } tasks { @@ -91,10 +94,12 @@ tasks { val appFile = aiSmokeTest.testAppArtifactDir.file(aiSmokeTest.testAppArtifactFilename.get()).get() val javaagentFile = agent.singleFile + val old3xJavaagentFile = old3xAgent.singleFile // need to delay for project to configure the extension systemProperty("ai.smoke-test.test-app-file", appFile) systemProperty("ai.smoke-test.javaagent-file", javaagentFile) + systemProperty("ai.smoke-test.old-3x-javaagent-file", old3xJavaagentFile) val smokeTestMatrix = findProperty("smokeTestMatrix") ?: System.getenv("CI") != null systemProperty("ai.smoke-test.matrix", smokeTestMatrix) diff --git a/classic-sdk/core/src/main/java/com/microsoft/applicationinsights/TelemetryClient.java b/classic-sdk/core/src/main/java/com/microsoft/applicationinsights/TelemetryClient.java index eab9230d30b..3b53dda06e4 100644 --- a/classic-sdk/core/src/main/java/com/microsoft/applicationinsights/TelemetryClient.java +++ b/classic-sdk/core/src/main/java/com/microsoft/applicationinsights/TelemetryClient.java @@ -25,6 +25,16 @@ public class TelemetryClient { private final TelemetryContext context = new TelemetryContext(); + // this is required for interop with versions of the Java agent prior to 3.4.0 + @SuppressWarnings("UnusedVariable") + private final TelemetryConfiguration configuration = new TelemetryConfiguration(); + + // this is required for interop with versions of the Java agent prior to 3.4.0 + @SuppressWarnings({"UnusedMethod", "MethodCanBeStatic"}) + private boolean isDisabled() { + return false; + } + public TelemetryClient() {} public TelemetryContext getContext() { diff --git a/classic-sdk/core/src/main/java/com/microsoft/applicationinsights/TelemetryConfiguration.java b/classic-sdk/core/src/main/java/com/microsoft/applicationinsights/TelemetryConfiguration.java new file mode 100644 index 00000000000..7efe0df9528 --- /dev/null +++ b/classic-sdk/core/src/main/java/com/microsoft/applicationinsights/TelemetryConfiguration.java @@ -0,0 +1,13 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.applicationinsights; + +// this class is required for interop with versions of the Java agent prior to 3.4.0 +class TelemetryConfiguration { + + // this method is required for interop with versions of the Java agent prior to 3.4.0 + boolean isTrackingDisabled() { + return false; + } +} diff --git a/classic-sdk/core/src/main/java/com/microsoft/applicationinsights/telemetry/ExceptionTelemetry.java b/classic-sdk/core/src/main/java/com/microsoft/applicationinsights/telemetry/ExceptionTelemetry.java index 59c9cb2b9b5..4b49d56c53b 100644 --- a/classic-sdk/core/src/main/java/com/microsoft/applicationinsights/telemetry/ExceptionTelemetry.java +++ b/classic-sdk/core/src/main/java/com/microsoft/applicationinsights/telemetry/ExceptionTelemetry.java @@ -42,6 +42,12 @@ public Throwable getThrowable() { return throwable; } + // this is required for interop with versions of the Java agent prior to 3.4.0 + @Nullable + public Exception getException() { + return throwable instanceof Exception ? (Exception) throwable : null; + } + public void setException(Throwable throwable) { setException(throwable, Integer.MAX_VALUE); } diff --git a/classic-sdk/core/src/main/java/com/microsoft/applicationinsights/telemetry/TelemetryContext.java b/classic-sdk/core/src/main/java/com/microsoft/applicationinsights/telemetry/TelemetryContext.java index 5629f668394..0e9e22e9337 100644 --- a/classic-sdk/core/src/main/java/com/microsoft/applicationinsights/telemetry/TelemetryContext.java +++ b/classic-sdk/core/src/main/java/com/microsoft/applicationinsights/telemetry/TelemetryContext.java @@ -12,6 +12,7 @@ import com.microsoft.applicationinsights.extensibility.context.UserContext; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import javax.annotation.Nullable; /** * Represents a context for sending telemetry to the Application Insights service. The context holds @@ -116,6 +117,12 @@ public void setConnectionString(String connectionString) { this.connectionString = connectionString; } + // this is required for interop with versions of the Java agent prior to 3.4.0 + @Nullable + public String getInstrumentationKey() { + return null; + } + /** Gets a dictionary of application-defined property values. */ public ConcurrentMap getProperties() { return properties; diff --git a/settings.gradle b/settings.gradle index efa87ab5fbf..59cfff4f813 100644 --- a/settings.gradle +++ b/settings.gradle @@ -82,9 +82,11 @@ include ':smoke-tests:apps:AzureSdk' include ':smoke-tests:apps:Cassandra' include ':smoke-tests:apps:ClassicSdkWebInterop2x' include ':smoke-tests:apps:ClassicSdkWebInterop3x' +include ':smoke-tests:apps:ClassicSdkWebInterop3xUsingOld3xAgent' include ':smoke-tests:apps:ConnectionStringOverrides' include ':smoke-tests:apps:CoreAndFilter2x' include ':smoke-tests:apps:CoreAndFilter3x' +include ':smoke-tests:apps:CoreAndFilter3xUsingOld3xAgent' include ':smoke-tests:apps:CustomDimensions' include ':smoke-tests:apps:CustomInstrumentation' include ':smoke-tests:apps:gRPC' diff --git a/smoke-tests/apps/ClassicSdkWebInterop3xUsingOld3xAgent/build.gradle.kts b/smoke-tests/apps/ClassicSdkWebInterop3xUsingOld3xAgent/build.gradle.kts new file mode 100644 index 00000000000..05c0d6641c0 --- /dev/null +++ b/smoke-tests/apps/ClassicSdkWebInterop3xUsingOld3xAgent/build.gradle.kts @@ -0,0 +1,12 @@ +plugins { + id("ai.smoke-test-war") +} + +dependencies { + implementation(project(":classic-sdk:web")) + implementation("org.springframework.boot:spring-boot-starter-web:2.1.7.RELEASE") { + exclude("org.springframework.boot", "spring-boot-starter-tomcat") + } + // this dependency is needed to make wildfly happy + implementation("org.reactivestreams:reactive-streams:1.0.3") +} diff --git a/smoke-tests/apps/ClassicSdkWebInterop3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SpringBootApp.java b/smoke-tests/apps/ClassicSdkWebInterop3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SpringBootApp.java new file mode 100644 index 00000000000..fe5cce0e623 --- /dev/null +++ b/smoke-tests/apps/ClassicSdkWebInterop3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SpringBootApp.java @@ -0,0 +1,16 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.applicationinsights.smoketestapp; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; + +@SpringBootApplication +public class SpringBootApp extends SpringBootServletInitializer { + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder applicationBuilder) { + return applicationBuilder.sources(SpringBootApp.class); + } +} diff --git a/smoke-tests/apps/ClassicSdkWebInterop3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/TestController.java b/smoke-tests/apps/ClassicSdkWebInterop3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/TestController.java new file mode 100644 index 00000000000..fa2be119274 --- /dev/null +++ b/smoke-tests/apps/ClassicSdkWebInterop3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/TestController.java @@ -0,0 +1,34 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.applicationinsights.smoketestapp; + +import com.microsoft.applicationinsights.telemetry.RequestTelemetry; +import com.microsoft.applicationinsights.web.internal.ThreadContext; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class TestController { + + @GetMapping("/") + public String root() { + return "OK"; + } + + @GetMapping("/test") + public String test() { + RequestTelemetry requestTelemetry = + ThreadContext.getRequestTelemetryContext().getHttpRequestTelemetry(); + requestTelemetry.getProperties().put("myattr1", "myvalue1"); + requestTelemetry.getProperties().put("myattr2", "myvalue2"); + requestTelemetry.getContext().getUser().setId("myuser"); + requestTelemetry.getContext().getSession().setId("mysessionid"); + requestTelemetry.getContext().getDevice().setOperatingSystem("mydeviceos"); + requestTelemetry.getContext().getDevice().setOperatingSystemVersion("mydeviceosversion"); + requestTelemetry.setName("myspanname"); + requestTelemetry.setSource("mysource"); + requestTelemetry.setSuccess(false); + return "OK!"; + } +} diff --git a/smoke-tests/apps/ClassicSdkWebInterop3xUsingOld3xAgent/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/ClassicSdkWebInterop3xUsingOld3xAgentTest.java b/smoke-tests/apps/ClassicSdkWebInterop3xUsingOld3xAgent/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/ClassicSdkWebInterop3xUsingOld3xAgentTest.java new file mode 100644 index 00000000000..ce0cb2e1e73 --- /dev/null +++ b/smoke-tests/apps/ClassicSdkWebInterop3xUsingOld3xAgent/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/ClassicSdkWebInterop3xUsingOld3xAgentTest.java @@ -0,0 +1,72 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.applicationinsights.smoketest; + +import static com.microsoft.applicationinsights.smoketest.WarEnvironmentValue.TOMCAT_8_JAVA_11; +import static com.microsoft.applicationinsights.smoketest.WarEnvironmentValue.TOMCAT_8_JAVA_11_OPENJ9; +import static com.microsoft.applicationinsights.smoketest.WarEnvironmentValue.TOMCAT_8_JAVA_17; +import static com.microsoft.applicationinsights.smoketest.WarEnvironmentValue.TOMCAT_8_JAVA_18; +import static com.microsoft.applicationinsights.smoketest.WarEnvironmentValue.TOMCAT_8_JAVA_19; +import static com.microsoft.applicationinsights.smoketest.WarEnvironmentValue.TOMCAT_8_JAVA_8; +import static com.microsoft.applicationinsights.smoketest.WarEnvironmentValue.TOMCAT_8_JAVA_8_OPENJ9; +import static com.microsoft.applicationinsights.smoketest.WarEnvironmentValue.WILDFLY_13_JAVA_8; +import static com.microsoft.applicationinsights.smoketest.WarEnvironmentValue.WILDFLY_13_JAVA_8_OPENJ9; +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +@UseAgent +abstract class ClassicSdkWebInterop3xUsingOld3xAgentTest { + + @RegisterExtension static final SmokeTestExtension testing = SmokeTestExtension.create(); + + @Test + @TargetUri("/test") + void doMostBasicTest() throws Exception { + Telemetry telemetry = testing.getTelemetry(0); + + assertThat(telemetry.rd.getName()).isEqualTo("myspanname"); + assertThat(telemetry.rd.getSource()).isEqualTo("mysource"); + assertThat(telemetry.rdEnvelope.getTags()).containsEntry("ai.user.id", "myuser"); + assertThat(telemetry.rdEnvelope.getTags()).containsEntry("ai.session.id", "mysessionid"); + assertThat(telemetry.rdEnvelope.getTags()).containsEntry("ai.device.os", "mydeviceos"); + assertThat(telemetry.rdEnvelope.getTags().get("ai.device.osVersion")) + .isEqualTo("mydeviceosversion"); + assertThat(telemetry.rd.getProperties()).containsEntry("myattr1", "myvalue1"); + assertThat(telemetry.rd.getProperties()).containsEntry("myattr2", "myvalue2"); + assertThat(telemetry.rd.getProperties()).hasSize(3); + assertThat(telemetry.rd.getProperties()) + .containsEntry("_MS.ProcessedByMetricExtractors", "True"); + + assertThat(telemetry.rd.getSuccess()).isFalse(); + } + + @Environment(TOMCAT_8_JAVA_8) + static class Tomcat8Java8Test extends ClassicSdkWebInterop3xUsingOld3xAgentTest {} + + @Environment(TOMCAT_8_JAVA_8_OPENJ9) + static class Tomcat8Java8OpenJ9Test extends ClassicSdkWebInterop3xUsingOld3xAgentTest {} + + @Environment(TOMCAT_8_JAVA_11) + static class Tomcat8Java11Test extends ClassicSdkWebInterop3xUsingOld3xAgentTest {} + + @Environment(TOMCAT_8_JAVA_11_OPENJ9) + static class Tomcat8Java11OpenJ9Test extends ClassicSdkWebInterop3xUsingOld3xAgentTest {} + + @Environment(TOMCAT_8_JAVA_17) + static class Tomcat8Java17Test extends ClassicSdkWebInterop3xUsingOld3xAgentTest {} + + @Environment(TOMCAT_8_JAVA_18) + static class Tomcat8Java18Test extends ClassicSdkWebInterop3xUsingOld3xAgentTest {} + + @Environment(TOMCAT_8_JAVA_19) + static class Tomcat8Java19Test extends ClassicSdkWebInterop3xUsingOld3xAgentTest {} + + @Environment(WILDFLY_13_JAVA_8) + static class Wildfly13Java8Test extends ClassicSdkWebInterop3xUsingOld3xAgentTest {} + + @Environment(WILDFLY_13_JAVA_8_OPENJ9) + static class Wildfly13Java8OpenJ9Test extends ClassicSdkWebInterop3xUsingOld3xAgentTest {} +} diff --git a/smoke-tests/apps/ClassicSdkWebInterop3xUsingOld3xAgent/src/smokeTest/resources/logback-test.xml b/smoke-tests/apps/ClassicSdkWebInterop3xUsingOld3xAgent/src/smokeTest/resources/logback-test.xml new file mode 100644 index 00000000000..0cbbecd57ce --- /dev/null +++ b/smoke-tests/apps/ClassicSdkWebInterop3xUsingOld3xAgent/src/smokeTest/resources/logback-test.xml @@ -0,0 +1,11 @@ + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger{36} - %msg%n + + + + + + diff --git a/smoke-tests/apps/CoreAndFilter2x/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/CoreAndFilter2xTest.java b/smoke-tests/apps/CoreAndFilter2x/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/CoreAndFilter2xTest.java index 4dbb6df8b91..f9eb976ffe4 100644 --- a/smoke-tests/apps/CoreAndFilter2x/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/CoreAndFilter2xTest.java +++ b/smoke-tests/apps/CoreAndFilter2x/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/CoreAndFilter2xTest.java @@ -518,7 +518,7 @@ private void validateSlowTest(int expectedDurationSeconds, String operationName) assertThat(rdEnvelope.getTags()).containsEntry("ai.operation.name", operationName); } - // old Application Insights version that do not support Java 11+ + // old 2.x Application Insights version that do not support Java 11+ @Environment(TOMCAT_8_JAVA_8) static class Tomcat8Java8Test extends CoreAndFilter2xTest {} diff --git a/smoke-tests/apps/CoreAndFilter3x/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/CoreAndFilter3xTest.java b/smoke-tests/apps/CoreAndFilter3x/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/CoreAndFilter3xTest.java index a82d776d67b..b628f7d93e1 100644 --- a/smoke-tests/apps/CoreAndFilter3x/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/CoreAndFilter3xTest.java +++ b/smoke-tests/apps/CoreAndFilter3x/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/CoreAndFilter3xTest.java @@ -236,39 +236,6 @@ void trackMetric() throws Exception { rd, rdEnvelope, mdEnvelope, "GET /CoreAndFilter3x/trackMetric"); } - @Test - @TargetUri("/trackMetricWithNamespace") - void trackMetricWithNamespace() throws Exception { - List rdList = testing.mockedIngestion.waitForItems("RequestData", 1); - List mdList = testing.mockedIngestion.waitForMetricItems("TimeToRespond", 1); - - Envelope rdEnvelope = rdList.get(0); - Envelope mdEnvelope = mdList.get(0); - - assertThat(rdEnvelope.getSampleRate()).isNull(); - assertThat(mdEnvelope.getSampleRate()).isNull(); - - RequestData rd = (RequestData) ((Data) rdEnvelope.getData()).getBaseData(); - MetricData md = (MetricData) ((Data) mdEnvelope.getData()).getBaseData(); - - List metrics = md.getMetrics(); - assertThat(metrics).hasSize(1); - DataPoint dp = metrics.get(0); - - final double expectedValue = 111222333.0; - assertThat(dp.getValue()).isEqualTo(expectedValue); - assertThat(dp.getName()).isEqualTo("TimeToRespond"); - assertThat(dp.getMetricNamespace()).isEqualTo("test"); - - assertThat(dp.getCount()).isNull(); - assertThat(dp.getMin()).isNull(); - assertThat(dp.getMax()).isNull(); - assertThat(dp.getStdDev()).isNull(); - - SmokeTestExtension.assertParentChild( - rd, rdEnvelope, mdEnvelope, "GET /CoreAndFilter3x/trackMetricWithNamespace"); - } - @Test @TargetUri("/trackTrace") void testTrackTrace() throws Exception { diff --git a/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/build.gradle.kts b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/build.gradle.kts new file mode 100644 index 00000000000..c467c5687c9 --- /dev/null +++ b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/build.gradle.kts @@ -0,0 +1,7 @@ +plugins { + id("ai.smoke-test-war") +} + +dependencies { + implementation(project(":classic-sdk:core")) +} diff --git a/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/HealthCheckServlet.java b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/HealthCheckServlet.java new file mode 100644 index 00000000000..94cfbe9c118 --- /dev/null +++ b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/HealthCheckServlet.java @@ -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; + +// this is used by the test infra in order to know when it's ok to start running the tests +@WebServlet("") +public class HealthCheckServlet extends HttpServlet { + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) {} +} diff --git a/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTestRequestSlowWithResponseTime.java b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTestRequestSlowWithResponseTime.java new file mode 100644 index 00000000000..77de6af68b6 --- /dev/null +++ b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTestRequestSlowWithResponseTime.java @@ -0,0 +1,32 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.applicationinsights.smoketestapp; + +import java.util.concurrent.TimeUnit; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@WebServlet("/requestSlow") +public class SimpleTestRequestSlowWithResponseTime extends HttpServlet { + + protected void doGet(HttpServletRequest request, HttpServletResponse response) { + int sleepTime = 25; + String customSleepTime = request.getParameter("sleeptime"); + if (customSleepTime != null) { + try { + sleepTime = Integer.parseInt(customSleepTime); + } catch (NumberFormatException e) { + System.err.printf("Invalid value for 'sleeptime': '%s'%n", customSleepTime); + } + } + try { + System.out.printf("Sleeping for %d seconds.%n", sleepTime); + TimeUnit.SECONDS.sleep(sleepTime); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + } +} diff --git a/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleThrowExceptionServlet.java b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleThrowExceptionServlet.java new file mode 100644 index 00000000000..4abe3f16c51 --- /dev/null +++ b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleThrowExceptionServlet.java @@ -0,0 +1,19 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.applicationinsights.smoketestapp; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@WebServlet("/autoExceptionWithFailedRequest") +public class SimpleThrowExceptionServlet extends HttpServlet { + + protected void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException { + throw new ServletException("This is a auto thrown exception !"); + } +} diff --git a/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackAvailabilityServlet.java b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackAvailabilityServlet.java new file mode 100644 index 00000000000..4e8928e44fc --- /dev/null +++ b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackAvailabilityServlet.java @@ -0,0 +1,30 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.applicationinsights.smoketestapp; + +import com.microsoft.applicationinsights.TelemetryClient; +import com.microsoft.applicationinsights.telemetry.AvailabilityTelemetry; +import com.microsoft.applicationinsights.telemetry.Duration; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@WebServlet("/trackAvailability") +public class SimpleTrackAvailabilityServlet extends HttpServlet { + + private final TelemetryClient client = new TelemetryClient(); + + protected void doGet(HttpServletRequest request, HttpServletResponse response) { + AvailabilityTelemetry telemetry = new AvailabilityTelemetry(); + telemetry.setId("an-id"); + telemetry.setName("a-name"); + telemetry.setDuration(new Duration(1234)); + telemetry.setSuccess(true); + telemetry.setRunLocation("a-run-location"); + telemetry.setMessage("a-message"); + + client.trackAvailability(telemetry); + } +} diff --git a/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackDependencyServlet.java b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackDependencyServlet.java new file mode 100644 index 00000000000..25419716928 --- /dev/null +++ b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackDependencyServlet.java @@ -0,0 +1,21 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.applicationinsights.smoketestapp; + +import com.microsoft.applicationinsights.TelemetryClient; +import com.microsoft.applicationinsights.telemetry.Duration; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@WebServlet("/trackDependency") +public class SimpleTrackDependencyServlet extends HttpServlet { + + private final TelemetryClient client = new TelemetryClient(); + + protected void doGet(HttpServletRequest request, HttpServletResponse response) { + client.trackDependency("DependencyTest", "commandName", new Duration(0, 0, 1, 1, 1), true); + } +} diff --git a/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackEventServlet.java b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackEventServlet.java new file mode 100644 index 00000000000..c92f1d142ec --- /dev/null +++ b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackEventServlet.java @@ -0,0 +1,38 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.applicationinsights.smoketestapp; + +import com.microsoft.applicationinsights.TelemetryClient; +import java.util.HashMap; +import java.util.Map; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@WebServlet("/trackEvent") +public class SimpleTrackEventServlet extends HttpServlet { + + private final TelemetryClient client = new TelemetryClient(); + + protected void doGet(HttpServletRequest request, HttpServletResponse response) { + + Map properties = + new HashMap() { + { + put("key", "value"); + } + }; + Map metrics = + new HashMap() { + { + put("key", 1d); + } + }; + + // Event + client.trackEvent("EventDataTest"); + client.trackEvent("EventDataPropertyTest", properties, metrics); + } +} diff --git a/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackExceptionServlet.java b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackExceptionServlet.java new file mode 100644 index 00000000000..c38c82b852c --- /dev/null +++ b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackExceptionServlet.java @@ -0,0 +1,45 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.applicationinsights.smoketestapp; + +import com.microsoft.applicationinsights.TelemetryClient; +import com.microsoft.applicationinsights.telemetry.ExceptionTelemetry; +import com.microsoft.applicationinsights.telemetry.SeverityLevel; +import java.util.HashMap; +import java.util.Map; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@WebServlet("/trackException") +public class SimpleTrackExceptionServlet extends HttpServlet { + + private final TelemetryClient client = new TelemetryClient(); + + protected void doGet(HttpServletRequest request, HttpServletResponse response) { + + Exception exception = new Exception("This is track exception."); + + Map properties = + new HashMap() { + { + put("key", "value"); + } + }; + Map metrics = + new HashMap() { + { + put("key", 1d); + } + }; + + client.trackException(exception); + client.trackException(exception, properties, metrics); + + ExceptionTelemetry et = new ExceptionTelemetry(exception); + et.setSeverityLevel(SeverityLevel.Error); + client.track(et); + } +} diff --git a/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackHttpRequestServlet.java b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackHttpRequestServlet.java new file mode 100644 index 00000000000..161a792e0ad --- /dev/null +++ b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackHttpRequestServlet.java @@ -0,0 +1,38 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.applicationinsights.smoketestapp; + +import com.microsoft.applicationinsights.TelemetryClient; +import com.microsoft.applicationinsights.telemetry.RequestTelemetry; +import java.net.MalformedURLException; +import java.util.Date; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@WebServlet("/trackHttpRequest") +public class SimpleTrackHttpRequestServlet extends HttpServlet { + + private final TelemetryClient client = new TelemetryClient(); + + protected void doGet(HttpServletRequest request, HttpServletResponse response) + throws MalformedURLException { + + // true + client.trackHttpRequest("HttpRequestDataTest", new Date(), 4711, "200", true); + + RequestTelemetry rt = new RequestTelemetry("PingTest", new Date(), 1, "200", true); + rt.setUrl("http://tempuri.org/ping"); + client.trackRequest(rt); + + // false + client.trackHttpRequest("FailedHttpRequest", new Date(), 6666, "404", false); + + RequestTelemetry rt2 = + new RequestTelemetry("FailedHttpRequest2", new Date(), 8888, "505", false); + rt2.setUrl("https://www.bingasdasdasdasda.com/"); + client.trackRequest(rt2); + } +} diff --git a/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackMetricServlet.java b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackMetricServlet.java new file mode 100644 index 00000000000..f2720b47481 --- /dev/null +++ b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackMetricServlet.java @@ -0,0 +1,20 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.applicationinsights.smoketestapp; + +import com.microsoft.applicationinsights.TelemetryClient; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@WebServlet("/trackMetric") +public class SimpleTrackMetricServlet extends HttpServlet { + + private final TelemetryClient client = new TelemetryClient(); + + protected void doGet(HttpServletRequest request, HttpServletResponse response) { + client.trackMetric("TimeToRespond", 111222333); + } +} diff --git a/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackMetricWithNamespaceServlet.java b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackMetricWithNamespaceServlet.java new file mode 100644 index 00000000000..761953118bf --- /dev/null +++ b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackMetricWithNamespaceServlet.java @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.applicationinsights.smoketestapp; + +import com.microsoft.applicationinsights.TelemetryClient; +import com.microsoft.applicationinsights.telemetry.MetricTelemetry; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@WebServlet("/trackMetricWithNamespace") +public class SimpleTrackMetricWithNamespaceServlet extends HttpServlet { + + private final TelemetryClient client = new TelemetryClient(); + + protected void doGet(HttpServletRequest request, HttpServletResponse response) { + + MetricTelemetry metricTelemetry = new MetricTelemetry(); + metricTelemetry.setName("TimeToRespond"); + metricTelemetry.setMetricNamespace("test"); + metricTelemetry.setValue(111222333); + + client.trackMetric(metricTelemetry); + } +} diff --git a/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackPageViewServlet.java b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackPageViewServlet.java new file mode 100644 index 00000000000..d2d22a380c0 --- /dev/null +++ b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackPageViewServlet.java @@ -0,0 +1,89 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.applicationinsights.smoketestapp; + +import com.microsoft.applicationinsights.TelemetryClient; +import com.microsoft.applicationinsights.telemetry.PageViewTelemetry; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@WebServlet("/trackPageView") +public class SimpleTrackPageViewServlet extends HttpServlet { + + private final TelemetryClient client = new TelemetryClient(); + + protected void doGet(HttpServletRequest request, HttpServletResponse response) { + + client.trackPageView("test-page"); + + // just making sure flush() doesn't throw exception + client.flush(); + + PageViewTelemetry pvt2 = new PageViewTelemetry("test-page-2"); + // instrumentation key set on the Telemetry is used by interop + pvt2.getContext() + .setConnectionString( + "InstrumentationKey=12341234-1234-1234-1234-123412341234;IngestionEndpoint=http://host.testcontainers.internal:6060/"); + // role name and instance set on the Telemetry is used by interop + pvt2.getContext().getCloud().setRole("role-goes-here"); + pvt2.getContext().getCloud().setRoleInstance("role-instance-goes-here"); + pvt2.getContext().getOperation().setName("operation-name-goes-here"); + pvt2.getContext().getOperation().setId("operation-id-goes-here"); + pvt2.getContext().getOperation().setParentId("operation-parent-id-goes-here"); + pvt2.getContext().getUser().setId("user-id-goes-here"); + pvt2.getContext().getUser().setAccountId("account-id-goes-here"); + pvt2.getContext().getUser().setUserAgent("user-agent-goes-here"); + // don't set device id, because then tests fail with "Telemetry from previous container" + // because they use device id to verify telemetry is from the current container + pvt2.getContext().getDevice().setOperatingSystem("os-goes-here"); + pvt2.getContext().getSession().setId("session-id-goes-here"); + pvt2.getContext().getLocation().setIp("1.2.3.4"); + pvt2.getContext().getProperties().put("a-prop", "a-value"); + pvt2.getContext().getProperties().put("another-prop", "another-value"); + pvt2.getProperties().put("key", "value"); + try { + pvt2.setTimestamp(new SimpleDateFormat("dd/MM/yyyy").parse("10/10/2010")); + } catch (ParseException e) { + throw new AssertionError(e); + } + pvt2.setDuration(123456); + client.trackPageView(pvt2); + + TelemetryClient otherClient = new TelemetryClient(); + // connection string set on the TelemetryClient is intentionally ignored by interop + otherClient + .getContext() + .setConnectionString( + "InstrumentationKey=12341234-1234-1234-1234-123412341234;IngestionEndpoint=http://host.testcontainers.internal:6060/"); + // role name and instance set on the TelemetryClient are intentionally ignored by interop + otherClient.getContext().getCloud().setRole("role-goes-here"); + otherClient.getContext().getCloud().setRoleInstance("role-instance-goes-here"); + otherClient.getContext().getOperation().setName("operation-name-goes-here"); + otherClient.getContext().getOperation().setId("operation-id-goes-here"); + otherClient.getContext().getOperation().setParentId("operation-parent-id-goes-here"); + otherClient.getContext().getUser().setId("user-id-goes-here"); + otherClient.getContext().getUser().setAccountId("account-id-goes-here"); + otherClient.getContext().getUser().setUserAgent("user-agent-goes-here"); + // don't set device id, because then tests fail with "Telemetry from previous container" + // because they use device id to verify telemetry is from the current container + otherClient.getContext().getDevice().setOperatingSystem("os-goes-here"); + otherClient.getContext().getSession().setId("session-id-goes-here"); + otherClient.getContext().getLocation().setIp("1.2.3.4"); + otherClient.getContext().getProperties().put("a-prop", "a-value"); + otherClient.getContext().getProperties().put("another-prop", "another-value"); + PageViewTelemetry pvt3 = new PageViewTelemetry("test-page-3"); + pvt3.getProperties().put("key", "value"); + try { + pvt3.setTimestamp(new SimpleDateFormat("dd/MM/yyyy").parse("10/10/2010")); + } catch (ParseException e) { + throw new AssertionError(e); + } + pvt3.setDuration(123456); + otherClient.trackPageView(pvt3); + } +} diff --git a/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackTraceServlet.java b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackTraceServlet.java new file mode 100644 index 00000000000..93f40712bc2 --- /dev/null +++ b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackTraceServlet.java @@ -0,0 +1,32 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.applicationinsights.smoketestapp; + +import com.microsoft.applicationinsights.TelemetryClient; +import com.microsoft.applicationinsights.telemetry.SeverityLevel; +import java.util.HashMap; +import java.util.Map; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@WebServlet("/trackTrace") +public class SimpleTrackTraceServlet extends HttpServlet { + + private final TelemetryClient client = new TelemetryClient(); + + protected void doGet(HttpServletRequest request, HttpServletResponse response) { + Map properties = + new HashMap() { + { + put("key", "value"); + } + }; + // Trace + client.trackTrace("This is first trace message."); + client.trackTrace("This is second trace message.", SeverityLevel.Error, null); + client.trackTrace("This is third trace message.", SeverityLevel.Information, properties); + } +} diff --git a/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SlowRequestCpuBoundServlet.java b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SlowRequestCpuBoundServlet.java new file mode 100644 index 00000000000..092fd211cba --- /dev/null +++ b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/java/com/microsoft/applicationinsights/smoketestapp/SlowRequestCpuBoundServlet.java @@ -0,0 +1,48 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.applicationinsights.smoketestapp; + +import java.math.BigDecimal; +import java.util.concurrent.ThreadLocalRandom; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@WebServlet("/slowLoop") +public class SlowRequestCpuBoundServlet extends HttpServlet { + + private static final BigDecimal MAX_VALUE = BigDecimal.valueOf(1_000); + private static final ThreadLocalRandom rand = ThreadLocalRandom.current(); + + protected void doGet(HttpServletRequest request, HttpServletResponse response) { + + long startTime = System.currentTimeMillis(); + + int responseTime = 25; + String customResponseTime = request.getParameter("responseTime"); + if (customResponseTime != null) { + try { + responseTime = Integer.parseInt(customResponseTime); + System.out.println("Custom responseTime = " + responseTime); + } catch (NumberFormatException e) { + System.err.printf("Invalid value for 'responseTime': '%s'%n", customResponseTime); + } + } + + long responseTimeMillis = responseTime * 1000L; + BigDecimal sum = BigDecimal.ONE; + int iterations = 0; + for (; durationSince(startTime) < responseTimeMillis; iterations++) { + BigDecimal operand = BigDecimal.valueOf(rand.nextDouble()).multiply(MAX_VALUE); + sum = sum.add(operand); + } + // this is just to use the value so JIT doesn't remove any computation + System.out.printf("Accumulated sum: %s (%d iterations)%n", sum.toString(), iterations); + } + + private static long durationSince(long startTime) { + return System.currentTimeMillis() - startTime; + } +} diff --git a/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/webapp/doPageView.jsp b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/webapp/doPageView.jsp new file mode 100644 index 00000000000..5a2f3ca4d23 --- /dev/null +++ b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/main/webapp/doPageView.jsp @@ -0,0 +1,17 @@ +<%@page import="com.microsoft.applicationinsights.TelemetryClient"%> + +<%@page pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%> + + + + +Simple Telemetry + + +

trackPageView Test

+ <% + TelemetryClient tc = new TelemetryClient(); + tc.trackPageView("doPageView"); + %> + + \ No newline at end of file diff --git a/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/CoreAndFilter3xUsingOld3xAgentTest.java b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/CoreAndFilter3xUsingOld3xAgentTest.java new file mode 100644 index 00000000000..b3cd0c1a7a8 --- /dev/null +++ b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/CoreAndFilter3xUsingOld3xAgentTest.java @@ -0,0 +1,551 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.applicationinsights.smoketest; + +import static com.microsoft.applicationinsights.smoketest.WarEnvironmentValue.TOMCAT_8_JAVA_11; +import static com.microsoft.applicationinsights.smoketest.WarEnvironmentValue.TOMCAT_8_JAVA_11_OPENJ9; +import static com.microsoft.applicationinsights.smoketest.WarEnvironmentValue.TOMCAT_8_JAVA_17; +import static com.microsoft.applicationinsights.smoketest.WarEnvironmentValue.TOMCAT_8_JAVA_18; +import static com.microsoft.applicationinsights.smoketest.WarEnvironmentValue.TOMCAT_8_JAVA_8; +import static com.microsoft.applicationinsights.smoketest.WarEnvironmentValue.TOMCAT_8_JAVA_8_OPENJ9; +import static com.microsoft.applicationinsights.smoketest.WarEnvironmentValue.WILDFLY_13_JAVA_8; +import static com.microsoft.applicationinsights.smoketest.WarEnvironmentValue.WILDFLY_13_JAVA_8_OPENJ9; +import static org.assertj.core.api.Assertions.assertThat; + +import com.microsoft.applicationinsights.smoketest.schemav2.Data; +import com.microsoft.applicationinsights.smoketest.schemav2.DataPoint; +import com.microsoft.applicationinsights.smoketest.schemav2.Duration; +import com.microsoft.applicationinsights.smoketest.schemav2.Envelope; +import com.microsoft.applicationinsights.smoketest.schemav2.EventData; +import com.microsoft.applicationinsights.smoketest.schemav2.ExceptionData; +import com.microsoft.applicationinsights.smoketest.schemav2.ExceptionDetails; +import com.microsoft.applicationinsights.smoketest.schemav2.MessageData; +import com.microsoft.applicationinsights.smoketest.schemav2.MetricData; +import com.microsoft.applicationinsights.smoketest.schemav2.PageViewData; +import com.microsoft.applicationinsights.smoketest.schemav2.RequestData; +import com.microsoft.applicationinsights.smoketest.schemav2.SeverityLevel; +import java.util.Comparator; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.function.Predicate; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +@UseAgent +abstract class CoreAndFilter3xUsingOld3xAgentTest { + + @RegisterExtension + static final SmokeTestExtension testing = SmokeTestExtension.builder().useOld3xAgent().build(); + + @Test + @TargetUri("/trackDependency") + void trackDependency() throws Exception { + Telemetry telemetry = testing.getTelemetry(1); + + Duration expectedDuration = new Duration(0, 0, 1, 1, 1); + + assertThat(telemetry.rdd1.getName()).isEqualTo("DependencyTest"); + assertThat(telemetry.rdd1.getData()).isEqualTo("commandName"); + assertThat(telemetry.rdd1.getType()).isNull(); + assertThat(telemetry.rdd1.getTarget()).isNull(); + assertThat(telemetry.rdd1.getProperties()).isEmpty(); + assertThat(telemetry.rdd1.getSuccess()).isTrue(); + + assertThat(telemetry.rdd1.getDuration()).isEqualTo(expectedDuration); + + SmokeTestExtension.assertParentChild( + telemetry.rd, + telemetry.rdEnvelope, + telemetry.rddEnvelope1, + "GET /CoreAndFilter3xUsingOld3xAgent/trackDependency"); + } + + @Test + @TargetUri("/trackEvent") + void testTrackEvent() throws Exception { + List rdList = testing.mockedIngestion.waitForItems("RequestData", 1); + + Envelope rdEnvelope = rdList.get(0); + String operationId = rdEnvelope.getTags().get("ai.operation.id"); + List edList = + testing.mockedIngestion.waitForItemsInOperation("EventData", 2, operationId); + + Envelope edEnvelope1 = edList.get(0); + Envelope edEnvelope2 = edList.get(1); + + assertThat(rdEnvelope.getSampleRate()).isNull(); + assertThat(edEnvelope1.getSampleRate()).isNull(); + assertThat(edEnvelope2.getSampleRate()).isNull(); + + RequestData rd = (RequestData) ((Data) rdEnvelope.getData()).getBaseData(); + + List events = testing.mockedIngestion.getTelemetryDataByTypeInRequest("EventData"); + events.sort(Comparator.comparing(EventData::getName)); + + EventData ed1 = events.get(0); + EventData ed2 = events.get(1); + + assertThat(ed1.getName()).isEqualTo("EventDataPropertyTest"); + assertThat(ed1.getProperties()).containsEntry("key", "value"); + assertThat(ed1.getMeasurements()).containsEntry("key", 1.0); + + assertThat(ed2.getName()).isEqualTo("EventDataTest"); + + SmokeTestExtension.assertParentChild( + rd, rdEnvelope, edEnvelope1, "GET /CoreAndFilter3xUsingOld3xAgent/trackEvent"); + SmokeTestExtension.assertParentChild( + rd, rdEnvelope, edEnvelope2, "GET /CoreAndFilter3xUsingOld3xAgent/trackEvent"); + } + + @Test + @TargetUri("/trackException") + void testTrackException() throws Exception { + List rdList = testing.mockedIngestion.waitForItems("RequestData", 1); + + Envelope rdEnvelope = rdList.get(0); + String operationId = rdEnvelope.getTags().get("ai.operation.id"); + List edList = + testing.mockedIngestion.waitForItemsInOperation("ExceptionData", 3, operationId); + assertThat(testing.mockedIngestion.getCountForType("EventData")).isZero(); + + Envelope edEnvelope1 = edList.get(0); + Envelope edEnvelope2 = edList.get(1); + Envelope edEnvelope3 = edList.get(2); + + assertThat(rdEnvelope.getSampleRate()).isNull(); + assertThat(edEnvelope1.getSampleRate()).isNull(); + assertThat(edEnvelope2.getSampleRate()).isNull(); + assertThat(edEnvelope3.getSampleRate()).isNull(); + + RequestData rd = (RequestData) ((Data) rdEnvelope.getData()).getBaseData(); + + final String expectedName = "This is track exception."; + final String expectedProperties = "value"; + final Double expectedMetrice = 1d; + + List exceptions = + testing.mockedIngestion.getTelemetryDataByTypeInRequest("ExceptionData"); + assertThat(exceptions) + .anySatisfy( + e -> + assertThat(e.getExceptions()) + .extracting(ExceptionDetails::getMessage) + .contains(expectedName)); + assertThat(exceptions) + .anySatisfy( + e -> { + assertThat(e.getExceptions()) + .extracting(ExceptionDetails::getMessage) + .contains(expectedName); + assertThat(e.getProperties()).containsEntry("key", expectedProperties); + assertThat(e.getMeasurements()).containsEntry("key", expectedMetrice); + }); + assertThat(exceptions) + .anySatisfy( + e -> { + assertThat(e.getExceptions()) + .extracting(ExceptionDetails::getMessage) + .contains(expectedName); + assertThat(e.getSeverityLevel()).isEqualTo(SeverityLevel.ERROR); + }); + + SmokeTestExtension.assertParentChild( + rd, rdEnvelope, edEnvelope1, "GET /CoreAndFilter3xUsingOld3xAgent/trackException"); + SmokeTestExtension.assertParentChild( + rd, rdEnvelope, edEnvelope2, "GET /CoreAndFilter3xUsingOld3xAgent/trackException"); + SmokeTestExtension.assertParentChild( + rd, rdEnvelope, edEnvelope3, "GET /CoreAndFilter3xUsingOld3xAgent/trackException"); + } + + @Test + @TargetUri("/trackHttpRequest") + void testHttpRequest() throws Exception { + testing.mockedIngestion.waitForItems("RequestData", 5); + + // TODO get HttpRequest data envelope and verify value + List requests = testing.mockedIngestion.getTelemetryDataByType("RequestData"); + + assertThat(requests) + .anySatisfy( + r -> { + assertThat(r.getName()).isEqualTo("HttpRequestDataTest"); + assertThat(r.getResponseCode()).isEqualTo("200"); + assertThat(r.getDuration()).isEqualTo(new Duration(4711)); + assertThat(r.getSuccess()).isTrue(); + }); + assertThat(requests) + .anySatisfy( + r -> { + assertThat(r.getName()).isEqualTo("PingTest"); + assertThat(r.getResponseCode()).isEqualTo("200"); + assertThat(r.getDuration()).isEqualTo(new Duration(1)); + assertThat(r.getSuccess()).isTrue(); + assertThat(r.getUrl()).isEqualTo("http://tempuri.org/ping"); + }); + assertThat(requests) + .anySatisfy( + r -> { + assertThat(r.getName()).isEqualTo("FailedHttpRequest"); + assertThat(r.getResponseCode()).isEqualTo("404"); + assertThat(r.getDuration()).isEqualTo(new Duration(6666)); + assertThat(r.getSuccess()).isFalse(); + }); + assertThat(requests) + .anySatisfy( + r -> { + assertThat(r.getName()).isEqualTo("FailedHttpRequest2"); + assertThat(r.getResponseCode()).isEqualTo("505"); + assertThat(r.getDuration()).isEqualTo(new Duration(8888)); + assertThat(r.getSuccess()).isFalse(); + assertThat(r.getUrl()).isEqualTo("https://www.bingasdasdasdasda.com/"); + }); + } + + @Test + @TargetUri("/trackMetric") + void trackMetric() throws Exception { + List rdList = testing.mockedIngestion.waitForItems("RequestData", 1); + List mdList = testing.mockedIngestion.waitForMetricItems("TimeToRespond", 1); + + Envelope rdEnvelope = rdList.get(0); + Envelope mdEnvelope = mdList.get(0); + + assertThat(rdEnvelope.getSampleRate()).isNull(); + assertThat(mdEnvelope.getSampleRate()).isNull(); + + RequestData rd = (RequestData) ((Data) rdEnvelope.getData()).getBaseData(); + MetricData md = (MetricData) ((Data) mdEnvelope.getData()).getBaseData(); + + List metrics = md.getMetrics(); + assertThat(metrics).hasSize(1); + DataPoint dp = metrics.get(0); + + final double expectedValue = 111222333.0; + assertThat(dp.getValue()).isEqualTo(expectedValue); + assertThat(dp.getName()).isEqualTo("TimeToRespond"); + assertThat(dp.getMetricNamespace()).isNull(); + + assertThat(dp.getCount()).isNull(); + assertThat(dp.getMin()).isNull(); + assertThat(dp.getMax()).isNull(); + assertThat(dp.getStdDev()).isNull(); + + SmokeTestExtension.assertParentChild( + rd, rdEnvelope, mdEnvelope, "GET /CoreAndFilter3xUsingOld3xAgent/trackMetric"); + } + + @Test + @TargetUri("/trackTrace") + void testTrackTrace() throws Exception { + List rdList = testing.mockedIngestion.waitForItems("RequestData", 1); + + Envelope rdEnvelope = rdList.get(0); + String operationId = rdEnvelope.getTags().get("ai.operation.id"); + List mdList = testing.mockedIngestion.waitForMessageItemsInRequest(3, operationId); + + Envelope mdEnvelope1 = mdList.get(0); + Envelope mdEnvelope2 = mdList.get(1); + Envelope mdEnvelope3 = mdList.get(2); + + assertThat(rdEnvelope.getSampleRate()).isNull(); + assertThat(mdEnvelope1.getSampleRate()).isNull(); + assertThat(mdEnvelope2.getSampleRate()).isNull(); + assertThat(mdEnvelope3.getSampleRate()).isNull(); + + RequestData rd = (RequestData) ((Data) rdEnvelope.getData()).getBaseData(); + + List messages = testing.mockedIngestion.getMessageDataInRequest(); + + assertThat(messages) + .anySatisfy(m -> assertThat(m.getMessage()).isEqualTo("This is first trace message.")); + + assertThat(messages) + .anySatisfy( + m -> { + assertThat(m.getMessage()).isEqualTo("This is second trace message."); + assertThat(m.getSeverityLevel()).isEqualTo(SeverityLevel.ERROR); + }); + + assertThat(messages) + .anySatisfy( + m -> { + assertThat(m.getMessage()).isEqualTo("This is third trace message."); + assertThat(m.getSeverityLevel()).isEqualTo(SeverityLevel.INFORMATION); + assertThat(m.getProperties()).containsEntry("key", "value"); + }); + + SmokeTestExtension.assertParentChild( + rd, rdEnvelope, mdEnvelope1, "GET /CoreAndFilter3xUsingOld3xAgent/trackTrace"); + SmokeTestExtension.assertParentChild( + rd, rdEnvelope, mdEnvelope2, "GET /CoreAndFilter3xUsingOld3xAgent/trackTrace"); + SmokeTestExtension.assertParentChild( + rd, rdEnvelope, mdEnvelope3, "GET /CoreAndFilter3xUsingOld3xAgent/trackTrace"); + } + + @Test + @TargetUri("/trackPageView") + void testTrackPageView() throws Exception { + List rdList = testing.mockedIngestion.waitForItems("RequestData", 1); + + Envelope rdEnvelope = rdList.get(0); + List pvdList = testing.mockedIngestion.waitForItems("PageViewData", 3); + assertThat(testing.mockedIngestion.getCountForType("EventData")).isZero(); + + RequestData rd = (RequestData) ((Data) rdEnvelope.getData()).getBaseData(); + + Envelope pvdEnvelope1 = null; + Envelope pvdEnvelope2 = null; + Envelope pvdEnvelope3 = null; + + for (Envelope pvdEnvelope : pvdList) { + PageViewData pv = (PageViewData) ((Data) pvdEnvelope.getData()).getBaseData(); + if (pv.getName().equals("test-page")) { + pvdEnvelope1 = pvdEnvelope; + } else if (pv.getName().equals("test-page-2")) { + pvdEnvelope2 = pvdEnvelope; + } else if (pv.getName().equals("test-page-3")) { + pvdEnvelope3 = pvdEnvelope; + } else { + throw new AssertionError("Unexpected page view: " + pv.getName()); + } + } + + assertThat(rdEnvelope.getSampleRate()).isNull(); + assertThat(pvdEnvelope1.getSampleRate()).isNull(); + assertThat(pvdEnvelope2.getSampleRate()).isNull(); + assertThat(pvdEnvelope3.getSampleRate()).isNull(); + + PageViewData pv1 = (PageViewData) ((Data) pvdEnvelope1.getData()).getBaseData(); + PageViewData pv2 = (PageViewData) ((Data) pvdEnvelope2.getData()).getBaseData(); + PageViewData pv3 = (PageViewData) ((Data) pvdEnvelope3.getData()).getBaseData(); + + assertThat(pv1).isNotNull(); + assertThat(pv1.getDuration()).isEqualTo(new Duration(0)); + // checking that instrumentation key, cloud role name, cloud role instance, and sdk version are + // from the agent + assertThat(pvdEnvelope1.getIKey()).isEqualTo("00000000-0000-0000-0000-0FEEDDADBEEF"); + assertThat(pvdEnvelope1.getTags()).containsEntry("ai.cloud.role", "testrolename"); + assertThat(pvdEnvelope1.getTags()).containsEntry("ai.cloud.roleInstance", "testroleinstance"); + assertThat(pvdEnvelope1.getTags()) + .hasEntrySatisfying("ai.internal.sdkVersion", v -> assertThat(v).startsWith("java:3.")); + + assertThat(pv2).isNotNull(); + assertThat(pv2.getDuration()).isEqualTo(new Duration(123456)); + assertThat(pvdEnvelope2.getTime()).isEqualTo("2010-10-10T00:00:00Z"); + assertThat(pv2.getProperties()).containsEntry("key", "value"); + assertThat(pv2.getProperties()).containsEntry("a-prop", "a-value"); + assertThat(pv2.getProperties()).containsEntry("another-prop", "another-value"); + // operation name is verified below in assertParentChild() + assertThat(pvdEnvelope2.getTags()).containsEntry("ai.user.id", "user-id-goes-here"); + assertThat(pvdEnvelope2.getTags()).containsEntry("ai.user.accountId", "account-id-goes-here"); + assertThat(pvdEnvelope2.getTags()).containsEntry("ai.user.userAgent", "user-agent-goes-here"); + assertThat(pvdEnvelope2.getTags()).containsEntry("ai.device.os", "os-goes-here"); + assertThat(pvdEnvelope2.getTags()).containsEntry("ai.session.id", "session-id-goes-here"); + assertThat(pvdEnvelope2.getTags()).containsEntry("ai.location.ip", "1.2.3.4"); + // checking that instrumentation key, cloud role name and cloud role instance are overridden + // 3.4 Classic SDK doesn't have setInstrumentationKey(), and prior Agent doesn't have support + // for setConnectionString() + // assertThat(pvdEnvelope2.getIKey()).isEqualTo("12341234-1234-1234-1234-123412341234"); + assertThat(pvdEnvelope2.getTags()).containsEntry("ai.cloud.role", "role-goes-here"); + assertThat(pvdEnvelope2.getTags().get("ai.cloud.roleInstance")) + .isEqualTo("role-instance-goes-here"); + // checking that sdk version is from the agent + assertThat(pvdEnvelope2.getTags()) + .hasEntrySatisfying("ai.internal.sdkVersion", v -> assertThat(v).startsWith("java:3.")); + + assertThat(pv3).isNotNull(); + assertThat(pv3.getDuration()).isEqualTo(new Duration(123456)); + assertThat(pvdEnvelope3.getTime()).isEqualTo("2010-10-10T00:00:00Z"); + assertThat(pv3.getProperties()).containsEntry("key", "value"); + assertThat(pv3.getProperties()).containsEntry("a-prop", "a-value"); + assertThat(pv3.getProperties()).containsEntry("another-prop", "another-value"); + // operation name is verified below in assertParentChild() + assertThat(pvdEnvelope3.getTags()).containsEntry("ai.user.id", "user-id-goes-here"); + assertThat(pvdEnvelope3.getTags()).containsEntry("ai.user.accountId", "account-id-goes-here"); + assertThat(pvdEnvelope3.getTags()).containsEntry("ai.user.userAgent", "user-agent-goes-here"); + assertThat(pvdEnvelope3.getTags()).containsEntry("ai.device.os", "os-goes-here"); + assertThat(pvdEnvelope3.getTags()).containsEntry("ai.session.id", "session-id-goes-here"); + assertThat(pvdEnvelope3.getTags()).containsEntry("ai.location.ip", "1.2.3.4"); + // checking that instrumentation key, cloud role name and cloud role instance are from the agent + assertThat(pvdEnvelope3.getIKey()).isEqualTo("00000000-0000-0000-0000-0FEEDDADBEEF"); + assertThat(pvdEnvelope3.getTags()).containsEntry("ai.cloud.role", "testrolename"); + assertThat(pvdEnvelope3.getTags()).containsEntry("ai.cloud.roleInstance", "testroleinstance"); + // checking that sdk version is from the agent + assertThat(pvdEnvelope3.getTags()) + .hasEntrySatisfying("ai.internal.sdkVersion", v -> assertThat(v).startsWith("java:3.")); + + SmokeTestExtension.assertParentChild( + rd, rdEnvelope, pvdEnvelope1, "GET /CoreAndFilter3xUsingOld3xAgent/trackPageView"); + + assertThat(pvdEnvelope2.getTags()).containsEntry("ai.operation.id", "operation-id-goes-here"); + assertThat(pvdEnvelope2.getTags()) + .containsEntry("ai.operation.parentId", "operation-parent-id-goes-here"); + assertThat(pvdEnvelope2.getTags().get("ai.operation.name")) + .isEqualTo("operation-name-goes-here"); + + assertThat(pvdEnvelope3.getTags()).containsEntry("ai.operation.id", "operation-id-goes-here"); + assertThat(pvdEnvelope3.getTags()) + .containsEntry("ai.operation.parentId", "operation-parent-id-goes-here"); + assertThat(pvdEnvelope3.getTags().get("ai.operation.name")) + .isEqualTo("operation-name-goes-here"); + } + + @Test + @TargetUri("/doPageView.jsp") + void testTrackPageViewJsp() throws Exception { + List rdList = testing.mockedIngestion.waitForItems("RequestData", 1); + + Envelope rdEnvelope = rdList.get(0); + String operationId = rdEnvelope.getTags().get("ai.operation.id"); + List pvdList = + testing.mockedIngestion.waitForItemsInOperation("PageViewData", 1, operationId); + assertThat(testing.mockedIngestion.getCountForType("EventData")).isZero(); + + Envelope pvdEnvelope = pvdList.get(0); + + assertThat(rdEnvelope.getSampleRate()).isNull(); + assertThat(pvdEnvelope.getSampleRate()).isNull(); + + RequestData rd = (RequestData) ((Data) rdEnvelope.getData()).getBaseData(); + + PageViewData pv = (PageViewData) ((Data) pvdEnvelope.getData()).getBaseData(); + assertThat(pv.getName()).isEqualTo("doPageView"); + assertThat(pv.getDuration()).isEqualTo(new Duration(0)); + + SmokeTestExtension.assertParentChild( + rd, rdEnvelope, pvdEnvelope, "GET /CoreAndFilter3xUsingOld3xAgent/doPageView.jsp"); + } + + @Test + @TargetUri("/autoFailedRequestWithResultCode") + void testAutoFailedRequestWithResultCode() throws Exception { + List rdList = testing.mockedIngestion.waitForItems("RequestData", 1); + + Envelope rdEnvelope = rdList.get(0); + + assertThat(rdEnvelope.getSampleRate()).isNull(); + + RequestData rd = (RequestData) ((Data) rdEnvelope.getData()).getBaseData(); + + assertThat(rd.getSuccess()).isFalse(); + assertThat(rd.getResponseCode()).isEqualTo("404"); + + assertThat(rdEnvelope.getTags()) + .containsEntry("ai.operation.name", "GET /CoreAndFilter3xUsingOld3xAgent/*"); + } + + @Test + @TargetUri("/requestSlow?sleeptime=20") + void testRequestSlowWithResponseTime() throws Exception { + validateSlowTest(20, "GET /CoreAndFilter3xUsingOld3xAgent/requestSlow"); + } + + @Test + @TargetUri("/slowLoop?responseTime=20") + void testSlowRequestUsingCpuBoundLoop() throws Exception { + validateSlowTest(20, "GET /CoreAndFilter3xUsingOld3xAgent/slowLoop"); + } + + @Test + @TargetUri("/autoExceptionWithFailedRequest") + void testAutoExceptionWithFailedRequest() throws Exception { + List rdList = testing.mockedIngestion.waitForItems("RequestData", 1); + + Envelope rdEnvelope = rdList.get(0); + String operationId = rdEnvelope.getTags().get("ai.operation.id"); + List edList = + testing.mockedIngestion.waitForItems( + new Predicate() { + @Override + public boolean test(Envelope input) { + if (!"ExceptionData".equals(input.getData().getBaseType())) { + return false; + } + if (!operationId.equals(input.getTags().get("ai.operation.id"))) { + return false; + } + // lastly, filter out ExceptionData captured from tomcat logger + ExceptionData data = (ExceptionData) ((Data) input.getData()).getBaseData(); + return !data.getProperties().containsKey("LoggerName"); + } + }, + 1, + 10, + TimeUnit.SECONDS); + + Envelope edEnvelope = edList.get(0); + + assertThat(rdEnvelope.getSampleRate()).isNull(); + assertThat(edEnvelope.getSampleRate()).isNull(); + + RequestData rd = (RequestData) ((Data) rdEnvelope.getData()).getBaseData(); + ExceptionData ed = (ExceptionData) ((Data) edEnvelope.getData()).getBaseData(); + + assertThat(rd.getSuccess()).isFalse(); + + ExceptionDetails details = getExceptionDetails(ed); + assertThat(details.getMessage()).isEqualTo("This is a auto thrown exception !"); + } + + @Test + @TargetUri("/index.jsp") + void testRequestJsp() throws Exception { + testing.mockedIngestion.waitForItems("RequestData", 1); + } + + private static ExceptionDetails getExceptionDetails(ExceptionData exceptionData) { + List details = exceptionData.getExceptions(); + return details.get(0); + } + + private void validateSlowTest(int expectedDurationSeconds, String operationName) + throws Exception { + List rdList = testing.mockedIngestion.waitForItems("RequestData", 1); + + Envelope rdEnvelope = rdList.get(0); + + assertThat(rdEnvelope.getSampleRate()).isNull(); + + RequestData rd = (RequestData) ((Data) rdEnvelope.getData()).getBaseData(); + + long actual = rd.getDuration().getTotalMilliseconds(); + long expected = (new Duration(0, 0, 0, expectedDurationSeconds, 0).getTotalMilliseconds()); + long tolerance = 2 * 1000; // 2 seconds + + long min = expected - tolerance; + long max = expected + tolerance; + + System.out.printf("Slow response time: expected=%d, actual=%d%n", expected, actual); + assertThat(actual).isGreaterThanOrEqualTo(min); + assertThat(actual).isLessThan(max); + + assertThat(rdEnvelope.getTags()).containsEntry("ai.operation.name", operationName); + } + + @Environment(TOMCAT_8_JAVA_8) + static class Tomcat8Java8Test extends CoreAndFilter3xUsingOld3xAgentTest {} + + @Environment(TOMCAT_8_JAVA_8_OPENJ9) + static class Tomcat8Java8OpenJ9Test extends CoreAndFilter3xUsingOld3xAgentTest {} + + @Environment(TOMCAT_8_JAVA_11) + static class Tomcat8Java11Test extends CoreAndFilter3xUsingOld3xAgentTest {} + + @Environment(TOMCAT_8_JAVA_11_OPENJ9) + static class Tomcat8Java11OpenJ9Test extends CoreAndFilter3xUsingOld3xAgentTest {} + + @Environment(TOMCAT_8_JAVA_17) + static class Tomcat8Java17Test extends CoreAndFilter3xUsingOld3xAgentTest {} + + @Environment(TOMCAT_8_JAVA_18) + static class Tomcat8Java18Test extends CoreAndFilter3xUsingOld3xAgentTest {} + + // note: old 3.x agents don't support Java 19 + + @Environment(WILDFLY_13_JAVA_8) + static class Wildfly13Java8Test extends CoreAndFilter3xUsingOld3xAgentTest {} + + @Environment(WILDFLY_13_JAVA_8_OPENJ9) + static class Wildfly13Java8OpenJ9Test extends CoreAndFilter3xUsingOld3xAgentTest {} +} diff --git a/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/smokeTest/resources/logback-test.xml b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/smokeTest/resources/logback-test.xml new file mode 100644 index 00000000000..0cbbecd57ce --- /dev/null +++ b/smoke-tests/apps/CoreAndFilter3xUsingOld3xAgent/src/smokeTest/resources/logback-test.xml @@ -0,0 +1,11 @@ + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger{36} - %msg%n + + + + + + diff --git a/smoke-tests/framework/src/main/java/com/microsoft/applicationinsights/smoketest/SmokeTestExtension.java b/smoke-tests/framework/src/main/java/com/microsoft/applicationinsights/smoketest/SmokeTestExtension.java index 009b2a02335..e706d7eb421 100644 --- a/smoke-tests/framework/src/main/java/com/microsoft/applicationinsights/smoketest/SmokeTestExtension.java +++ b/smoke-tests/framework/src/main/java/com/microsoft/applicationinsights/smoketest/SmokeTestExtension.java @@ -65,8 +65,6 @@ public class SmokeTestExtension private static final String FAKE_INGESTION_ENDPOINT = "http://host.testcontainers.internal:6060/"; private static final File appFile = new File(System.getProperty("ai.smoke-test.test-app-file")); - private static final File javaagentFile = - new File(System.getProperty("ai.smoke-test.javaagent-file")); // TODO (trask) make private and expose methods on AiSmokeTest(?) protected final MockedAppInsightsIngestionServer mockedIngestion = @@ -91,6 +89,8 @@ public class SmokeTestExtension private final boolean skipHealthCheck; private final boolean readOnly; private final boolean usesGlobalIngestionEndpoint; + private final boolean useOld3xAgent; + private final File javaagentFile; public static SmokeTestExtension create() { return builder().build(); @@ -105,12 +105,18 @@ public static SmokeTestExtensionBuilder builder() { @Nullable String dependencyContainerEnvVarName, boolean usesGlobalIngestionEndpoint, boolean skipHealthCheck, - boolean readOnly) { + boolean readOnly, + boolean useOld3xAgent) { this.skipHealthCheck = skipHealthCheck; this.readOnly = readOnly; this.dependencyContainer = dependencyContainer; this.dependencyContainerEnvVarName = dependencyContainerEnvVarName; this.usesGlobalIngestionEndpoint = usesGlobalIngestionEndpoint; + this.useOld3xAgent = useOld3xAgent; + + String javaagentPathSystemProperty = + useOld3xAgent ? "ai.smoke-test.old-3x-javaagent-file" : "ai.smoke-test.javaagent-file"; + javaagentFile = new File(System.getProperty(javaagentPathSystemProperty)); } @Override @@ -233,17 +239,19 @@ private void waitForHealthCheckTelemetry(String contextRootUrl) }, TELEMETRY_RECEIVE_TIMEOUT_SECONDS, TimeUnit.SECONDS); - mockedIngestion.waitForItem( - input -> { - if (!"MetricData".equals(input.getData().getBaseType())) { - return false; - } - MetricData data = (MetricData) ((Data) input.getData()).getBaseData(); - String metricId = data.getProperties().get("_MS.MetricId"); - return metricId != null && metricId.equals("requests/duration"); - }, - 10, // metrics should come in pretty quickly after spans - TimeUnit.SECONDS); + if (!useOld3xAgent) { + mockedIngestion.waitForItem( + input -> { + if (!"MetricData".equals(input.getData().getBaseType())) { + return false; + } + MetricData data = (MetricData) ((Data) input.getData()).getBaseData(); + String metricId = data.getProperties().get("_MS.MetricId"); + return metricId != null && metricId.equals("requests/duration"); + }, + 10, // metrics should come in pretty quickly after spans + TimeUnit.SECONDS); + } System.out.printf( "Received request telemetry after %.3f seconds...%n", receivedTelemetryTimer.elapsed(TimeUnit.MILLISECONDS) / 1000.0); diff --git a/smoke-tests/framework/src/main/java/com/microsoft/applicationinsights/smoketest/SmokeTestExtensionBuilder.java b/smoke-tests/framework/src/main/java/com/microsoft/applicationinsights/smoketest/SmokeTestExtensionBuilder.java index 870f3bf5b0e..e4d20af2cdc 100644 --- a/smoke-tests/framework/src/main/java/com/microsoft/applicationinsights/smoketest/SmokeTestExtensionBuilder.java +++ b/smoke-tests/framework/src/main/java/com/microsoft/applicationinsights/smoketest/SmokeTestExtensionBuilder.java @@ -12,6 +12,7 @@ public class SmokeTestExtensionBuilder { private boolean usesGlobalIngestionEndpoint; private boolean skipHealthCheck; private boolean readOnly; + private boolean useOld3xAgent; public SmokeTestExtensionBuilder setDependencyContainer( String envVarName, GenericContainer container) { @@ -35,12 +36,18 @@ public SmokeTestExtensionBuilder setReadOnly(boolean readOnly) { return this; } + public SmokeTestExtensionBuilder useOld3xAgent() { + useOld3xAgent = true; + return this; + } + public SmokeTestExtension build() { return new SmokeTestExtension( dependencyContainer, dependencyContainerEnvVarName, usesGlobalIngestionEndpoint, skipHealthCheck, - readOnly); + readOnly, + useOld3xAgent); } }