Skip to content

Commit e6165b0

Browse files
committed
Add transport and connect timeout properties for OTLP logging
Closes spring-projectsgh-42528 Closes spring-projectsgh-42527
1 parent b997c0c commit e6165b0

File tree

20 files changed

+134
-83
lines changed

20 files changed

+134
-83
lines changed
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.actuate.autoconfigure.logging.opentelemetry;
17+
package org.springframework.boot.actuate.autoconfigure.logging;
1818

1919
import io.opentelemetry.api.OpenTelemetry;
2020
import io.opentelemetry.sdk.logs.LogRecordProcessor;
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.actuate.autoconfigure.logging.opentelemetry;
17+
package org.springframework.boot.actuate.autoconfigure.logging;
1818

1919
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
2020
import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder;

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/logging/opentelemetry/otlp/package-info.java

-20
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp;
17+
package org.springframework.boot.actuate.autoconfigure.logging.otlp;
1818

1919
import io.opentelemetry.api.OpenTelemetry;
2020
import io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporter;
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,21 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp;
17+
package org.springframework.boot.actuate.autoconfigure.logging.otlp;
1818

1919
import java.util.Locale;
2020

2121
import io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporter;
2222
import io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporterBuilder;
23+
import io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter;
24+
import io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporterBuilder;
2325

24-
import org.springframework.boot.actuate.autoconfigure.opentelemetry.otlp.Transport;
2526
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2627
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2728
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2829
import org.springframework.context.annotation.Bean;
2930
import org.springframework.context.annotation.Configuration;
31+
import org.springframework.util.Assert;
3032

3133
/**
3234
* Configurations imported by {@link OtlpLoggingAutoConfiguration}.
@@ -61,6 +63,9 @@ static class PropertiesOtlpLoggingConnectionDetails implements OtlpLoggingConnec
6163

6264
@Override
6365
public String getUrl(Transport transport) {
66+
Assert.state(transport == this.properties.getTransport(),
67+
"Requested transport %s doesn't match configured transport %s".formatted(transport,
68+
this.properties.getTransport()));
6469
return this.properties.getEndpoint();
6570
}
6671

@@ -69,18 +74,33 @@ public String getUrl(Transport transport) {
6974
}
7075

7176
@Configuration(proxyBeanMethods = false)
77+
@ConditionalOnMissingBean({ OtlpGrpcLogRecordExporter.class, OtlpHttpLogRecordExporter.class })
78+
@ConditionalOnBean(OtlpLoggingConnectionDetails.class)
7279
static class Exporters {
7380

74-
@ConditionalOnMissingBean(value = OtlpHttpLogRecordExporter.class,
75-
type = "io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter")
76-
@ConditionalOnBean(OtlpLoggingConnectionDetails.class)
7781
@Bean
82+
@ConditionalOnProperty(prefix = "management.otlp.logging", name = "transport", havingValue = "http",
83+
matchIfMissing = true)
7884
OtlpHttpLogRecordExporter otlpHttpLogRecordExporter(OtlpLoggingProperties properties,
7985
OtlpLoggingConnectionDetails connectionDetails) {
8086
OtlpHttpLogRecordExporterBuilder builder = OtlpHttpLogRecordExporter.builder()
8187
.setEndpoint(connectionDetails.getUrl(Transport.HTTP))
82-
.setCompression(properties.getCompression().name().toLowerCase(Locale.US))
83-
.setTimeout(properties.getTimeout());
88+
.setTimeout(properties.getTimeout())
89+
.setConnectTimeout(properties.getConnectTimeout())
90+
.setCompression(properties.getCompression().name().toLowerCase(Locale.US));
91+
properties.getHeaders().forEach(builder::addHeader);
92+
return builder.build();
93+
}
94+
95+
@Bean
96+
@ConditionalOnProperty(prefix = "management.otlp.logging", name = "transport", havingValue = "grpc")
97+
OtlpGrpcLogRecordExporter otlpGrpcLogRecordExporter(OtlpLoggingProperties properties,
98+
OtlpLoggingConnectionDetails connectionDetails) {
99+
OtlpGrpcLogRecordExporterBuilder builder = OtlpGrpcLogRecordExporter.builder()
100+
.setEndpoint(connectionDetails.getUrl(Transport.GRPC))
101+
.setTimeout(properties.getTimeout())
102+
.setConnectTimeout(properties.getConnectTimeout())
103+
.setCompression(properties.getCompression().name().toLowerCase(Locale.US));
84104
properties.getHeaders().forEach(builder::addHeader);
85105
return builder.build();
86106
}
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp;
17+
package org.springframework.boot.actuate.autoconfigure.logging.otlp;
1818

19-
import org.springframework.boot.actuate.autoconfigure.opentelemetry.otlp.Transport;
2019
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
2120

2221
/**
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp;
17+
package org.springframework.boot.actuate.autoconfigure.logging.otlp;
1818

1919
import java.time.Duration;
2020
import java.util.HashMap;
2121
import java.util.Map;
2222

23-
import org.springframework.boot.actuate.autoconfigure.opentelemetry.otlp.Compression;
2423
import org.springframework.boot.context.properties.ConfigurationProperties;
2524

2625
/**
@@ -45,6 +44,16 @@ public class OtlpLoggingProperties {
4544
*/
4645
private Duration timeout = Duration.ofSeconds(10);
4746

