Skip to content

Commit

Permalink
Improve behavior of some tests (open-telemetry#4123)
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraaga authored Jan 26, 2022
1 parent 3a9b448 commit 3cabcc7
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,19 @@ class KotlinCoroutinesTest {
fun stressTest() {
val context1 = Context.root().with(ANIMAL, "cat")
runBlocking(context1.asContextElement()) {
assertThat(Context.current().get(ANIMAL)).isEqualTo("cat")
delay(10)
assertThat(Context.current().get(ANIMAL)).isEqualTo("cat")
for (i in 0 until 100) {
GlobalScope.launch {
launch {
assertThat(Context.current().get(ANIMAL)).isEqualTo("cat")
withContext(context1.with(ANIMAL, "dog").asContextElement()) {
assertThat(Context.current().get(ANIMAL)).isEqualTo("dog")
delay(10)
assertThat(Context.current().get(ANIMAL)).isEqualTo("dog")
}
}
GlobalScope.launch {
launch {
assertThat(Context.current().get(ANIMAL)).isEqualTo("cat")
withContext(context1.with(ANIMAL, "koala").asContextElement()) {
assertThat(Context.current().get(ANIMAL)).isEqualTo("koala")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ class InteroperabilityTest {

static {
spanExporter = spy(SpanExporter.class);
when(spanExporter.export(anyList())).thenReturn(CompletableResultCode.ofSuccess());

SpanProcessor spanProcessor = SimpleSpanProcessor.create(spanExporter);
openTelemetry =
Expand All @@ -80,6 +79,7 @@ class InteroperabilityTest {
@BeforeEach
void resetMocks() {
reset(spanExporter);
when(spanExporter.export(anyList())).thenReturn(CompletableResultCode.ofSuccess());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,52 +73,55 @@ void configureExportersGeneral() {
props.put("otel.exporter.otlp.compression", "gzip");
props.put("otel.exporter.otlp.timeout", "15s");
ConfigProperties properties = DefaultConfigProperties.createForTest(props);
SpanExporter spanExporter =
SpanExporterConfiguration.configureExporter(
"otlp", properties, Collections.emptyMap(), MeterProvider.noop());
MetricExporter metricExporter =
MetricExporterConfiguration.configureOtlpMetrics(properties, SdkMeterProvider.builder());
LogExporter logExporter =
LogExporterConfiguration.configureOtlpLogs(properties, MeterProvider.noop());

assertThat(spanExporter)
.extracting("delegate.timeoutNanos")
.isEqualTo(TimeUnit.SECONDS.toNanos(15));
assertThat(spanExporter.export(SPAN_DATA).join(15, TimeUnit.SECONDS).isSuccess()).isTrue();
assertThat(server.traceRequests).hasSize(1);
assertThat(server.requestHeaders)
.anyMatch(
headers ->
headers.contains(
":path", "/opentelemetry.proto.collector.trace.v1.TraceService/Export")
&& headers.contains("header-key", "header-value")
&& headers.contains("grpc-encoding", "gzip"));
try (SpanExporter spanExporter =
SpanExporterConfiguration.configureExporter(
"otlp", properties, Collections.emptyMap(), MeterProvider.noop());
MetricExporter metricExporter =
MetricExporterConfiguration.configureOtlpMetrics(
properties, SdkMeterProvider.builder());
LogExporter logExporter =
LogExporterConfiguration.configureOtlpLogs(properties, MeterProvider.noop())) {
assertThat(spanExporter)
.extracting("delegate.timeoutNanos")
.isEqualTo(TimeUnit.SECONDS.toNanos(15));
assertThat(spanExporter.export(SPAN_DATA).join(15, TimeUnit.SECONDS).isSuccess()).isTrue();
assertThat(server.traceRequests).hasSize(1);
assertThat(server.requestHeaders)
.anyMatch(
headers ->
headers.contains(
":path", "/opentelemetry.proto.collector.trace.v1.TraceService/Export")
&& headers.contains("header-key", "header-value")
&& headers.contains("grpc-encoding", "gzip"));

assertThat(metricExporter)
.extracting("delegate.timeoutNanos")
.isEqualTo(TimeUnit.SECONDS.toNanos(15));
assertThat(metricExporter.export(METRIC_DATA).join(15, TimeUnit.SECONDS).isSuccess()).isTrue();
assertThat(server.metricRequests).hasSize(1);
assertThat(server.requestHeaders)
.anyMatch(
headers ->
headers.contains(
":path", "/opentelemetry.proto.collector.metrics.v1.MetricsService/Export")
&& headers.contains("header-key", "header-value")
&& headers.contains("grpc-encoding", "gzip"));
assertThat(metricExporter)
.extracting("delegate.timeoutNanos")
.isEqualTo(TimeUnit.SECONDS.toNanos(15));
assertThat(metricExporter.export(METRIC_DATA).join(15, TimeUnit.SECONDS).isSuccess())
.isTrue();
assertThat(server.metricRequests).hasSize(1);
assertThat(server.requestHeaders)
.anyMatch(
headers ->
headers.contains(
":path",
"/opentelemetry.proto.collector.metrics.v1.MetricsService/Export")
&& headers.contains("header-key", "header-value")
&& headers.contains("grpc-encoding", "gzip"));

assertThat(logExporter)
.extracting("delegate.timeoutNanos")
.isEqualTo(TimeUnit.SECONDS.toNanos(15));
assertThat(logExporter.export(LOG_DATA).join(15, TimeUnit.SECONDS).isSuccess()).isTrue();
assertThat(server.logRequests).hasSize(1);
assertThat(server.requestHeaders)
.anyMatch(
headers ->
headers.contains(
":path", "/opentelemetry.proto.collector.logs.v1.LogsService/Export")
&& headers.contains("header-key", "header-value")
&& headers.contains("grpc-encoding", "gzip"));
assertThat(logExporter)
.extracting("delegate.timeoutNanos")
.isEqualTo(TimeUnit.SECONDS.toNanos(15));
assertThat(logExporter.export(LOG_DATA).join(15, TimeUnit.SECONDS).isSuccess()).isTrue();
assertThat(server.logRequests).hasSize(1);
assertThat(server.requestHeaders)
.anyMatch(
headers ->
headers.contains(
":path", "/opentelemetry.proto.collector.logs.v1.LogsService/Export")
&& headers.contains("header-key", "header-value")
&& headers.contains("grpc-encoding", "gzip"));
}
}

@Test
Expand All @@ -137,24 +140,24 @@ void configureSpanExporter() {
props.put("otel.exporter.otlp.traces.headers", "header-key=header-value");
props.put("otel.exporter.otlp.traces.compression", "gzip");
props.put("otel.exporter.otlp.traces.timeout", "15s");
SpanExporter spanExporter =
try (SpanExporter spanExporter =
SpanExporterConfiguration.configureExporter(
"otlp",
DefaultConfigProperties.createForTest(props),
Collections.emptyMap(),
MeterProvider.noop());

assertThat(spanExporter)
.extracting("delegate.timeoutNanos")
.isEqualTo(TimeUnit.SECONDS.toNanos(15));
assertThat(spanExporter.export(SPAN_DATA).join(10, TimeUnit.SECONDS).isSuccess()).isTrue();
assertThat(server.traceRequests).hasSize(1);
assertThat(server.requestHeaders)
.anyMatch(
headers ->
headers.contains(
":path", "/opentelemetry.proto.collector.trace.v1.TraceService/Export")
&& headers.contains("header-key", "header-value"));
MeterProvider.noop())) {
assertThat(spanExporter)
.extracting("delegate.timeoutNanos")
.isEqualTo(TimeUnit.SECONDS.toNanos(15));
assertThat(spanExporter.export(SPAN_DATA).join(10, TimeUnit.SECONDS).isSuccess()).isTrue();
assertThat(server.traceRequests).hasSize(1);
assertThat(server.requestHeaders)
.anyMatch(
headers ->
headers.contains(
":path", "/opentelemetry.proto.collector.trace.v1.TraceService/Export")
&& headers.contains("header-key", "header-value"));
}
}

@Test
Expand All @@ -174,23 +177,26 @@ public void configureMetricExporter() {
props.put("otel.exporter.otlp.metrics.compression", "gzip");
props.put("otel.exporter.otlp.metrics.timeout", "15s");
props.put("otel.exporter.otlp.metrics.temporality", "DELTA");
MetricExporter metricExporter =
try (MetricExporter metricExporter =
MetricExporterConfiguration.configureOtlpMetrics(
DefaultConfigProperties.createForTest(props), SdkMeterProvider.builder());
DefaultConfigProperties.createForTest(props), SdkMeterProvider.builder())) {

assertThat(metricExporter)
.extracting("delegate.timeoutNanos")
.isEqualTo(TimeUnit.SECONDS.toNanos(15));
assertThat(metricExporter.getPreferredTemporality()).isEqualTo(AggregationTemporality.DELTA);
assertThat(metricExporter.export(METRIC_DATA).join(15, TimeUnit.SECONDS).isSuccess()).isTrue();
assertThat(server.metricRequests).hasSize(1);
assertThat(server.requestHeaders)
.anyMatch(
headers ->
headers.contains(
":path", "/opentelemetry.proto.collector.metrics.v1.MetricsService/Export")
&& headers.contains("header-key", "header-value")
&& headers.contains("grpc-encoding", "gzip"));
assertThat(metricExporter)
.extracting("delegate.timeoutNanos")
.isEqualTo(TimeUnit.SECONDS.toNanos(15));
assertThat(metricExporter.getPreferredTemporality()).isEqualTo(AggregationTemporality.DELTA);
assertThat(metricExporter.export(METRIC_DATA).join(15, TimeUnit.SECONDS).isSuccess())
.isTrue();
assertThat(server.metricRequests).hasSize(1);
assertThat(server.requestHeaders)
.anyMatch(
headers ->
headers.contains(
":path",
"/opentelemetry.proto.collector.metrics.v1.MetricsService/Export")
&& headers.contains("header-key", "header-value")
&& headers.contains("grpc-encoding", "gzip"));
}
}

@Test
Expand All @@ -209,22 +215,23 @@ public void configureLogExporter() {
props.put("otel.exporter.otlp.logs.headers", "header-key=header-value");
props.put("otel.exporter.otlp.logs.compression", "gzip");
props.put("otel.exporter.otlp.logs.timeout", "15s");
LogExporter logExporter =
try (LogExporter logExporter =
LogExporterConfiguration.configureOtlpLogs(
DefaultConfigProperties.createForTest(props), MeterProvider.noop());
DefaultConfigProperties.createForTest(props), MeterProvider.noop())) {

assertThat(logExporter)
.extracting("delegate.timeoutNanos")
.isEqualTo(TimeUnit.SECONDS.toNanos(15));
assertThat(logExporter.export(LOG_DATA).join(15, TimeUnit.SECONDS).isSuccess()).isTrue();
assertThat(server.logRequests).hasSize(1);
assertThat(server.requestHeaders)
.anyMatch(
headers ->
headers.contains(
":path", "/opentelemetry.proto.collector.logs.v1.LogsService/Export")
&& headers.contains("header-key", "header-value")
&& headers.contains("grpc-encoding", "gzip"));
assertThat(logExporter)
.extracting("delegate.timeoutNanos")
.isEqualTo(TimeUnit.SECONDS.toNanos(15));
assertThat(logExporter.export(LOG_DATA).join(15, TimeUnit.SECONDS).isSuccess()).isTrue();
assertThat(server.logRequests).hasSize(1);
assertThat(server.requestHeaders)
.anyMatch(
headers ->
headers.contains(
":path", "/opentelemetry.proto.collector.logs.v1.LogsService/Export")
&& headers.contains("header-key", "header-value")
&& headers.contains("grpc-encoding", "gzip"));
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,16 @@ void configureSpanExporterRetryPolicy() {
props.put(
"otel.exporter.otlp.traces.certificate", certificate.certificateFile().getAbsolutePath());
props.put("otel.experimental.exporter.otlp.retry.enabled", "true");
SpanExporter spanExporter =
try (SpanExporter spanExporter =
SpanExporterConfiguration.configureExporter(
"otlp",
DefaultConfigProperties.createForTest(props),
Collections.emptyMap(),
MeterProvider.noop());
MeterProvider.noop())) {

testRetryableStatusCodes(() -> SPAN_DATA, spanExporter::export, server.traceRequests::size);
testDefaultRetryPolicy(() -> SPAN_DATA, spanExporter::export, server.traceRequests::size);
testRetryableStatusCodes(() -> SPAN_DATA, spanExporter::export, server.traceRequests::size);
testDefaultRetryPolicy(() -> SPAN_DATA, spanExporter::export, server.traceRequests::size);
}
}

@Test
Expand All @@ -91,12 +92,13 @@ void configureLogExporterRetryPolicy() {
props.put(
"otel.exporter.otlp.logs.certificate", certificate.certificateFile().getAbsolutePath());
props.put("otel.experimental.exporter.otlp.retry.enabled", "true");
LogExporter logExporter =
try (LogExporter logExporter =
LogExporterConfiguration.configureOtlpLogs(
DefaultConfigProperties.createForTest(props), MeterProvider.noop());
DefaultConfigProperties.createForTest(props), MeterProvider.noop())) {

testRetryableStatusCodes(() -> LOG_DATA, logExporter::export, server.logRequests::size);
testDefaultRetryPolicy(() -> LOG_DATA, logExporter::export, server.logRequests::size);
testRetryableStatusCodes(() -> LOG_DATA, logExporter::export, server.logRequests::size);
testDefaultRetryPolicy(() -> LOG_DATA, logExporter::export, server.logRequests::size);
}
}

private static <T> void testRetryableStatusCodes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
import io.opentelemetry.sdk.logs.LogProcessor;
import io.opentelemetry.sdk.logs.SdkLogEmitterProvider;
import io.opentelemetry.sdk.logs.data.LogData;
import java.io.Closeable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* An exporter is responsible for taking a collection of {@link LogData}s and transmitting them to
* their ultimate destination.
*/
public interface LogExporter {
public interface LogExporter extends Closeable {

/**
* Returns a {@link LogExporter} which delegates all exports to the {@code exporters} in order.
Expand Down Expand Up @@ -73,4 +75,10 @@ static LogExporter composite(Iterable<LogExporter> exporters) {
* @return a {@link CompletableResultCode} which is completed when shutdown completes
*/
CompletableResultCode shutdown();

/** Closes this {@link LogExporter}, releasing any resources. */
@Override
default void close() {
shutdown().join(10, TimeUnit.SECONDS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyCollection;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -38,6 +39,7 @@ class SimpleLogProcessorTest {
@BeforeEach
void setUp() {
logProcessor = SimpleLogProcessor.create(logExporter);
when(logExporter.export(anyCollection())).thenReturn(CompletableResultCode.ofSuccess());
when(logExporter.shutdown()).thenReturn(CompletableResultCode.ofSuccess());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.MetricData;
import java.io.Closeable;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;

/**
Expand All @@ -17,7 +19,7 @@
*
* <p>All OpenTelemetry exporters should allow access to a {@code MetricExporter} instance.
*/
public interface MetricExporter {
public interface MetricExporter extends Closeable {

/** Returns the preferred temporality for metrics. */
@Nullable
Expand Down Expand Up @@ -50,4 +52,10 @@ default AggregationTemporality getPreferredTemporality() {
* @return a {@link CompletableResultCode} which is completed when shutdown completes.
*/
CompletableResultCode shutdown();

/** Closes this {@link MetricExporter}, releasing any resources. */
@Override
default void close() {
shutdown().join(10, TimeUnit.SECONDS);
}
}

0 comments on commit 3cabcc7

Please sign in to comment.