Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test helper for auto configure #6496

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion docs/apidiffs/current_vs_latest/opentelemetry-sdk-testing.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,39 @@
Comparing source compatibility of against
No changes.
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
*** MODIFIED SUPERCLASS: io.opentelemetry.sdk.testing.sdk.OpenTelemetryTesting (<- java.lang.Object)
--- REMOVED METHOD: PUBLIC(-) io.opentelemetry.sdk.testing.assertj.TracesAssert assertTraces()
--- REMOVED METHOD: PUBLIC(-) void clearLogRecords()
--- REMOVED METHOD: PUBLIC(-) void clearMetrics()
--- REMOVED METHOD: PUBLIC(-) void clearSpans()
--- REMOVED METHOD: PUBLIC(-) java.util.List<io.opentelemetry.sdk.logs.data.LogRecordData> getLogRecords()
--- REMOVED METHOD: PUBLIC(-) java.util.List<io.opentelemetry.sdk.metrics.data.MetricData> getMetrics()
--- REMOVED METHOD: PUBLIC(-) io.opentelemetry.api.OpenTelemetry getOpenTelemetry()
--- REMOVED METHOD: PUBLIC(-) java.util.List<io.opentelemetry.sdk.trace.data.SpanData> getSpans()
+++ NEW CLASS: PUBLIC(+) io.opentelemetry.sdk.testing.sdk.AutoConfiguredOpenTelemetryTesting (not serializable)
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
+++ NEW SUPERCLASS: io.opentelemetry.sdk.testing.sdk.OpenTelemetryTesting
+++ NEW CONSTRUCTOR: PUBLIC(+) AutoConfiguredOpenTelemetryTesting(io.opentelemetry.sdk.testing.sdk.OpenTelemetryTestSdk)
+++ NEW METHOD: PUBLIC(+) FINAL(+) void assertTraces(java.util.function.Consumer[]<io.opentelemetry.sdk.testing.assertj.TraceAssert>)
+++ NEW ANNOTATION: java.lang.SafeVarargs
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.testing.sdk.AutoConfiguredOpenTelemetryTesting create(java.util.Map<java.lang.String,java.lang.String>)
+++ NEW CLASS: PUBLIC(+) io.opentelemetry.sdk.testing.sdk.OpenTelemetryTesting (not serializable)
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
+++ NEW SUPERCLASS: java.lang.Object
+++ NEW CONSTRUCTOR: PUBLIC(+) OpenTelemetryTesting(io.opentelemetry.sdk.testing.sdk.OpenTelemetryTestSdk)
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.TracesAssert assertTraces()
+++ NEW METHOD: PUBLIC(+) void clearLogRecords()
+++ NEW METHOD: PUBLIC(+) void clearMetrics()
+++ NEW METHOD: PUBLIC(+) void clearSpans()
+++ NEW METHOD: PUBLIC(+) java.util.List<io.opentelemetry.sdk.logs.data.LogRecordData> getLogRecords()
+++ NEW METHOD: PUBLIC(+) java.util.List<io.opentelemetry.sdk.metrics.data.MetricData> getMetrics()
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.OpenTelemetry getOpenTelemetry()
+++ NEW METHOD: PUBLIC(+) java.util.List<io.opentelemetry.sdk.trace.data.SpanData> getSpans()
+++ NEW CLASS: PUBLIC(+) io.opentelemetry.sdk.testing.sdk.OpenTelemetryTestSdk (not serializable)
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
+++ NEW SUPERCLASS: java.lang.Object
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.testing.sdk.OpenTelemetryTestSdk create()
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.exporter.InMemoryLogRecordExporter getLogRecordExporter()
+++ NEW METHOD: PUBLIC(+) java.util.function.Supplier<java.util.Collection<io.opentelemetry.sdk.metrics.data.MetricData>> getMetricReader()
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.OpenTelemetrySdk getOpenTelemetry()
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter getSpanExporter()
3 changes: 3 additions & 0 deletions sdk/testing/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ dependencies {
compileOnly("org.assertj:assertj-core")
compileOnly("junit:junit")
compileOnly("org.junit.jupiter:junit-jupiter-api")
compileOnly(project(":sdk-extensions:autoconfigure"))
compileOnly("org.awaitility:awaitility")

annotationProcessor("com.google.auto.value:auto-value")

testImplementation("junit:junit")
testImplementation("org.junit.vintage:junit-vintage-engine")
testImplementation(project(":sdk-extensions:autoconfigure"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,19 @@

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
import io.opentelemetry.sdk.logs.data.LogRecordData;
import io.opentelemetry.sdk.logs.export.SimpleLogRecordProcessor;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.internal.SdkMeterProviderUtil;
import io.opentelemetry.sdk.testing.exporter.InMemoryLogRecordExporter;
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader;
import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.testing.sdk.OpenTelemetryTestSdk;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Supplier;
import org.junit.rules.ExternalResource;

/**
Expand Down Expand Up @@ -63,45 +58,24 @@ public final class OpenTelemetryRule extends ExternalResource {
* exporter and W3C trace context propagation.
*/
public static OpenTelemetryRule create() {
InMemorySpanExporter spanExporter = InMemorySpanExporter.create();
OpenTelemetryTestSdk testSdk = OpenTelemetryTestSdk.create();

SdkTracerProvider tracerProvider =
SdkTracerProvider.builder()
.addSpanProcessor(SimpleSpanProcessor.create(spanExporter))
.build();

InMemoryMetricReader metricReader = InMemoryMetricReader.create();

SdkMeterProvider meterProvider =
SdkMeterProvider.builder().registerMetricReader(metricReader).build();

InMemoryLogRecordExporter logRecordExporter = InMemoryLogRecordExporter.create();

SdkLoggerProvider loggerProvider =
SdkLoggerProvider.builder()
.addLogRecordProcessor(SimpleLogRecordProcessor.create(logRecordExporter))
.build();

OpenTelemetrySdk openTelemetry =
OpenTelemetrySdk.builder()
.setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance()))
.setTracerProvider(tracerProvider)
.setMeterProvider(meterProvider)
.setLoggerProvider(loggerProvider)
.build();

return new OpenTelemetryRule(openTelemetry, spanExporter, metricReader, logRecordExporter);
return new OpenTelemetryRule(
testSdk.getOpenTelemetry(),
testSdk.getSpanExporter(),
testSdk.getMetricReader(),
testSdk.getLogRecordExporter());
}

private final OpenTelemetrySdk openTelemetry;
private final InMemorySpanExporter spanExporter;
private final InMemoryMetricReader metricReader;
private final Supplier<Collection<MetricData>> metricReader;
private final InMemoryLogRecordExporter logRecordExporter;

private OpenTelemetryRule(
OpenTelemetrySdk openTelemetry,
InMemorySpanExporter spanExporter,
InMemoryMetricReader metricReader,
Supplier<Collection<MetricData>> metricReader,
InMemoryLogRecordExporter logRecordExporter) {
this.openTelemetry = openTelemetry;
this.spanExporter = spanExporter;
Expand All @@ -125,7 +99,7 @@ public List<SpanData> getSpans() {
* @since 1.15.0
*/
public List<MetricData> getMetrics() {
return new ArrayList<>(metricReader.collectAllMetrics());
return new ArrayList<>(metricReader.get());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,10 @@

package io.opentelemetry.sdk.testing.junit5;

import static io.opentelemetry.sdk.testing.assertj.TracesAssert.assertThat;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
import io.opentelemetry.sdk.logs.data.LogRecordData;
import io.opentelemetry.sdk.logs.export.SimpleLogRecordProcessor;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.internal.SdkMeterProviderUtil;
import io.opentelemetry.sdk.testing.assertj.TracesAssert;
import io.opentelemetry.sdk.testing.exporter.InMemoryLogRecordExporter;
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader;
import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
import java.util.ArrayList;
import java.util.List;
import io.opentelemetry.sdk.testing.sdk.OpenTelemetryTestSdk;
import io.opentelemetry.sdk.testing.sdk.OpenTelemetryTesting;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
Expand Down Expand Up @@ -57,121 +38,20 @@
* // }
* }</pre>
*/
public final class OpenTelemetryExtension
public final class OpenTelemetryExtension extends OpenTelemetryTesting
implements BeforeEachCallback, BeforeAllCallback, AfterAllCallback {

/**
* Returns a {@link OpenTelemetryExtension} with a default SDK initialized with an in-memory span
* exporter and W3C trace context propagation.
*/
public static OpenTelemetryExtension create() {
InMemorySpanExporter spanExporter = InMemorySpanExporter.create();

SdkTracerProvider tracerProvider =
SdkTracerProvider.builder()
.addSpanProcessor(SimpleSpanProcessor.create(spanExporter))
.build();

InMemoryMetricReader metricReader = InMemoryMetricReader.create();

SdkMeterProvider meterProvider =
SdkMeterProvider.builder().registerMetricReader(metricReader).build();

InMemoryLogRecordExporter logRecordExporter = InMemoryLogRecordExporter.create();

SdkLoggerProvider loggerProvider =
SdkLoggerProvider.builder()
.addLogRecordProcessor(SimpleLogRecordProcessor.create(logRecordExporter))
.build();

OpenTelemetrySdk openTelemetry =
OpenTelemetrySdk.builder()
.setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance()))
.setTracerProvider(tracerProvider)
.setMeterProvider(meterProvider)
.setLoggerProvider(loggerProvider)
.build();

return new OpenTelemetryExtension(openTelemetry, spanExporter, metricReader, logRecordExporter);
}

private final OpenTelemetrySdk openTelemetry;
private final InMemorySpanExporter spanExporter;
private final InMemoryMetricReader metricReader;
private final InMemoryLogRecordExporter logRecordExporter;

private OpenTelemetryExtension(
OpenTelemetrySdk openTelemetry,
InMemorySpanExporter spanExporter,
InMemoryMetricReader metricReader,
InMemoryLogRecordExporter logRecordExporter) {
this.openTelemetry = openTelemetry;
this.spanExporter = spanExporter;
this.metricReader = metricReader;
this.logRecordExporter = logRecordExporter;
}

/** Returns the {@link OpenTelemetrySdk} created by this extension. */
public OpenTelemetry getOpenTelemetry() {
return openTelemetry;
}

/** Returns all the exported {@link SpanData} so far. */
public List<SpanData> getSpans() {
return spanExporter.getFinishedSpanItems();
}

/**
* Returns the current {@link MetricData} in {@link AggregationTemporality#CUMULATIVE} format.
*
* @since 1.15.0
*/
public List<MetricData> getMetrics() {
return new ArrayList<>(metricReader.collectAllMetrics());
}

/**
* Returns all the exported {@link LogRecordData} so far.
*
* @since 1.32.0
*/
public List<LogRecordData> getLogRecords() {
return new ArrayList<>(logRecordExporter.getFinishedLogRecordItems());
}

/**
* Returns a {@link TracesAssert} for asserting on the currently exported traces. This method
* requires AssertJ to be on the classpath.
*/
public TracesAssert assertTraces() {
return assertThat(spanExporter.getFinishedSpanItems());
}

/**
* Clears the collected exported {@link SpanData}. Consider making your test smaller instead of
* manually clearing state using this method.
*/
public void clearSpans() {
spanExporter.reset();
return new OpenTelemetryExtension(OpenTelemetryTestSdk.create());
}

/**
* Clears all registered metric instruments, such that {@link #getMetrics()} is empty.
*
* @since 1.15.0
*/
public void clearMetrics() {
SdkMeterProviderUtil.resetForTest(openTelemetry.getSdkMeterProvider());
}

/**
* Clears the collected exported {@link LogRecordData}. Consider making your test smaller instead
* of manually clearing state using this method.
*
* @since 1.32.0
*/
public void clearLogRecords() {
logRecordExporter.reset();
private OpenTelemetryExtension(OpenTelemetryTestSdk openTelemetryTestSdk) {
super(openTelemetryTestSdk);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.testing.sdk;

import static org.awaitility.Awaitility.await;

import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder;
import io.opentelemetry.sdk.autoconfigure.internal.AutoConfigureUtil;
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.testing.assertj.TraceAssert;
import io.opentelemetry.sdk.testing.exporter.InMemoryLogRecordExporter;
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricExporter;
import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

public class AutoConfiguredOpenTelemetryTesting extends OpenTelemetryTesting {

static final String MEMORY_EXPORTER = "memory";

public static AutoConfiguredOpenTelemetryTesting create(Map<String, String> properties) {
return new AutoConfiguredOpenTelemetryTesting(getOpenTelemetrySdk(properties));
}

private AutoConfiguredOpenTelemetryTesting(OpenTelemetryTestSdk sdk) {
super(sdk);
}

private static OpenTelemetryTestSdk getOpenTelemetrySdk(Map<String, String> properties) {

InMemorySpanExporter spanExporter = InMemorySpanExporter.create();
InMemoryMetricExporter metricExporter =
InMemoryMetricExporter.create(AggregationTemporality.DELTA);
InMemoryLogRecordExporter logRecordExporter = InMemoryLogRecordExporter.create();

AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder =
AutoConfiguredOpenTelemetrySdk.builder()
.addPropertiesSupplier(AutoConfiguredOpenTelemetryTesting::getProperties)
.addPropertiesSupplier(() -> properties);
AutoConfigureUtil.setComponentLoader(
sdkBuilder, new InMemoryComponentLoader(spanExporter, metricExporter, logRecordExporter));
OpenTelemetrySdk openTelemetrySdk = sdkBuilder.build().getOpenTelemetrySdk();
return new OpenTelemetryTestSdk(
openTelemetrySdk, spanExporter, metricExporter::getFinishedMetricItems, logRecordExporter);
}

private static Map<String, String> getProperties() {
Map<String, String> map = new HashMap<>();
map.put("otel.bsp.schedule.delay", "10");
map.put("otel.metric.export.interval", "10");
map.put("otel.blrp.schedule.delay", "10");
map.put("otel.traces.exporter", MEMORY_EXPORTER);
map.put("otel.metrics.exporter", MEMORY_EXPORTER);
map.put("otel.logs.exporter", MEMORY_EXPORTER);
return map;
}

/** needs org.awaitility:awaitility to be on the classpath */
@SafeVarargs
@SuppressWarnings("varargs")
public final void assertTraces(Consumer<TraceAssert>... assertions) {
await()
.atMost(Duration.ofSeconds(1))
.untilAsserted(() -> assertTraces().hasTracesSatisfyingExactly(assertions));
}
}
Loading
Loading