47+
/**
48+
* Connect timeout for the OTel collector connection.
49+
*/
50+
private Duration connectTimeout = Duration.ofSeconds(10);
51+
52+
/**
53+
* Transport used to send the spans.
54+
*/
55+
private Transport transport = Transport.HTTP;
56+
4857
/**
4958
* Method used to compress the payload.
5059
*/
@@ -71,6 +80,22 @@ public void setTimeout(Duration timeout) {
7180
this.timeout = timeout;
7281
}
7382

83+
public Duration getConnectTimeout() {
84+
return this.connectTimeout;
85+
}
86+
87+
public void setConnectTimeout(Duration connectTimeout) {
88+
this.connectTimeout = connectTimeout;
89+
}
90+
91+
public Transport getTransport() {
92+
return this.transport;
93+
}
94+
95+
public void setTransport(Transport transport) {
96+
this.transport = transport;
97+
}
98+
7499
public Compression getCompression() {
75100
return this.compression;
76101
}
@@ -83,4 +108,18 @@ public Map<String, String> getHeaders() {
83108
return this.headers;
84109
}
85110

111+
public enum Compression {
112+
113+
/**
114+
* Gzip compression.
115+
*/
116+
GZIP,
117+
118+
/**
119+
* No compression.
120+
*/
121+
NONE
122+
123+
}
124+
86125
}
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.actuate.autoconfigure.opentelemetry.otlp;
17+
package org.springframework.boot.actuate.autoconfigure.logging.otlp;
1818

1919
/**
2020
* Transport used to send OTLP data.
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
*/
1616

1717
/**
18-
* Auto-configuration for OpenTelemetry logging.
18+
* Auto-configuration for exporting logs with OTLP.
1919
*/
20-
package org.springframework.boot.actuate.autoconfigure.logging.opentelemetry;
20+
package org.springframework.boot.actuate.autoconfigure.logging.otlp;

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/opentelemetry/otlp/package-info.java

-20
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.actuate.autoconfigure.logging.opentelemetry;
17+
package org.springframework.boot.actuate.autoconfigure.logging;
1818

1919
import java.util.Collection;
2020
import java.util.concurrent.atomic.AtomicInteger;
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp;
17+
package org.springframework.boot.actuate.autoconfigure.logging.otlp;
1818

1919
import java.io.IOException;
2020
import java.nio.charset.StandardCharsets;
@@ -32,7 +32,7 @@
3232
import org.junit.jupiter.api.BeforeEach;
3333
import org.junit.jupiter.api.Test;
3434

35-
import org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.OpenTelemetryLoggingAutoConfiguration;
35+
import org.springframework.boot.actuate.autoconfigure.logging.OpenTelemetryLoggingAutoConfiguration;
3636
import org.springframework.boot.actuate.autoconfigure.opentelemetry.OpenTelemetryAutoConfiguration;
3737
import org.springframework.boot.autoconfigure.AutoConfigurations;
3838
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp;
17+
package org.springframework.boot.actuate.autoconfigure.logging.otlp;
1818

1919
import io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporter;
2020
import io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter;
@@ -24,8 +24,7 @@
2424
import org.junit.jupiter.params.ParameterizedTest;
2525
import org.junit.jupiter.params.provider.ValueSource;
2626

27-
import org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp.OtlpLoggingConfigurations.ConnectionDetails.PropertiesOtlpLoggingConnectionDetails;
28-
import org.springframework.boot.actuate.autoconfigure.opentelemetry.otlp.Transport;
27+
import org.springframework.boot.actuate.autoconfigure.logging.otlp.OtlpLoggingConfigurations.ConnectionDetails.PropertiesOtlpLoggingConnectionDetails;
2928
import org.springframework.boot.autoconfigure.AutoConfigurations;
3029
import org.springframework.boot.test.context.FilteredClassLoader;
3130
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
@@ -100,6 +99,40 @@ void shouldBackOffWhenCustomOtlpLoggingConnectionDetailsIsDefined() {
10099

101100
}
102101

