From c181ccafa6c0f481882551dc0c550b74d9a0d480 Mon Sep 17 00:00:00 2001 From: Dmitry Aleksandrov Date: Tue, 20 Jun 2023 16:09:06 +0300 Subject: [PATCH] [4.x] - Remove Microprofile dependencies from SE Telemetry (#6998) * Remove microprofile dependencies * Tests for SE Agent detection. --- microprofile/telemetry/pom.xml | 16 +++++ .../telemetry/AgentDetectorTest.java | 65 +++++++++++++++++++ tracing/opentelemetry/pom.xml | 11 +--- .../opentelemetry/HelidonOpenTelemetry.java | 4 +- .../opentelemetry/AgentDetectorTest.java | 25 ++++--- 5 files changed, 96 insertions(+), 25 deletions(-) create mode 100644 microprofile/telemetry/src/test/java/io/helidon/microprofile/telemetry/AgentDetectorTest.java diff --git a/microprofile/telemetry/pom.xml b/microprofile/telemetry/pom.xml index b0f2ab7dc7d..fef4c9e663b 100644 --- a/microprofile/telemetry/pom.xml +++ b/microprofile/telemetry/pom.xml @@ -87,6 +87,22 @@ jakarta.activation-api provided + + + org.junit.jupiter + junit-jupiter-api + test + + + org.hamcrest + hamcrest-all + test + + + io.helidon.microprofile.tests + helidon-microprofile-tests-junit5 + test + diff --git a/microprofile/telemetry/src/test/java/io/helidon/microprofile/telemetry/AgentDetectorTest.java b/microprofile/telemetry/src/test/java/io/helidon/microprofile/telemetry/AgentDetectorTest.java new file mode 100644 index 00000000000..17f3750783d --- /dev/null +++ b/microprofile/telemetry/src/test/java/io/helidon/microprofile/telemetry/AgentDetectorTest.java @@ -0,0 +1,65 @@ +package io.helidon.microprofile.telemetry;/* + * Copyright (c) 2023 Oracle and/or its affiliates. + * + * Licensed 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. + */ + +import io.helidon.config.Config; +import io.helidon.microprofile.server.ServerCdiExtension; +import io.helidon.microprofile.tests.junit5.AddConfig; +import io.helidon.microprofile.tests.junit5.AddExtension; +import io.helidon.microprofile.tests.junit5.HelidonTest; +import io.helidon.tracing.opentelemetry.HelidonOpenTelemetry; +import jakarta.enterprise.inject.spi.CDI; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + + +/** + * Check Agent Detector working correctly. + */ +@HelidonTest(resetPerTest = true) +@AddExtension(ServerCdiExtension.class) +class AgentDetectorTest { + + public static final String OTEL_AGENT_PRESENT = "otel.agent.present"; + public static final String IO_OPENTELEMETRY_JAVAAGENT = "io.opentelemetry.javaagent"; + + @Test + @AddConfig(key = OTEL_AGENT_PRESENT, value = "true") + void shouldBeNoOpTelemetry(){ + Config config = CDI.current().select(Config.class).get(); + boolean present = HelidonOpenTelemetry.AgentDetector.isAgentPresent(config); + assertThat(present, is(true)); + } + + @Test + @AddConfig(key = OTEL_AGENT_PRESENT, value = "false") + void shouldNotBeNoOpTelemetry(){ + Config config = CDI.current().select(Config.class).get(); + boolean present = HelidonOpenTelemetry.AgentDetector.isAgentPresent(config); + assertThat(present, is(false)); + } + + @Test + void checkEnvVariable(){ + System.setProperty(IO_OPENTELEMETRY_JAVAAGENT, "true"); + Config config = CDI.current().select(Config.class).get(); + boolean present = HelidonOpenTelemetry.AgentDetector.isAgentPresent(config); + assertThat(present, is(true)); + } +} diff --git a/tracing/opentelemetry/pom.xml b/tracing/opentelemetry/pom.xml index 09eebf6894f..e3f8a84bc27 100644 --- a/tracing/opentelemetry/pom.xml +++ b/tracing/opentelemetry/pom.xml @@ -58,6 +58,7 @@ provided true + org.junit.jupiter junit-jupiter-api @@ -68,15 +69,5 @@ hamcrest-all test - - io.helidon.microprofile.tests - helidon-microprofile-tests-junit5 - test - - - io.helidon.microprofile.server - helidon-microprofile-server - test - diff --git a/tracing/opentelemetry/src/main/java/io/helidon/tracing/opentelemetry/HelidonOpenTelemetry.java b/tracing/opentelemetry/src/main/java/io/helidon/tracing/opentelemetry/HelidonOpenTelemetry.java index 3faff6c0e8d..490445f86ed 100644 --- a/tracing/opentelemetry/src/main/java/io/helidon/tracing/opentelemetry/HelidonOpenTelemetry.java +++ b/tracing/opentelemetry/src/main/java/io/helidon/tracing/opentelemetry/HelidonOpenTelemetry.java @@ -33,8 +33,8 @@ public final class HelidonOpenTelemetry { private static final System.Logger LOGGER = System.getLogger(HelidonOpenTelemetry.class.getName()); - static final String OTEL_AGENT_PRESENT_PROPERTY = "otel.agent.present"; - static final String IO_OPENTELEMETRY_JAVAAGENT = "io.opentelemetry.javaagent"; + private static final String OTEL_AGENT_PRESENT_PROPERTY = "otel.agent.present"; + private static final String IO_OPENTELEMETRY_JAVAAGENT = "io.opentelemetry.javaagent"; private HelidonOpenTelemetry() { } /** diff --git a/tracing/opentelemetry/src/test/java/io/helidon/tracing/opentelemetry/AgentDetectorTest.java b/tracing/opentelemetry/src/test/java/io/helidon/tracing/opentelemetry/AgentDetectorTest.java index bf673ef91b6..7769947fc91 100644 --- a/tracing/opentelemetry/src/test/java/io/helidon/tracing/opentelemetry/AgentDetectorTest.java +++ b/tracing/opentelemetry/src/test/java/io/helidon/tracing/opentelemetry/AgentDetectorTest.java @@ -13,14 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package io.helidon.tracing.opentelemetry; +import java.util.Map; + import io.helidon.config.Config; -import io.helidon.microprofile.server.ServerCdiExtension; -import io.helidon.microprofile.tests.junit5.AddConfig; -import io.helidon.microprofile.tests.junit5.AddExtension; -import io.helidon.microprofile.tests.junit5.HelidonTest; -import jakarta.enterprise.inject.spi.CDI; +import io.helidon.config.ConfigSources; + import org.junit.jupiter.api.Test; import static org.hamcrest.MatcherAssert.assertThat; @@ -30,30 +30,29 @@ /** * Check Agent Detector working correctly. */ -@HelidonTest(resetPerTest = true) -@AddExtension(ServerCdiExtension.class) class AgentDetectorTest { + public static final String OTEL_AGENT_PRESENT = "otel.agent.present"; + public static final String IO_OPENTELEMETRY_JAVAAGENT = "io.opentelemetry.javaagent"; + @Test - @AddConfig(key = HelidonOpenTelemetry.OTEL_AGENT_PRESENT_PROPERTY, value = "true") void shouldBeNoOpTelemetry(){ - Config config = CDI.current().select(Config.class).get(); + Config config = Config.create(ConfigSources.create(Map.of(OTEL_AGENT_PRESENT, "true"))); boolean present = HelidonOpenTelemetry.AgentDetector.isAgentPresent(config); assertThat(present, is(true)); } @Test - @AddConfig(key = HelidonOpenTelemetry.OTEL_AGENT_PRESENT_PROPERTY, value = "false") void shouldNotBeNoOpTelemetry(){ - Config config = CDI.current().select(Config.class).get(); + Config config = Config.create(ConfigSources.create(Map.of(OTEL_AGENT_PRESENT, "false"))); boolean present = HelidonOpenTelemetry.AgentDetector.isAgentPresent(config); assertThat(present, is(false)); } @Test void checkEnvVariable(){ - System.setProperty(HelidonOpenTelemetry.IO_OPENTELEMETRY_JAVAAGENT, "true"); - Config config = CDI.current().select(Config.class).get(); + System.setProperty(IO_OPENTELEMETRY_JAVAAGENT, "true"); + Config config = Config.create(); boolean present = HelidonOpenTelemetry.AgentDetector.isAgentPresent(config); assertThat(present, is(true)); }