diff --git a/smoke-tests/src/test/groovy/io/opentelemetry/smoketest/PrometheusSmokeTest.groovy b/smoke-tests/src/test/groovy/io/opentelemetry/smoketest/PrometheusSmokeTest.groovy new file mode 100644 index 000000000000..d9c1d50e7cc4 --- /dev/null +++ b/smoke-tests/src/test/groovy/io/opentelemetry/smoketest/PrometheusSmokeTest.groovy @@ -0,0 +1,56 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.smoketest + +import io.opentelemetry.testing.internal.armeria.client.WebClient +import java.time.Duration +import spock.lang.IgnoreIf + +import static io.opentelemetry.smoketest.TestContainerManager.useWindowsContainers + +@IgnoreIf({ useWindowsContainers() }) +class PrometheusSmokeTest extends SmokeTest { + private static final int PROMETHEUS_PORT = 9090 + + protected String getTargetImage(String jdk) { + "ghcr.io/open-telemetry/opentelemetry-java-instrumentation/smoke-test-spring-boot:jdk$jdk-20211213.1570880324" + } + + @Override + protected Map getExtraEnv() { + return Map.of("OTEL_METRICS_EXPORTER", "prometheus", "OTEL_EXPORTER_PROMETHEUS_PORT", PROMETHEUS_PORT.toString()) + } + + @Override + protected TargetWaitStrategy getWaitStrategy() { + return new TargetWaitStrategy.Log(Duration.ofMinutes(1), ".*Started SpringbootApplication in.*") + } + + protected List getExtraPorts() { + return [PROMETHEUS_PORT] + } + + def "Should export metrics"(int jdk) { + setup: + startTarget(jdk) + + when: + client().get("/greeting").aggregate().join() + + then: + def prometheusClient = WebClient.of("h1c://localhost:${containerManager.getTargetMappedPort(9090)}") + def prometheusData = prometheusClient.get("/").aggregate().join().contentUtf8() + + prometheusData.contains("runtime_jvm_memory_pool") + + cleanup: + stopTarget() + + where: + jdk << [8, 11, 17] + } + +} \ No newline at end of file diff --git a/smoke-tests/src/test/groovy/io/opentelemetry/smoketest/SmokeTest.groovy b/smoke-tests/src/test/groovy/io/opentelemetry/smoketest/SmokeTest.groovy index 12744614d7cb..05e4f84e8539 100644 --- a/smoke-tests/src/test/groovy/io/opentelemetry/smoketest/SmokeTest.groovy +++ b/smoke-tests/src/test/groovy/io/opentelemetry/smoketest/SmokeTest.groovy @@ -58,6 +58,14 @@ abstract class SmokeTest extends Specification { return [] } + /** + * Subclasses can override this method to provide additional ports that should be exposed from the + * target container + */ + protected List getExtraPorts() { + return [] + } + def setupSpec() { containerManager.startEnvironmentOnce() telemetryRetriever = new TelemetryRetriever(containerManager.getBackendMappedPort()) @@ -69,7 +77,7 @@ abstract class SmokeTest extends Specification { def startTarget(String jdk, String serverVersion, boolean windows) { def targetImage = getTargetImage(jdk, serverVersion, windows) - return containerManager.startTarget(targetImage, agentPath, jvmArgsEnvVarName, extraEnv, extraResources, getWaitStrategy(), getCommand()) + return containerManager.startTarget(targetImage, agentPath, jvmArgsEnvVarName, extraEnv, extraResources, extraPorts, getWaitStrategy(), getCommand()) } protected abstract String getTargetImage(String jdk) diff --git a/smoke-tests/src/test/java/io/opentelemetry/smoketest/LinuxTestContainerManager.java b/smoke-tests/src/test/java/io/opentelemetry/smoketest/LinuxTestContainerManager.java index 9ef4cc4ec0c1..42fb41c0cb2d 100644 --- a/smoke-tests/src/test/java/io/opentelemetry/smoketest/LinuxTestContainerManager.java +++ b/smoke-tests/src/test/java/io/opentelemetry/smoketest/LinuxTestContainerManager.java @@ -6,6 +6,7 @@ package io.opentelemetry.smoketest; import java.time.Duration; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Consumer; @@ -69,14 +70,20 @@ public Consumer startTarget( String jvmArgsEnvVarName, Map extraEnv, List extraResources, + List extraPorts, TargetWaitStrategy waitStrategy, String[] command) { Consumer output = new ToStringConsumer(); + List ports = new ArrayList<>(); + ports.add(TARGET_PORT); + if (extraPorts != null) { + ports.addAll(extraPorts); + } target = new GenericContainer<>(DockerImageName.parse(targetImageName)) .withStartupTimeout(Duration.ofMinutes(5)) - .withExposedPorts(TARGET_PORT) + .withExposedPorts(ports.toArray(new Integer[0])) .withNetwork(network) .withLogConsumer(output) .withLogConsumer(new Slf4jLogConsumer(appLogger)) diff --git a/smoke-tests/src/test/java/io/opentelemetry/smoketest/TestContainerManager.java b/smoke-tests/src/test/java/io/opentelemetry/smoketest/TestContainerManager.java index 7be5fdea376c..895e495e99e7 100644 --- a/smoke-tests/src/test/java/io/opentelemetry/smoketest/TestContainerManager.java +++ b/smoke-tests/src/test/java/io/opentelemetry/smoketest/TestContainerManager.java @@ -24,6 +24,7 @@ Consumer startTarget( String jvmArgsEnvVarName, Map extraEnv, List extraResources, + List extraPorts, TargetWaitStrategy waitStrategy, String[] command); diff --git a/smoke-tests/src/test/java/io/opentelemetry/smoketest/windows/WindowsTestContainerManager.java b/smoke-tests/src/test/java/io/opentelemetry/smoketest/windows/WindowsTestContainerManager.java index 20e3711c5970..39793268a96c 100644 --- a/smoke-tests/src/test/java/io/opentelemetry/smoketest/windows/WindowsTestContainerManager.java +++ b/smoke-tests/src/test/java/io/opentelemetry/smoketest/windows/WindowsTestContainerManager.java @@ -76,8 +76,6 @@ protected void startEnvironment() { .exec() .getId(); - String backendSuffix = "-windows-20210611.927888723"; - String backendImageName = "ghcr.io/open-telemetry/opentelemetry-java-instrumentation/smoke-test-fake-backend-windows:20210918.1248928123"; if (!imageExists(backendImageName)) { @@ -134,8 +132,13 @@ public Consumer startTarget( String jvmArgsEnvVarName, Map extraEnv, List extraResources, + List extraPorts, TargetWaitStrategy waitStrategy, String[] cmd) { + if (extraPorts != null && !extraPorts.isEmpty()) { + throw new UnsupportedOperationException("extra ports not supported"); + } + stopTarget(); if (!imageExists(targetImageName)) {