Skip to content

Commit

Permalink
test: add MetricFactoryTest
Browse files Browse the repository at this point in the history
  • Loading branch information
EddeCCC committed Nov 19, 2024
1 parent 5a2b37b commit 741d79e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions inspectit-gepard-agent/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ObservableDoubleMeasurement> callback1 = (measurement) -> valueRecorded.set(true);
Consumer<ObservableDoubleMeasurement> 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();
}
}

0 comments on commit 741d79e

Please sign in to comment.