Add transport selection to OtlpLoggingConnectionDetails

Closes gh-42171
This commit is contained in:
Moritz Halbritter 2024-09-06 13:36:29 +02:00
parent 8835593c07
commit 861e5209ef
7 changed files with 45 additions and 15 deletions

View File

@ -21,6 +21,7 @@ import java.util.Locale;
import io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporter;
import io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporterBuilder;
import org.springframework.boot.actuate.autoconfigure.opentelemetry.otlp.Transport;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@ -59,7 +60,7 @@ final class OtlpLoggingConfigurations {
}
@Override
public String getUrl() {
public String getUrl(Transport transport) {
return this.properties.getEndpoint();
}
@ -77,7 +78,7 @@ final class OtlpLoggingConfigurations {
OtlpHttpLogRecordExporter otlpHttpLogRecordExporter(OtlpLoggingProperties properties,
OtlpLoggingConnectionDetails connectionDetails) {
OtlpHttpLogRecordExporterBuilder builder = OtlpHttpLogRecordExporter.builder()
.setEndpoint(connectionDetails.getUrl())
.setEndpoint(connectionDetails.getUrl(Transport.HTTP))
.setCompression(properties.getCompression().name().toLowerCase(Locale.US))
.setTimeout(properties.getTimeout());
properties.getHeaders().forEach(builder::addHeader);

View File

@ -16,6 +16,7 @@
package org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp;
import org.springframework.boot.actuate.autoconfigure.opentelemetry.otlp.Transport;
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
/**
@ -28,8 +29,9 @@ public interface OtlpLoggingConnectionDetails extends ConnectionDetails {
/**
* Address to where logs will be published.
* @param transport the transport to use
* @return the address to where logs will be published
*/
String getUrl();
String getUrl(Transport transport);
}

View File

@ -25,6 +25,7 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp.OtlpLoggingConfigurations.ConnectionDetails.PropertiesOtlpLoggingConnectionDetails;
import org.springframework.boot.actuate.autoconfigure.opentelemetry.otlp.Transport;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
@ -57,7 +58,7 @@ class OtlpLoggingAutoConfigurationTests {
.run((context) -> {
assertThat(context).hasSingleBean(OtlpLoggingConnectionDetails.class);
OtlpLoggingConnectionDetails connectionDetails = context.getBean(OtlpLoggingConnectionDetails.class);
assertThat(connectionDetails.getUrl()).isEqualTo("http://localhost:4318/v1/logs");
assertThat(connectionDetails.getUrl(Transport.HTTP)).isEqualTo("http://localhost:4318/v1/logs");
assertThat(context).hasSingleBean(OtlpHttpLogRecordExporter.class)
.hasSingleBean(LogRecordExporter.class);
});
@ -124,7 +125,7 @@ class OtlpLoggingAutoConfigurationTests {
@Bean
public OtlpLoggingConnectionDetails customOtlpLogsConnectionDetails() {
return () -> "https://otel.example.com/v1/logs";
return (transport) -> "https://otel.example.com/v1/logs";
}
}

View File

@ -17,6 +17,7 @@
package org.springframework.boot.docker.compose.service.connection.otlp;
import org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp.OtlpLoggingConnectionDetails;
import org.springframework.boot.actuate.autoconfigure.opentelemetry.otlp.Transport;
import org.springframework.boot.docker.compose.service.connection.test.DockerComposeTest;
import org.springframework.boot.testsupport.container.TestImage;
@ -32,7 +33,8 @@ class OpenTelemetryLoggingDockerComposeConnectionDetailsFactoryIntegrationTests
@DockerComposeTest(composeFile = "otlp-compose.yaml", image = TestImage.OPENTELEMETRY)
void runCreatesConnectionDetails(OtlpLoggingConnectionDetails connectionDetails) {
assertThat(connectionDetails.getUrl()).startsWith("http://").endsWith("/v1/logs");
assertThat(connectionDetails.getUrl(Transport.HTTP)).startsWith("http://").endsWith("/v1/logs");
assertThat(connectionDetails.getUrl(Transport.GRPC)).startsWith("http://").endsWith("/v1/logs");
}
}

View File

@ -17,6 +17,7 @@
package org.springframework.boot.docker.compose.service.connection.otlp;
import org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp.OtlpLoggingConnectionDetails;
import org.springframework.boot.actuate.autoconfigure.opentelemetry.otlp.Transport;
import org.springframework.boot.docker.compose.core.RunningService;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource;
@ -30,7 +31,9 @@ import org.springframework.boot.docker.compose.service.connection.DockerComposeC
class OpenTelemetryLoggingDockerComposeConnectionDetailsFactory
extends DockerComposeConnectionDetailsFactory<OtlpLoggingConnectionDetails> {
private static final int OTLP_PORT = 4318;
private static final int OTLP_GRPC_PORT = 4317;
private static final int OTLP_HTTP_PORT = 4318;
OpenTelemetryLoggingDockerComposeConnectionDetailsFactory() {
super("otel/opentelemetry-collector-contrib",
@ -47,17 +50,24 @@ class OpenTelemetryLoggingDockerComposeConnectionDetailsFactory
private final String host;
private final int port;
private final int grpcPort;
private final int httPort;
private OpenTelemetryLoggingDockerComposeConnectionDetails(RunningService source) {
super(source);
this.host = source.host();
this.port = source.ports().get(OTLP_PORT);
this.grpcPort = source.ports().get(OTLP_GRPC_PORT);
this.httPort = source.ports().get(OTLP_HTTP_PORT);
}
@Override
public String getUrl() {
return "http://%s:%d/v1/logs".formatted(this.host, this.port);
public String getUrl(Transport transport) {
int port = switch (transport) {
case HTTP -> this.httPort;
case GRPC -> this.grpcPort;
};
return "http://%s:%d/v1/logs".formatted(this.host, port);
}
}

View File

@ -23,6 +23,7 @@ import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp.OtlpLoggingConnectionDetails;
import org.springframework.boot.actuate.autoconfigure.opentelemetry.otlp.Transport;
import org.springframework.boot.actuate.autoconfigure.tracing.otlp.OtlpAutoConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
@ -43,15 +44,18 @@ class OpenTelemetryLoggingContainerConnectionDetailsFactoryIntegrationTests {
@Container
@ServiceConnection
static final GenericContainer<?> container = TestImage.OPENTELEMETRY.genericContainer().withExposedPorts(4318);
static final GenericContainer<?> container = TestImage.OPENTELEMETRY.genericContainer()
.withExposedPorts(4317, 4318);
@Autowired
private OtlpLoggingConnectionDetails connectionDetails;
@Test
void connectionCanBeMadeToOpenTelemetryContainer() {
assertThat(this.connectionDetails.getUrl())
assertThat(this.connectionDetails.getUrl(Transport.HTTP))
.isEqualTo("http://" + container.getHost() + ":" + container.getMappedPort(4318) + "/v1/logs");
assertThat(this.connectionDetails.getUrl(Transport.GRPC))
.isEqualTo("http://" + container.getHost() + ":" + container.getMappedPort(4317) + "/v1/logs");
}
@Configuration(proxyBeanMethods = false)

View File

@ -20,6 +20,7 @@ import org.testcontainers.containers.Container;
import org.testcontainers.containers.GenericContainer;
import org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp.OtlpLoggingConnectionDetails;
import org.springframework.boot.actuate.autoconfigure.opentelemetry.otlp.Transport;
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionDetailsFactory;
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionSource;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
@ -31,10 +32,15 @@ import org.springframework.boot.testcontainers.service.connection.ServiceConnect
* the {@code "otel/opentelemetry-collector-contrib"} image.
*
* @author Eddú Meléndez
* @author Moritz Halbritter
*/
class OpenTelemetryLoggingContainerConnectionDetailsFactory
extends ContainerConnectionDetailsFactory<Container<?>, OtlpLoggingConnectionDetails> {
private static final int OTLP_GRPC_PORT = 4317;
private static final int OTLP_HTTP_PORT = 4318;
OpenTelemetryLoggingContainerConnectionDetailsFactory() {
super("otel/opentelemetry-collector-contrib",
"org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp.OtlpLoggingAutoConfiguration");
@ -54,8 +60,12 @@ class OpenTelemetryLoggingContainerConnectionDetailsFactory
}
@Override
public String getUrl() {
return "http://%s:%d/v1/logs".formatted(getContainer().getHost(), getContainer().getMappedPort(4318));
public String getUrl(Transport transport) {
int port = switch (transport) {
case HTTP -> OTLP_HTTP_PORT;
case GRPC -> OTLP_GRPC_PORT;
};
return "http://%s:%d/v1/logs".formatted(getContainer().getHost(), getContainer().getMappedPort(port));
}
}