From 00eb3e25e3373d5c7ec6465b94416c64c46d4b5b Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Mon, 18 Dec 2023 19:38:51 +0100 Subject: [PATCH] add oats test --- .../opentelemetry/ConnectionProperties.java | 3 ++- .../opentelemetry/MetricsOtlpConfig.java | 2 -- .../opentelemetry/OpenTelemetryConfig.java | 17 +++++++--------- .../opentelemetry/ResourceAttributesTest.java | 20 ++++++++++++++----- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/grafana/opentelemetry/ConnectionProperties.java b/src/main/java/com/grafana/opentelemetry/ConnectionProperties.java index b65c0b0..7458954 100644 --- a/src/main/java/com/grafana/opentelemetry/ConnectionProperties.java +++ b/src/main/java/com/grafana/opentelemetry/ConnectionProperties.java @@ -4,4 +4,5 @@ import java.util.Map; import java.util.Optional; -public record ConnectionProperties(Optional endpoint, Map headers, Duration metricExportInterval) {} +public record ConnectionProperties( + Optional endpoint, Map headers, Duration metricExportInterval) {} diff --git a/src/main/java/com/grafana/opentelemetry/MetricsOtlpConfig.java b/src/main/java/com/grafana/opentelemetry/MetricsOtlpConfig.java index 57c4c61..868ffda 100644 --- a/src/main/java/com/grafana/opentelemetry/MetricsOtlpConfig.java +++ b/src/main/java/com/grafana/opentelemetry/MetricsOtlpConfig.java @@ -1,8 +1,6 @@ package com.grafana.opentelemetry; -import io.micrometer.core.instrument.config.validate.DurationValidator; import io.micrometer.registry.otlp.OtlpConfig; - import java.time.Duration; import java.util.Map; import java.util.concurrent.TimeUnit; diff --git a/src/main/java/com/grafana/opentelemetry/OpenTelemetryConfig.java b/src/main/java/com/grafana/opentelemetry/OpenTelemetryConfig.java index 496aba7..b0ff64a 100644 --- a/src/main/java/com/grafana/opentelemetry/OpenTelemetryConfig.java +++ b/src/main/java/com/grafana/opentelemetry/OpenTelemetryConfig.java @@ -5,10 +5,6 @@ import io.micrometer.common.util.StringUtils; import io.micrometer.core.instrument.Clock; -import io.micrometer.core.instrument.config.validate.DurationValidator; -import io.micrometer.core.instrument.config.validate.InvalidReason; -import io.micrometer.core.instrument.config.validate.PropertyValidator; -import io.micrometer.core.instrument.config.validate.Validated; import io.micrometer.core.instrument.logging.LoggingMeterRegistry; import io.micrometer.registry.otlp.OtlpMeterRegistry; import io.opentelemetry.api.OpenTelemetry; @@ -29,7 +25,6 @@ import java.util.Objects; import java.util.Optional; import java.util.Set; -import java.util.concurrent.TimeUnit; import java.util.function.Predicate; import java.util.stream.Collectors; import org.apache.logging.log4j.util.Strings; @@ -120,8 +115,10 @@ public AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk( GrafanaProperties properties, ConnectionProperties connectionProperties, @Value("${spring.application.name:#{null}}") String applicationName) { - // the log record exporter uses the global instance, so we need to set it as global to avoid a warning - AutoConfiguredOpenTelemetrySdkBuilder builder = AutoConfiguredOpenTelemetrySdk.builder().setResultAsGlobal(); + // the log record exporter uses the global instance, so we need to set it as global to avoid a + // warning + AutoConfiguredOpenTelemetrySdkBuilder builder = + AutoConfiguredOpenTelemetrySdk.builder().setResultAsGlobal(); Map configProperties = getConfigProperties(properties, connectionProperties); builder.addPropertiesSupplier(() -> configProperties); @@ -169,8 +166,7 @@ private Map getConfigProperties( public ConnectionProperties connectionProperties( GrafanaProperties properties, @Value("${otel.exporter.otlp.endpoint:#{null}}") String otlpEndpoint, - @Value("${otel.metric.export.interval:60000}") String metricExportInterval - ) { + @Value("${otel.metric.export.interval:60000}") String metricExportInterval) { GrafanaProperties.CloudProperties cloud = properties.getCloud(); Map headers = getHeaders(cloud.getInstanceId(), cloud.getApiKey()); if (StringUtils.isBlank(otlpEndpoint)) { @@ -178,7 +174,8 @@ public ConnectionProperties connectionProperties( } Optional endpoint = getEndpoint(otlpEndpoint, cloud.getZone(), headers); - return new ConnectionProperties(endpoint, headers, Duration.of(Integer.parseInt(metricExportInterval), ChronoUnit.MILLIS)); + return new ConnectionProperties( + endpoint, headers, Duration.of(Integer.parseInt(metricExportInterval), ChronoUnit.MILLIS)); } static Map maskAuthHeader(Map configProperties) { diff --git a/src/test/java/com/grafana/opentelemetry/ResourceAttributesTest.java b/src/test/java/com/grafana/opentelemetry/ResourceAttributesTest.java index c79f21f..d8cbb84 100644 --- a/src/test/java/com/grafana/opentelemetry/ResourceAttributesTest.java +++ b/src/test/java/com/grafana/opentelemetry/ResourceAttributesTest.java @@ -3,16 +3,19 @@ import static org.junit.jupiter.api.Named.named; import io.micrometer.core.instrument.Clock; +import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.sdk.resources.Resource; import java.util.Map; import java.util.function.Function; import java.util.stream.Stream; import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.util.ReflectionUtils; public class ResourceAttributesTest { @@ -31,15 +34,22 @@ public TestCase withRunner( } } - private final ApplicationContextRunner contextRunner = - new ApplicationContextRunner() - .withConfiguration(AutoConfigurations.of(OpenTelemetryConfig.class)) - .withBean(Clock.class, () -> Clock.SYSTEM); + @AfterEach + void setUp() throws Exception { + GlobalOpenTelemetry.resetForTest(); + ReflectionUtils.invokeMethod( + Class.forName("io.opentelemetry.api.events.GlobalEventEmitterProvider") + .getMethod("resetForTest"), + null); + } @ParameterizedTest @MethodSource("testCases") void readResourceAttributes(TestCase testCase) { - ApplicationContextRunner runner = contextRunner; + ApplicationContextRunner runner = + new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(OpenTelemetryConfig.class)) + .withBean(Clock.class, () -> Clock.SYSTEM); if (testCase.runner != null) { runner = testCase.runner.apply(runner); }