From 741d79e5823fa4152092981b1a68b6ee30fbc046 Mon Sep 17 00:00:00 2001 From: EddeCCC Date: Tue, 19 Nov 2024 16:35:01 +0100 Subject: [PATCH] test: add MetricFactoryTest --- inspectit-gepard-agent/build.gradle | 1 + .../internal/metrics/MetricFactoryTest.java | 63 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 inspectit-gepard-agent/src/test/java/rocks/inspectit/gepard/agent/internal/metrics/MetricFactoryTest.java diff --git a/inspectit-gepard-agent/build.gradle b/inspectit-gepard-agent/build.gradle index b0e07d0..3b2bc4b 100644 --- a/inspectit-gepard-agent/build.gradle +++ b/inspectit-gepard-agent/build.gradle @@ -108,6 +108,7 @@ dependencies { testCompileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-extension-api") testCompileOnly("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi") testCompileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-bootstrap") + testCompileOnly("io.opentelemetry:opentelemetry-exporter-logging") testImplementation("org.junit.jupiter:junit-jupiter-api:${versions.junit}") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${versions.junit}") diff --git a/inspectit-gepard-agent/src/test/java/rocks/inspectit/gepard/agent/internal/metrics/MetricFactoryTest.java b/inspectit-gepard-agent/src/test/java/rocks/inspectit/gepard/agent/internal/metrics/MetricFactoryTest.java new file mode 100644 index 0000000..18d504e --- /dev/null +++ b/inspectit-gepard-agent/src/test/java/rocks/inspectit/gepard/agent/internal/metrics/MetricFactoryTest.java @@ -0,0 +1,63 @@ +/* (C) 2024 */ +package rocks.inspectit.gepard.agent.internal.metrics; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.api.metrics.ObservableDoubleGauge; +import io.opentelemetry.api.metrics.ObservableDoubleMeasurement; +import io.opentelemetry.exporter.logging.LoggingMetricExporter; +import io.opentelemetry.sdk.OpenTelemetrySdk; +import io.opentelemetry.sdk.metrics.SdkMeterProvider; +import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader; +import java.time.Duration; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.Consumer; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import rocks.inspectit.gepard.agent.internal.otel.OpenTelemetryAccessor; + +class MetricFactoryTest { + + // Interval to read and export metrics + private static final Duration INTERVAL = Duration.ofMillis(500); + + @BeforeAll + static void beforeAll() { + GlobalOpenTelemetry.resetForTest(); + + // Build our own OpenTelemetrySdk, so we don't use the NOOP implementations + SdkMeterProvider meterProvider = + SdkMeterProvider.builder() + .registerMetricReader( + PeriodicMetricReader.builder(LoggingMetricExporter.create()) + .setInterval(INTERVAL) + .build()) + .build(); + OpenTelemetrySdk.builder().setMeterProvider(meterProvider).buildAndRegisterGlobal(); + OpenTelemetryAccessor.setOpenTelemetry(GlobalOpenTelemetry.get()); + } + + @Test + void shouldRecordValueWithCallbackWhenCreatingGauge() throws InterruptedException { + AtomicBoolean valueRecorded = new AtomicBoolean(false); + + Consumer callback1 = (measurement) -> valueRecorded.set(true); + Consumer callback2 = + (measurement) -> { + throw new RuntimeException("Test exception"); + }; + + ObservableDoubleGauge gauge1 = + MetricFactory.createObservableDoubleGauge("test-gauge", callback1); + ObservableDoubleGauge gauge2 = + MetricFactory.createObservableDoubleGauge("fail-gauge", callback2); + // Wait for MetricReader + Thread.sleep(INTERVAL.toMillis() + 100); + + assertTrue(valueRecorded.get()); + + gauge1.close(); + gauge2.close(); + } +}