mirror of
https://github.com/spring-projects/spring-boot.git
synced 2026-09-24 02:09:05 +00:00
Merge pull request #49002 from joaquinjsb
* pr/49002: Polish "Add customizers for OtlpHttpLogRecordExporterBuilder and OtlpGrpcLogRecordExporterBuilder" Add customizers for OtlpHttpLogRecordExporterBuilder and OtlpGrpcLogRecordExporterBuilder Closes gh-49002
This commit is contained in:
+4
@@ -162,6 +162,10 @@ include-code::AutoConfiguredOpenTelemetrySdkConfiguration[]
|
||||
The javadoc:org.springframework.boot.opentelemetry.autoconfigure.logging.OpenTelemetryLoggingAutoConfiguration[] configures OpenTelemetry's javadoc:io.opentelemetry.sdk.logs.SdkLoggerProvider[].
|
||||
Exporting logs via OTLP is supported through the javadoc:org.springframework.boot.opentelemetry.autoconfigure.logging.otlp.OtlpLoggingAutoConfiguration[], which enables OTLP log exporting over HTTP or gRPC.
|
||||
|
||||
NOTE: If you need to apply advanced customizations to OTLP log record exporters, consider registering javadoc:org.springframework.boot.opentelemetry.autoconfigure.logging.otlp.OtlpHttpLogRecordExporterBuilderCustomizer[] or javadoc:org.springframework.boot.opentelemetry.autoconfigure.logging.otlp.OtlpGrpcLogRecordExporterBuilderCustomizer[] beans.
|
||||
These will be invoked before the creation of the javadoc:io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporter[] or javadoc:io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter[].
|
||||
The customizers take precedence over anything applied by the auto-configuration.
|
||||
|
||||
However, while there is a `SdkLoggerProvider` bean, Spring Boot doesn't support bridging logs to this bean out of the box.
|
||||
This can be done with 3rd-party log bridges, as described in the xref:reference:actuator/loggers.adoc#actuator.loggers.opentelemetry[Logging with OpenTelemetry] section.
|
||||
|
||||
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2012-present the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.opentelemetry.autoconfigure.logging.otlp;
|
||||
|
||||
import io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporterBuilder;
|
||||
|
||||
/**
|
||||
* Callback interface that can be implemented by beans wishing to customize the
|
||||
* {@link OtlpGrpcLogRecordExporterBuilder} whilst retaining default auto-configuration.
|
||||
*
|
||||
* @author Joaquin Santana
|
||||
* @since 4.1.0
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface OtlpGrpcLogRecordExporterBuilderCustomizer {
|
||||
|
||||
/**
|
||||
* Customize the {@link OtlpGrpcLogRecordExporterBuilder}.
|
||||
* @param builder the builder to customize
|
||||
*/
|
||||
void customize(OtlpGrpcLogRecordExporterBuilder builder);
|
||||
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2012-present the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.opentelemetry.autoconfigure.logging.otlp;
|
||||
|
||||
import io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporterBuilder;
|
||||
|
||||
/**
|
||||
* Callback interface that can be implemented by beans wishing to customize the
|
||||
* {@link OtlpHttpLogRecordExporterBuilder} whilst retaining default auto-configuration.
|
||||
*
|
||||
* @author Joaquin Santana
|
||||
* @since 4.1.0
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface OtlpHttpLogRecordExporterBuilderCustomizer {
|
||||
|
||||
/**
|
||||
* Customize the {@link OtlpHttpLogRecordExporterBuilder}.
|
||||
* @param builder the builder to customize
|
||||
*/
|
||||
void customize(OtlpHttpLogRecordExporterBuilder builder);
|
||||
|
||||
}
|
||||
+6
-2
@@ -88,7 +88,8 @@ final class OtlpLoggingConfigurations {
|
||||
@ConditionalOnProperty(name = "management.opentelemetry.logging.export.otlp.transport", havingValue = "http",
|
||||
matchIfMissing = true)
|
||||
OtlpHttpLogRecordExporter otlpHttpLogRecordExporter(OtlpLoggingProperties properties,
|
||||
OtlpLoggingConnectionDetails connectionDetails, ObjectProvider<MeterProvider> meterProvider) {
|
||||
OtlpLoggingConnectionDetails connectionDetails, ObjectProvider<MeterProvider> meterProvider,
|
||||
ObjectProvider<OtlpHttpLogRecordExporterBuilderCustomizer> customizers) {
|
||||
OtlpHttpLogRecordExporterBuilder builder = OtlpHttpLogRecordExporter.builder()
|
||||
.setEndpoint(connectionDetails.getUrl(Transport.HTTP))
|
||||
.setTimeout(properties.getTimeout())
|
||||
@@ -96,13 +97,15 @@ final class OtlpLoggingConfigurations {
|
||||
.setCompression(properties.getCompression().name().toLowerCase(Locale.US));
|
||||
properties.getHeaders().forEach(builder::addHeader);
|
||||
meterProvider.ifAvailable(builder::setMeterProvider);
|
||||
customizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(name = "management.opentelemetry.logging.export.otlp.transport", havingValue = "grpc")
|
||||
OtlpGrpcLogRecordExporter otlpGrpcLogRecordExporter(OtlpLoggingProperties properties,
|
||||
OtlpLoggingConnectionDetails connectionDetails, ObjectProvider<MeterProvider> meterProvider) {
|
||||
OtlpLoggingConnectionDetails connectionDetails, ObjectProvider<MeterProvider> meterProvider,
|
||||
ObjectProvider<OtlpGrpcLogRecordExporterBuilderCustomizer> customizers) {
|
||||
OtlpGrpcLogRecordExporterBuilder builder = OtlpGrpcLogRecordExporter.builder()
|
||||
.setEndpoint(connectionDetails.getUrl(Transport.GRPC))
|
||||
.setTimeout(properties.getTimeout())
|
||||
@@ -110,6 +113,7 @@ final class OtlpLoggingConfigurations {
|
||||
.setCompression(properties.getCompression().name().toLowerCase(Locale.US));
|
||||
properties.getHeaders().forEach(builder::addHeader);
|
||||
meterProvider.ifAvailable(builder::setMeterProvider);
|
||||
customizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
|
||||
+42
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.boot.opentelemetry.autoconfigure.logging.otlp;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@@ -224,6 +225,47 @@ class OtlpLoggingAutoConfigurationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCustomizeHttpTransportWithOtlpHttpLogRecordExporterBuilderCustomizer() {
|
||||
Duration connectTimeout = Duration.ofMinutes(20);
|
||||
Duration timeout = Duration.ofMinutes(10);
|
||||
this.contextRunner
|
||||
.withBean("httpCustomizer1", OtlpHttpLogRecordExporterBuilderCustomizer.class,
|
||||
() -> (builder) -> builder.setConnectTimeout(connectTimeout))
|
||||
.withBean("httpCustomizer2", OtlpHttpLogRecordExporterBuilderCustomizer.class,
|
||||
() -> (builder) -> builder.setTimeout(timeout))
|
||||
.withPropertyValues("management.opentelemetry.logging.export.otlp.endpoint=http://localhost:4318/v1/logs")
|
||||
.run((context) -> {
|
||||
assertThat(context).hasSingleBean(OtlpHttpLogRecordExporter.class)
|
||||
.hasSingleBean(LogRecordExporter.class);
|
||||
OtlpHttpLogRecordExporter exporter = context.getBean(OtlpHttpLogRecordExporter.class);
|
||||
assertThat(exporter).extracting("delegate.httpSender.client")
|
||||
.hasFieldOrPropertyWithValue("connectTimeoutMillis", (int) connectTimeout.toMillis())
|
||||
.hasFieldOrPropertyWithValue("callTimeoutMillis", (int) timeout.toMillis());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCustomizeGrpcTransportWhenEnabledWithOtlpGrpcLogRecordExporterBuilderCustomizer() {
|
||||
Duration timeout = Duration.ofMinutes(10);
|
||||
Duration connectTimeout = Duration.ofMinutes(20);
|
||||
this.contextRunner
|
||||
.withBean("grpcCustomizer1", OtlpGrpcLogRecordExporterBuilderCustomizer.class,
|
||||
() -> (builder) -> builder.setConnectTimeout(connectTimeout))
|
||||
.withBean("grpcCustomizer2", OtlpGrpcLogRecordExporterBuilderCustomizer.class,
|
||||
() -> (builder) -> builder.setTimeout(timeout))
|
||||
.withPropertyValues("management.opentelemetry.logging.export.otlp.endpoint=http://localhost:4318/v1/logs",
|
||||
"management.opentelemetry.logging.export.otlp.transport=grpc")
|
||||
.run((context) -> {
|
||||
assertThat(context).hasSingleBean(OtlpGrpcLogRecordExporter.class)
|
||||
.hasSingleBean(LogRecordExporter.class);
|
||||
OtlpGrpcLogRecordExporter exporter = context.getBean(OtlpGrpcLogRecordExporter.class);
|
||||
assertThat(exporter).extracting("delegate.grpcSender.client")
|
||||
.hasFieldOrPropertyWithValue("connectTimeoutMillis", (int) connectTimeout.toMillis())
|
||||
.hasFieldOrPropertyWithValue("callTimeoutMillis", (int) timeout.toMillis());
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
public static class MultipleSdkLoggerProviderBuilderCustomizersConfig {
|
||||
|
||||
|
||||
@@ -1710,6 +1710,7 @@ bom {
|
||||
javadoc("opentelemetry-sdk-logs", version -> "https://javadoc.io/doc/io.opentelemetry/opentelemetry-sdk-logs/%s".formatted(version), "io.opentelemetry.sdk.logs")
|
||||
javadoc("opentelemetry-sdk-metrics", version -> "https://javadoc.io/doc/io.opentelemetry/opentelemetry-sdk-metrics/%s".formatted(version), "io.opentelemetry.sdk.metrics")
|
||||
javadoc("opentelemetry-sdk-trace", version -> "https://javadoc.io/doc/io.opentelemetry/opentelemetry-sdk-trace/%s".formatted(version), "io.opentelemetry.sdk.trace")
|
||||
javadoc("opentelemetry-exporter-otlp", version -> "https://javadoc.io/doc/io.opentelemetry/opentelemetry-exporter-otlp/%s".formatted(version), "io.opentelemetry.exporter.otlp")
|
||||
releaseNotes("https://github.com/open-telemetry/opentelemetry-java/releases/tag/v{version}")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user