Skip to content

Commit 6519fbd

Browse files
committed
add spring starter span logging
1 parent 701529d commit 6519fbd

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.spring.autoconfigure.internal.instrumentation.logging;
7+
8+
import static java.util.Collections.emptyList;
9+
10+
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
11+
import io.opentelemetry.instrumentation.spring.autoconfigure.internal.SdkEnabled;
12+
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
13+
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
14+
import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder;
15+
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
16+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
17+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
18+
import org.springframework.context.annotation.Bean;
19+
import org.springframework.context.annotation.Conditional;
20+
import org.springframework.context.annotation.Configuration;
21+
22+
/**
23+
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
24+
* any time.
25+
*/
26+
@Conditional(SdkEnabled.class)
27+
// for forwards compatibility with declarative configuration
28+
@ConditionalOnProperty(name = "otel.debug", havingValue = "true")
29+
@ConditionalOnClass(LoggingSpanExporter.class)
30+
@Configuration
31+
public class LoggingExporterAutoConfiguration {
32+
33+
@Bean
34+
public AutoConfigurationCustomizerProvider loggingOtelCustomizer() {
35+
return p ->
36+
p.addTracerProviderCustomizer(
37+
(builder, config) -> {
38+
enableLoggingExporter(builder, config);
39+
return builder;
40+
});
41+
}
42+
43+
public static void enableLoggingExporter(
44+
SdkTracerProviderBuilder builder, ConfigProperties config) {
45+
// don't install another instance if the user has already explicitly requested it.
46+
if (loggingExporterIsNotAlreadyConfigured(config)) {
47+
builder.addSpanProcessor(SimpleSpanProcessor.create(LoggingSpanExporter.create()));
48+
}
49+
}
50+
51+
private static boolean loggingExporterIsNotAlreadyConfigured(ConfigProperties config) {
52+
return !config.getList("otel.traces.exporter", emptyList()).contains("logging");
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.opentelemetry.instrumentation.spring.autoconfigure.internal.instrumentation.logging;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import io.opentelemetry.api.OpenTelemetry;
6+
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
7+
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
8+
import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension;
9+
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
10+
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
11+
import java.util.Collections;
12+
import org.junit.jupiter.api.Test;
13+
import org.junit.jupiter.api.extension.RegisterExtension;
14+
import org.springframework.boot.autoconfigure.AutoConfigurations;
15+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
16+
17+
class LoggingExporterAutoConfigurationTest {
18+
@RegisterExtension
19+
static final InstrumentationExtension testing = LibraryInstrumentationExtension.create();
20+
21+
private final ApplicationContextRunner runner =
22+
new ApplicationContextRunner()
23+
.withBean(
24+
ConfigProperties.class,
25+
() -> DefaultConfigProperties.createFromMap(Collections.emptyMap()))
26+
.withConfiguration(
27+
AutoConfigurations.of(
28+
LoggingExporterAutoConfiguration.class, OpenTelemetryAutoConfiguration.class));
29+
30+
@Test
31+
void instrumentationEnabled() {
32+
runner
33+
.withPropertyValues("otel.debug=true")
34+
.run(
35+
context ->
36+
assertThat(context.getBean(OpenTelemetry.class).toString())
37+
.containsOnlyOnce("cntscnt"));
38+
}
39+
40+
@Test
41+
void instrumentationDisabled() {
42+
runner
43+
.withPropertyValues("otel.debug=false")
44+
.run(
45+
context ->
46+
assertThat(context.getBean(OpenTelemetry.class).toString())
47+
.doesNotContain("cntscnt"));
48+
}
49+
}

testing-common/src/main/java/io/opentelemetry/instrumentation/testing/provider/TestExporterCustomizerProvider.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
import java.util.List;
2727

2828
public class TestExporterCustomizerProvider implements DeclarativeConfigurationCustomizerProvider {
29+
30+
@Override
31+
public int order() {
32+
return Integer.MIN_VALUE; // run before other customizers that might add exporters
33+
}
34+
2935
@Override
3036
public void customize(DeclarativeConfigurationCustomizer customizer) {
3137
if (TestSpanExporterComponentProvider.getSpanExporter() == null) {

0 commit comments

Comments
 (0)