Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.spring.autoconfigure.internal.instrumentation.logging;

import static java.util.Collections.emptyList;

import io.opentelemetry.exporter.logging.LoggingSpanExporter;
import io.opentelemetry.instrumentation.spring.autoconfigure.internal.SdkEnabled;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
@Conditional(SdkEnabled.class)
// to match "otel.javaagent.debug" system property
@ConditionalOnProperty(name = "otel.spring-starter.debug", havingValue = "true")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point - also added the missing entry for #14449

@ConditionalOnClass(LoggingSpanExporter.class)
@Configuration
public class LoggingExporterAutoConfiguration {

@Bean
public AutoConfigurationCustomizerProvider loggingOtelCustomizer() {
return p ->
p.addTracerProviderCustomizer(
(builder, config) -> {
enableLoggingExporter(builder, config);
return builder;
});
}

public static void enableLoggingExporter(
SdkTracerProviderBuilder builder, ConfigProperties config) {
// don't install another instance if the user has already explicitly requested it.
if (loggingExporterIsNotAlreadyConfigured(config)) {
builder.addSpanProcessor(SimpleSpanProcessor.create(LoggingSpanExporter.create()));
}
}

private static boolean loggingExporterIsNotAlreadyConfigured(ConfigProperties config) {
return !config.getList("otel.traces.exporter", emptyList()).contains("logging");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.spring.autoconfigure.internal.instrumentation.logging;

import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
import java.util.Collections;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

class LoggingExporterAutoConfigurationTest {
private final ApplicationContextRunner runner =
new ApplicationContextRunner()
.withBean(
ConfigProperties.class,
() -> DefaultConfigProperties.createFromMap(Collections.emptyMap()))
.withConfiguration(
AutoConfigurations.of(
LoggingExporterAutoConfiguration.class, OpenTelemetryAutoConfiguration.class));

@Test
void instrumentationEnabled() {
runner
.withPropertyValues("otel.spring-starter.debug=true", "otel.traces.exporter=none")
.run(
context ->
assertThat(context.getBean(OpenTelemetry.class).toString())
.containsOnlyOnce("LoggingSpanExporter"));
}

@Test
void alreadyAdded() {
runner
.withPropertyValues("otel.spring-starter.debug=true", "otel.traces.exporter=logging")
.run(
context ->
assertThat(context.getBean(OpenTelemetry.class).toString())
.containsOnlyOnce("LoggingSpanExporter"));
}

@Test
void instrumentationDisabled() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and one more test for debugDisable would be nice to be sure

Suggested change
void instrumentationDisabled() {
void debugUnset() {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

runner.run(
context ->
assertThat(context.getBean(OpenTelemetry.class).toString())
.doesNotContain("LoggingSpanExporter"));
}
}
Loading