Skip to content

Commit

Permalink
[improve][test] Disable OTel autoconfigured exporters in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lhotari committed Nov 1, 2024
1 parent 6bd0308 commit f902ce2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader;
import java.util.Map;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
Expand Down Expand Up @@ -207,6 +208,9 @@ private Pair<OpenTelemetrySdk, InMemoryMetricReader> buildOpenTelemetryAndReader
var metricReader = InMemoryMetricReader.create();
var openTelemetry = AutoConfiguredOpenTelemetrySdk.builder()
.disableShutdownHook()
.addPropertiesSupplier(() -> Map.of("otel.metrics.exporter", "none",
"otel.traces.exporter", "none",
"otel.logs.exporter", "none"))
.addMeterProviderCustomizer((builder, __) -> builder.registerMetricReader(metricReader))
.build()
.getOpenTelemetrySdk();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,20 @@ public static Consumer<AutoConfiguredOpenTelemetrySdkBuilder> getOpenTelemetrySd
sdkBuilder.addMeterProviderCustomizer(
(meterProviderBuilder, __) -> meterProviderBuilder.registerMetricReader(reader));
sdkBuilder.disableShutdownHook();
disableExporters(sdkBuilder);
sdkBuilder.addPropertiesSupplier(
() -> Map.of(OpenTelemetryService.OTEL_SDK_DISABLED_KEY, "false",
"otel.java.enabled.resource.providers", "none"));
};
}

public static void disableExporters(AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder) {
sdkBuilder.addPropertiesSupplier(() ->
Map.of("otel.metrics.exporter", "none",
"otel.traces.exporter", "none",
"otel.logs.exporter", "none"));
}

public static void assertMetricDoubleSumValue(Collection<MetricData> metrics, String metricName,
Attributes attributes, Consumer<Double> valueConsumer) {
assertThat(metrics)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,8 @@ protected void initializePulsarServices(SpyConfig spyConfig, Builder builder) {
var reader = InMemoryMetricReader.create();
openTelemetryMetricReader(reader);
registerCloseable(reader);
openTelemetrySdkBuilderCustomizer = BrokerOpenTelemetryTestUtil.getOpenTelemetrySdkBuilderConsumer(reader);
openTelemetrySdkBuilderCustomizer =
BrokerOpenTelemetryTestUtil.getOpenTelemetrySdkBuilderConsumer(reader);
} else {
openTelemetrySdkBuilderCustomizer = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ private static Consumer<AutoConfiguredOpenTelemetrySdkBuilder> getBuilderCustomi
(sdkMeterProviderBuilder, __) -> sdkMeterProviderBuilder.registerMetricReader(extraReader));
}
autoConfigurationCustomizer.disableShutdownHook();
// disable all autoconfigured exporters
autoConfigurationCustomizer.addPropertiesSupplier(() ->
Map.of("otel.metrics.exporter", "none",
"otel.traces.exporter", "none",
"otel.logs.exporter", "none"));
autoConfigurationCustomizer.addPropertiesSupplier(() -> extraProperties);
};
}
Expand Down

0 comments on commit f902ce2

Please sign in to comment.