From 11311ef1d8a7d3359d51be0a5deb95dc2528d155 Mon Sep 17 00:00:00 2001 From: Tobias Stadler Date: Wed, 13 Oct 2021 11:55:22 +0200 Subject: [PATCH 1/3] Added an integration test for retrieving the service name/version from the main jars manifest --- integration-tests/main-app-test/pom.xml | 51 ++++++++++++++++ .../main/java/co/elastic/apm/test/Main.java | 28 +++++++++ .../java/co/elastic/apm/test/ServiceIT.java | 60 +++++++++++++++++++ integration-tests/pom.xml | 1 + 4 files changed, 140 insertions(+) create mode 100644 integration-tests/main-app-test/pom.xml create mode 100644 integration-tests/main-app-test/src/main/java/co/elastic/apm/test/Main.java create mode 100644 integration-tests/main-app-test/src/test/java/co/elastic/apm/test/ServiceIT.java diff --git a/integration-tests/main-app-test/pom.xml b/integration-tests/main-app-test/pom.xml new file mode 100644 index 0000000000..2e72bdce17 --- /dev/null +++ b/integration-tests/main-app-test/pom.xml @@ -0,0 +1,51 @@ + + + 4.0.0 + + + integration-tests + co.elastic.apm + 1.26.1-SNAPSHOT + + + main-app-test + ${project.groupId}:${project.artifactId} + + + ${project.basedir}/../.. + + + + + org.slf4j + slf4j-simple + ${version.slf4j} + test + + + org.testcontainers + testcontainers + ${version.testcontainers} + test + + + + + ${project.artifactId} + + + maven-jar-plugin + + + + co.elastic.apm.test.Main + My Service Name + My Service Version + + + + + + + diff --git a/integration-tests/main-app-test/src/main/java/co/elastic/apm/test/Main.java b/integration-tests/main-app-test/src/main/java/co/elastic/apm/test/Main.java new file mode 100644 index 0000000000..5c91cfb7ef --- /dev/null +++ b/integration-tests/main-app-test/src/main/java/co/elastic/apm/test/Main.java @@ -0,0 +1,28 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.test; + +public class Main { + + public static void main(String[] args) throws InterruptedException { + if (System.getProperty("java.specification.version").equals("1.7")) { + Thread.sleep(5000); // on Java 7 the agent initialization is delayed by at least 3000 ms + } + } +} diff --git a/integration-tests/main-app-test/src/test/java/co/elastic/apm/test/ServiceIT.java b/integration-tests/main-app-test/src/test/java/co/elastic/apm/test/ServiceIT.java new file mode 100644 index 0000000000..9623c0eb66 --- /dev/null +++ b/integration-tests/main-app-test/src/test/java/co/elastic/apm/test/ServiceIT.java @@ -0,0 +1,60 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.test; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.utility.DockerImageName; + +import java.io.File; +import java.io.FileFilter; +import java.util.Arrays; + +import static org.assertj.core.api.Assertions.assertThat; + +class ServiceIT { + + @ParameterizedTest + @ValueSource(strings = {"openjdk:7", "openjdk:8", "openjdk:11", "openjdk:17"}) + void testServiceNameAndVersionFromManifest(String image) { + GenericContainer app = new GenericContainer<>(DockerImageName.parse(image)) + .withFileSystemBind(getAgentJar(), "/tmp/elastic-apm-agent.jar") + .withFileSystemBind("target/main-app-test.jar", "/tmp/main-app.jar") + .withCommand("java -javaagent:/tmp/elastic-apm-agent.jar -jar /tmp/main-app.jar") + .waitingFor(Wait.forLogMessage(".* Starting Elastic APM .*", 1)); + app.start(); + + try { + assertThat(app.getLogs()).contains(" as My Service Name (My Service Version) on "); + } finally { + app.stop(); + } + } + + private static String getAgentJar() { + File buildDir = new File("../../elastic-apm-agent/target/"); + FileFilter fileFilter = file -> file.getName().matches("elastic-apm-agent-\\d\\.\\d+\\.\\d+(\\.RC\\d+)?(-SNAPSHOT)?.jar"); + return Arrays.stream(buildDir.listFiles(fileFilter)) + .findFirst() + .map(File::getAbsolutePath) + .orElse(null); + } +} diff --git a/integration-tests/pom.xml b/integration-tests/pom.xml index c79c189690..88f0eb5ca3 100644 --- a/integration-tests/pom.xml +++ b/integration-tests/pom.xml @@ -22,6 +22,7 @@ cdi-app external-plugin-test runtime-attach + main-app-test From 261e9107ec46c4231b10a7121261d03e8fa1cd8a Mon Sep 17 00:00:00 2001 From: Tobias Stadler Date: Wed, 1 Dec 2021 13:47:16 +0100 Subject: [PATCH 2/3] Updated version --- integration-tests/main-app-test/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration-tests/main-app-test/pom.xml b/integration-tests/main-app-test/pom.xml index cacaa31be7..7b9ddc4468 100644 --- a/integration-tests/main-app-test/pom.xml +++ b/integration-tests/main-app-test/pom.xml @@ -6,7 +6,7 @@ integration-tests co.elastic.apm - 1.27.1-SNAPSHOT + 1.27.2-SNAPSHOT main-app-test From 4c83ac201477587d5a1220f7754b3eb4359c87a8 Mon Sep 17 00:00:00 2001 From: Felix Barnsteiner Date: Thu, 27 Jan 2022 08:43:18 +0100 Subject: [PATCH 3/3] Remove Java 7 test - makes the test simpler - The Java 7 test doesn't run properly on Apple Silicon - Testing 8, 11, and 17 gives me enough confidence that this works --- .../src/main/java/co/elastic/apm/test/Main.java | 3 --- .../src/test/java/co/elastic/apm/test/ServiceIT.java | 5 +++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/integration-tests/main-app-test/src/main/java/co/elastic/apm/test/Main.java b/integration-tests/main-app-test/src/main/java/co/elastic/apm/test/Main.java index 5c91cfb7ef..649c71543d 100644 --- a/integration-tests/main-app-test/src/main/java/co/elastic/apm/test/Main.java +++ b/integration-tests/main-app-test/src/main/java/co/elastic/apm/test/Main.java @@ -21,8 +21,5 @@ public class Main { public static void main(String[] args) throws InterruptedException { - if (System.getProperty("java.specification.version").equals("1.7")) { - Thread.sleep(5000); // on Java 7 the agent initialization is delayed by at least 3000 ms - } } } diff --git a/integration-tests/main-app-test/src/test/java/co/elastic/apm/test/ServiceIT.java b/integration-tests/main-app-test/src/test/java/co/elastic/apm/test/ServiceIT.java index 9623c0eb66..36523532d0 100644 --- a/integration-tests/main-app-test/src/test/java/co/elastic/apm/test/ServiceIT.java +++ b/integration-tests/main-app-test/src/test/java/co/elastic/apm/test/ServiceIT.java @@ -33,8 +33,9 @@ class ServiceIT { @ParameterizedTest - @ValueSource(strings = {"openjdk:7", "openjdk:8", "openjdk:11", "openjdk:17"}) + @ValueSource(strings = {"openjdk:8", "openjdk:11", "openjdk:17"}) void testServiceNameAndVersionFromManifest(String image) { + assertThat(new File("target/main-app-test.jar")).exists(); GenericContainer app = new GenericContainer<>(DockerImageName.parse(image)) .withFileSystemBind(getAgentJar(), "/tmp/elastic-apm-agent.jar") .withFileSystemBind("target/main-app-test.jar", "/tmp/main-app.jar") @@ -55,6 +56,6 @@ private static String getAgentJar() { return Arrays.stream(buildDir.listFiles(fileFilter)) .findFirst() .map(File::getAbsolutePath) - .orElse(null); + .orElseThrow(() -> new IllegalStateException("Agent jar not found. Execute mvn package to build the agent jar.")); } }