Skip to content

Commit 32ea07c

Browse files
authored
Change Spring starter default otlp protocol from gRPC to http/protobuf (#10212)
1 parent 4784045 commit 32ea07c

File tree

6 files changed

+112
-20
lines changed

6 files changed

+112
-20
lines changed

instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/otlp/OtlpExporterUtil.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@
88
import io.opentelemetry.exporter.otlp.internal.OtlpConfigUtil;
99
import java.time.Duration;
1010
import java.util.Map;
11-
import java.util.Objects;
1211
import java.util.function.BiConsumer;
1312
import java.util.function.Function;
1413
import java.util.function.Supplier;
14+
import org.slf4j.Logger;
15+
import org.slf4j.LoggerFactory;
1516

1617
class OtlpExporterUtil {
1718
private OtlpExporterUtil() {}
1819

20+
private static final Logger logger = LoggerFactory.getLogger(OtlpExporterUtil.class);
21+
1922
static <G, H, E> E applySignalProperties(
2023
String dataType,
2124
OtlpExporterProperties properties,
@@ -39,7 +42,18 @@ static <G, H, E> E applySignalProperties(
3942
G grpcBuilder = newGrpcBuilder.get();
4043
H httpBuilder = newHttpBuilder.get();
4144

42-
boolean isHttpProtobuf = Objects.equals(protocol, OtlpConfigUtil.PROTOCOL_HTTP_PROTOBUF);
45+
boolean isHttpProtobuf = !"grpc".equals(protocol);
46+
47+
if (protocol != null
48+
&& !OtlpConfigUtil.PROTOCOL_GRPC.equals(protocol)
49+
&& !OtlpConfigUtil.PROTOCOL_HTTP_PROTOBUF.equals(protocol)) {
50+
logger.warn(
51+
"Unknown OTLP protocol '"
52+
+ protocol
53+
+ "', using '"
54+
+ OtlpConfigUtil.PROTOCOL_HTTP_PROTOBUF
55+
+ "'.");
56+
}
4357

4458
String endpoint = signalProperties.getEndpoint();
4559
if (endpoint == null) {

instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/MetricExporterAutoConfigurationTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import static org.assertj.core.api.Assertions.assertThat;
99

1010
import io.opentelemetry.exporter.logging.LoggingMetricExporter;
11-
import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter;
11+
import io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporter;
1212
import io.opentelemetry.instrumentation.spring.autoconfigure.exporters.logging.LoggingMetricExporterAutoConfiguration;
1313
import io.opentelemetry.instrumentation.spring.autoconfigure.exporters.otlp.OtlpMetricExporterAutoConfiguration;
1414
import org.junit.jupiter.api.Test;
@@ -28,7 +28,7 @@ class MetricExporterAutoConfigurationTest {
2828
void defaultConfiguration() {
2929
contextRunner.run(
3030
context -> {
31-
assertThat(context.getBean("otelOtlpMetricExporter", OtlpGrpcMetricExporter.class))
31+
assertThat(context.getBean("otelOtlpMetricExporter", OtlpHttpMetricExporter.class))
3232
.as("OTLP exporter is enabled by default")
3333
.isNotNull();
3434
assertThat(context.containsBean("otelLoggingMetricExporter"))
@@ -43,7 +43,7 @@ void loggingEnabledByConfiguration() {
4343
.withPropertyValues("otel.exporter.logging.enabled=true")
4444
.run(
4545
context -> {
46-
assertThat(context.getBean("otelOtlpMetricExporter", OtlpGrpcMetricExporter.class))
46+
assertThat(context.getBean("otelOtlpMetricExporter", OtlpHttpMetricExporter.class))
4747
.as("OTLP exporter is present even with logging enabled")
4848
.isNotNull();
4949
assertThat(context.getBean("otelLoggingMetricExporter", LoggingMetricExporter.class))

instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/SpanExporterAutoConfigurationTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import static org.assertj.core.api.Assertions.assertThat;
99

1010
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
11-
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
11+
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter;
1212
import io.opentelemetry.instrumentation.spring.autoconfigure.exporters.logging.LoggingSpanExporterAutoConfiguration;
1313
import io.opentelemetry.instrumentation.spring.autoconfigure.exporters.otlp.OtlpSpanExporterAutoConfiguration;
1414
import org.junit.jupiter.api.Test;
@@ -28,7 +28,7 @@ class SpanExporterAutoConfigurationTest {
2828
void defaultConfiguration() {
2929
contextRunner.run(
3030
context -> {
31-
assertThat(context.getBean("otelOtlpSpanExporter", OtlpGrpcSpanExporter.class))
31+
assertThat(context.getBean("otelOtlpSpanExporter", OtlpHttpSpanExporter.class))
3232
.as("OTLP exporter is enabled by default")
3333
.isNotNull();
3434
assertThat(context.containsBean("otelLoggingSpanExporter"))
@@ -43,7 +43,7 @@ void loggingEnabledByConfiguration() {
4343
.withPropertyValues("otel.exporter.logging.enabled=true")
4444
.run(
4545
context -> {
46-
assertThat(context.getBean("otelOtlpSpanExporter", OtlpGrpcSpanExporter.class))
46+
assertThat(context.getBean("otelOtlpSpanExporter", OtlpHttpSpanExporter.class))
4747
.as("OTLP exporter is present even with logging enabled")
4848
.isNotNull();
4949
assertThat(context.getBean("otelLoggingSpanExporter", LoggingSpanExporter.class))

instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/otlp/OtlpLogExporterAutoConfigurationTest.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

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

10+
import io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporter;
1011
import io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter;
1112
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
1213
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
14+
import org.junit.jupiter.api.DisplayName;
1315
import org.junit.jupiter.api.Test;
1416
import org.springframework.boot.autoconfigure.AutoConfigurations;
1517
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
@@ -64,11 +66,37 @@ void otlpLogsDisabled() {
6466
}
6567

6668
@Test
67-
void loggerPresentByDefault() {
69+
void otlpHttpUsedByDefault() {
6870
runner.run(
6971
context ->
7072
assertThat(
71-
context.getBean("otelOtlpLogRecordExporter", OtlpGrpcLogRecordExporter.class))
73+
context.getBean("otelOtlpLogRecordExporter", OtlpHttpLogRecordExporter.class))
7274
.isNotNull());
7375
}
76+
77+
@Test
78+
@DisplayName("use grpc when protocol set")
79+
void useGrpc() {
80+
runner
81+
.withPropertyValues("otel.exporter.otlp.protocol=grpc")
82+
.run(
83+
context ->
84+
assertThat(
85+
context.getBean(
86+
"otelOtlpLogRecordExporter", OtlpGrpcLogRecordExporter.class))
87+
.isNotNull());
88+
}
89+
90+
@Test
91+
@DisplayName("use http when unknown protocol set")
92+
void useHttpWhenAnUnknownProtocolIsSet() {
93+
runner
94+
.withPropertyValues("otel.exporter.otlp.protocol=unknown")
95+
.run(
96+
context ->
97+
assertThat(
98+
context.getBean(
99+
"otelOtlpLogRecordExporter", OtlpHttpLogRecordExporter.class))
100+
.isNotNull());
101+
}
74102
}

instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/otlp/OtlpMetricExporterAutoConfigurationTest.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

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

10+
import io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporter;
1011
import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter;
1112
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
13+
import org.junit.jupiter.api.DisplayName;
1214
import org.junit.jupiter.api.Test;
1315
import org.springframework.boot.autoconfigure.AutoConfigurations;
1416
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
@@ -25,19 +27,41 @@ class OtlpMetricExporterAutoConfigurationTest {
2527
void otlpEnabled() {
2628
runner
2729
.withPropertyValues("otel.exporter.otlp.enabled=true")
30+
.run(
31+
context ->
32+
assertThat(context.getBean("otelOtlpMetricExporter", OtlpHttpMetricExporter.class))
33+
.isNotNull());
34+
}
35+
36+
@Test
37+
@DisplayName("use grpc when protocol set")
38+
void useGrpc() {
39+
runner
40+
.withPropertyValues("otel.exporter.otlp.protocol=grpc")
2841
.run(
2942
context ->
3043
assertThat(context.getBean("otelOtlpMetricExporter", OtlpGrpcMetricExporter.class))
3144
.isNotNull());
3245
}
3346

47+
@Test
48+
@DisplayName("use http when unknown protocol set")
49+
void useHttpWhenAnUnknownProtocolIsSet() {
50+
runner
51+
.withPropertyValues("otel.exporter.otlp.protocol=unknown")
52+
.run(
53+
context ->
54+
assertThat(context.getBean("otelOtlpMetricExporter", OtlpHttpMetricExporter.class))
55+
.isNotNull());
56+
}
57+
3458
@Test
3559
void otlpMetricsEnabled() {
3660
runner
3761
.withPropertyValues("otel.exporter.otlp.metrics.enabled=true")
3862
.run(
3963
context ->
40-
assertThat(context.getBean("otelOtlpMetricExporter", OtlpGrpcMetricExporter.class))
64+
assertThat(context.getBean("otelOtlpMetricExporter", OtlpHttpMetricExporter.class))
4165
.isNotNull());
4266
}
4367

@@ -63,10 +87,10 @@ void otlpMetricsDisabled() {
6387
}
6488

6589
@Test
66-
void exporterPresentByDefault() {
90+
void otlpHttpUsedByDefault() {
6791
runner.run(
6892
context ->
69-
assertThat(context.getBean("otelOtlpMetricExporter", OtlpGrpcMetricExporter.class))
93+
assertThat(context.getBean("otelOtlpMetricExporter", OtlpHttpMetricExporter.class))
7094
.isNotNull());
7195
}
7296
}

instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/exporters/otlp/OtlpSpanExporterAutoConfigurationTest.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static org.assertj.core.api.Assertions.assertThat;
99

1010
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
11+
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter;
1112
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporterBuilder;
1213
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
1314
import io.opentelemetry.instrumentation.spring.autoconfigure.MapConverterTestAutoConfiguration;
@@ -31,17 +32,16 @@ class OtlpSpanExporterAutoConfigurationTest {
3132
AutoConfigurations.of(
3233
OpenTelemetryAutoConfiguration.class,
3334
OtlpSpanExporterAutoConfiguration.class,
34-
MapConverterTestAutoConfiguration.class))
35-
.withBean(OtlpHttpSpanExporterBuilder.class, () -> otlpHttpSpanExporterBuilder);
35+
MapConverterTestAutoConfiguration.class));
3636

3737
@Test
38-
@DisplayName("when exporters are ENABLED should initialize OtlpGrpcSpanExporter bean")
38+
@DisplayName("when exporters are ENABLED should initialize OtlpHttpSpanExporter bean")
3939
void otlpEnabled() {
4040
this.contextRunner
4141
.withPropertyValues("otel.exporter.otlp.enabled=true")
4242
.run(
4343
context ->
44-
assertThat(context.getBean("otelOtlpSpanExporter", OtlpGrpcSpanExporter.class))
44+
assertThat(context.getBean("otelOtlpSpanExporter", OtlpHttpSpanExporter.class))
4545
.isNotNull());
4646

4747
Mockito.verifyNoMoreInteractions(otlpHttpSpanExporterBuilder);
@@ -53,7 +53,7 @@ void otlpTracesEnabled() {
5353
.withPropertyValues("otel.exporter.otlp.traces.enabled=true")
5454
.run(
5555
context ->
56-
assertThat(context.getBean("otelOtlpSpanExporter", OtlpGrpcSpanExporter.class))
56+
assertThat(context.getBean("otelOtlpSpanExporter", OtlpHttpSpanExporter.class))
5757
.isNotNull());
5858
}
5959

@@ -80,18 +80,19 @@ void otlpTracesDisabled() {
8080
}
8181

8282
@Test
83-
@DisplayName("when otlp enabled property is MISSING should initialize OtlpGrpcSpanExporter bean")
83+
@DisplayName("when otlp enabled property is MISSING should initialize OtlpHttpSpanExporter bean")
8484
void exporterPresentByDefault() {
8585
this.contextRunner.run(
8686
context ->
87-
assertThat(context.getBean("otelOtlpSpanExporter", OtlpGrpcSpanExporter.class))
87+
assertThat(context.getBean("otelOtlpSpanExporter", OtlpHttpSpanExporter.class))
8888
.isNotNull());
8989
}
9090

9191
@Test
9292
@DisplayName("use http/protobuf when protocol set")
9393
void useHttp() {
9494
this.contextRunner
95+
.withBean(OtlpHttpSpanExporterBuilder.class, () -> otlpHttpSpanExporterBuilder)
9596
.withPropertyValues(
9697
"otel.exporter.otlp.enabled=true",
9798
"otel.exporter.otlp.protocol=http/protobuf",
@@ -113,6 +114,7 @@ void useHttp() {
113114
@DisplayName("use http/protobuf with environment variables for headers using the MapConverter")
114115
void useHttpWithEnv() {
115116
this.contextRunner
117+
.withBean(OtlpHttpSpanExporterBuilder.class, () -> otlpHttpSpanExporterBuilder)
116118
.withPropertyValues(
117119
"otel.exporter.otlp.enabled=true", "otel.exporter.otlp.protocol=http/protobuf")
118120
// are similar to environment variables in that they use the same converters
@@ -125,6 +127,30 @@ void useHttpWithEnv() {
125127
Mockito.verifyNoMoreInteractions(otlpHttpSpanExporterBuilder);
126128
}
127129

130+
@Test
131+
@DisplayName("use grpc when protocol set")
132+
void useGrpc() {
133+
this.contextRunner
134+
.withPropertyValues("otel.exporter.otlp.protocol=grpc")
135+
.run(
136+
context ->
137+
assertThat(context.getBean(OtlpGrpcSpanExporter.class))
138+
.as("Should contain the gRPC span exporter when grpc is set")
139+
.isNotNull());
140+
}
141+
142+
@Test
143+
@DisplayName("use http when unknown protocol set")
144+
void useHttpWhenAnUnknownProtocolIsSet() {
145+
this.contextRunner
146+
.withPropertyValues("otel.exporter.otlp.protocol=unknown")
147+
.run(
148+
context ->
149+
assertThat(context.getBean(OtlpHttpSpanExporter.class))
150+
.as("Should contain the http span exporter when an unknown is set")
151+
.isNotNull());
152+
}
153+
128154
@Test
129155
@DisplayName("logging exporter can still be configured")
130156
void loggingExporter() {

0 commit comments

Comments
 (0)