102+
@Test
103+
void shouldUseHttpExporterIfTransportIsNotSet() {
104+
this.contextRunner.withPropertyValues("management.otlp.logging.endpoint=http://localhost:4318/v1/logs")
105+
.run((context) -> {
106+
assertThat(context).hasSingleBean(OtlpHttpLogRecordExporter.class)
107+
.hasSingleBean(LogRecordExporter.class);
108+
assertThat(context).doesNotHaveBean(OtlpGrpcLogRecordExporter.class);
109+
});
110+
}
111+
112+
@Test
113+
void shouldUseHttpExporterIfTransportIsSetToHttp() {
114+
this.contextRunner
115+
.withPropertyValues("management.otlp.logging.endpoint=http://localhost:4318/v1/logs",
116+
"management.otlp.logging.transport=http")
117+
.run((context) -> {
118+
assertThat(context).hasSingleBean(OtlpHttpLogRecordExporter.class)
119+
.hasSingleBean(LogRecordExporter.class);
120+
assertThat(context).doesNotHaveBean(OtlpGrpcLogRecordExporter.class);
121+
});
122+
}
123+
124+
@Test
125+
void shouldUseGrpcExporterIfTransportIsSetToGrpc() {
126+
this.contextRunner
127+
.withPropertyValues("management.otlp.logging.endpoint=http://localhost:4318/v1/logs",
128+
"management.otlp.logging.transport=grpc")
129+
.run((context) -> {
130+
assertThat(context).hasSingleBean(OtlpGrpcLogRecordExporter.class)
131+
.hasSingleBean(LogRecordExporter.class);
132+
assertThat(context).doesNotHaveBean(OtlpHttpLogRecordExporter.class);
133+
});
134+
}
135+
103136
@Configuration(proxyBeanMethods = false)
104137
public static class CustomHttpExporterConfiguration {
105138

spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/service/connection/otlp/GrafanaOpenTelemetryLoggingDockerComposeConnectionDetailsFactoryIntegrationTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package org.springframework.boot.docker.compose.service.connection.otlp;
1818

19-
import org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp.OtlpLoggingConnectionDetails;
20-
import org.springframework.boot.actuate.autoconfigure.opentelemetry.otlp.Transport;
19+
import org.springframework.boot.actuate.autoconfigure.logging.otlp.OtlpLoggingConnectionDetails;
20+
import org.springframework.boot.actuate.autoconfigure.logging.otlp.Transport;
2121
import org.springframework.boot.docker.compose.service.connection.test.DockerComposeTest;
2222
import org.springframework.boot.testsupport.container.TestImage;
2323

spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/service/connection/otlp/OpenTelemetryLoggingDockerComposeConnectionDetailsFactoryIntegrationTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package org.springframework.boot.docker.compose.service.connection.otlp;
1818

19-
import org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp.OtlpLoggingConnectionDetails;
20-
import org.springframework.boot.actuate.autoconfigure.opentelemetry.otlp.Transport;
19+
import org.springframework.boot.actuate.autoconfigure.logging.otlp.OtlpLoggingConnectionDetails;
20+
import org.springframework.boot.actuate.autoconfigure.logging.otlp.Transport;
2121
import org.springframework.boot.docker.compose.service.connection.test.DockerComposeTest;
2222
import org.springframework.boot.testsupport.container.TestImage;
2323

spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/service/connection/otlp/OpenTelemetryLoggingDockerComposeConnectionDetailsFactory.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package org.springframework.boot.docker.compose.service.connection.otlp;
1818

19-
import org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp.OtlpLoggingConnectionDetails;
20-
import org.springframework.boot.actuate.autoconfigure.opentelemetry.otlp.Transport;
19+
import org.springframework.boot.actuate.autoconfigure.logging.otlp.OtlpLoggingConnectionDetails;
20+
import org.springframework.boot.actuate.autoconfigure.logging.otlp.Transport;
2121
import org.springframework.boot.docker.compose.core.RunningService;
2222
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
2323
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource;
@@ -40,7 +40,7 @@ class OpenTelemetryLoggingDockerComposeConnectionDetailsFactory
4040

4141
OpenTelemetryLoggingDockerComposeConnectionDetailsFactory() {
4242
super(OPENTELEMETRY_IMAGE_NAMES,
43-
"org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp.OtlpLoggingAutoConfiguration");
43+
"org.springframework.boot.actuate.autoconfigure.logging.otlp.OtlpLoggingAutoConfiguration");
4444
}
4545

4646
@Override

spring-boot-project/spring-boot-testcontainers/src/dockerTest/java/org/springframework/boot/testcontainers/service/connection/otlp/GrafanaOpenTelemetryLoggingContainerConnectionDetailsFactoryIntegrationTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
import org.testcontainers.junit.jupiter.Testcontainers;
2323

2424
import org.springframework.beans.factory.annotation.Autowired;
25-
import org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp.OtlpLoggingAutoConfiguration;
26-
import org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp.OtlpLoggingConnectionDetails;
27-
import org.springframework.boot.actuate.autoconfigure.opentelemetry.otlp.Transport;
25+
import org.springframework.boot.actuate.autoconfigure.logging.otlp.OtlpLoggingAutoConfiguration;
26+
import org.springframework.boot.actuate.autoconfigure.logging.otlp.OtlpLoggingConnectionDetails;
27+
import org.springframework.boot.actuate.autoconfigure.logging.otlp.Transport;
2828
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
2929
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
3030
import org.springframework.boot.testsupport.container.TestImage;

0 commit comments

Comments
 (0)