Skip to content

Commit

Permalink
Add transport selection to OtlpLoggingConnectionDetails
Browse files Browse the repository at this point in the history
Closes gh-42171
  • Loading branch information
mhalbritter committed Sep 6, 2024
1 parent 8835593 commit 861e520
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand Down Expand Up @@ -59,7 +60,7 @@ static class PropertiesOtlpLoggingConnectionDetails implements OtlpLoggingConnec
}

@Override
public String getUrl() {
public String getUrl(Transport transport) {
return this.properties.getEndpoint();
}

Expand All @@ -77,7 +78,7 @@ static class Exporters {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;
Expand Down Expand Up @@ -57,7 +58,7 @@ void shouldSupplyBeans() {
.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);
});
Expand Down Expand Up @@ -124,7 +125,7 @@ public static class CustomOtlpLogsConnectionDetails {

@Bean
public OtlpLoggingConnectionDetails customOtlpLogsConnectionDetails() {
return () -> "https://otel.example.com/v1/logs";
return (transport) -> "https://otel.example.com/v1/logs";
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,7 +31,9 @@
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",
Expand All @@ -47,17 +50,24 @@ private static final class OpenTelemetryLoggingDockerComposeConnectionDetails ex

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);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

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;
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
Expand All @@ -31,10 +32,15 @@
* 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");
Expand All @@ -54,8 +60,12 @@ private OpenTelemetryLoggingContainerConnectionDetails(ContainerConnectionSource
}

@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));
}

}
Expand Down

0 comments on commit 861e520

Please sign in to comment.