Skip to content

Commit

Permalink
Support Otlp Tracing's GRPC port from service connections
Browse files Browse the repository at this point in the history
Otlp Tracing's exporter is configured using `Transport`. Current support
for service connections read the mapped port for HTTP transport `4318`.
This commits adds support to read port for GRPC transport `4317`.
  • Loading branch information
eddumelendez committed Jul 5, 2024
1 parent 69630bb commit 7d37001
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporterBuilder;

import org.springframework.boot.actuate.autoconfigure.tracing.ConditionalOnEnabledTracing;
import org.springframework.boot.actuate.autoconfigure.tracing.otlp.OtlpProperties.Transport;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand All @@ -34,6 +35,7 @@
* Configurations imported by {@link OtlpAutoConfiguration}.
*
* @author Moritz Halbritter
* @author Eddú Meléndez
*/
class OtlpTracingConfigurations {

Expand Down Expand Up @@ -63,6 +65,11 @@ public String getUrl() {
return this.properties.getEndpoint();
}

@Override
public String getGrpcEndpoint() {
return this.properties.getEndpoint();
}

}

}
Expand All @@ -79,7 +86,7 @@ static class Exporters {
OtlpHttpSpanExporter otlpHttpSpanExporter(OtlpProperties properties,
OtlpTracingConnectionDetails connectionDetails) {
OtlpHttpSpanExporterBuilder builder = OtlpHttpSpanExporter.builder()
.setEndpoint(connectionDetails.getUrl())
.setEndpoint(resolveEndpoint(properties.getTransport(), connectionDetails))
.setTimeout(properties.getTimeout())
.setCompression(properties.getCompression().name().toLowerCase());
for (Entry<String, String> header : properties.getHeaders().entrySet()) {
Expand All @@ -93,7 +100,7 @@ OtlpHttpSpanExporter otlpHttpSpanExporter(OtlpProperties properties,
OtlpGrpcSpanExporter otlpGrpcSpanExporter(OtlpProperties properties,
OtlpTracingConnectionDetails connectionDetails) {
OtlpGrpcSpanExporterBuilder builder = OtlpGrpcSpanExporter.builder()
.setEndpoint(connectionDetails.getUrl())
.setEndpoint(resolveEndpoint(properties.getTransport(), connectionDetails))
.setTimeout(properties.getTimeout())
.setCompression(properties.getCompression().name().toLowerCase());
for (Entry<String, String> header : properties.getHeaders().entrySet()) {
Expand All @@ -102,6 +109,11 @@ OtlpGrpcSpanExporter otlpGrpcSpanExporter(OtlpProperties properties,
return builder.build();
}

private static String resolveEndpoint(OtlpProperties.Transport transport,
OtlpTracingConnectionDetails connectionDetails) {
return (transport == Transport.HTTP) ? connectionDetails.getUrl() : connectionDetails.getGrpcEndpoint();
}

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
Expand Down Expand Up @@ -32,4 +32,8 @@ public interface OtlpTracingConnectionDetails extends ConnectionDetails {
*/
String getUrl();

default String getGrpcEndpoint() {
return "http://localhost:4317/v1/traces";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class OpenTelemetryTracingDockerComposeConnectionDetailsFactoryIntegrationTests

@DockerComposeTest(composeFile = "otlp-compose.yaml", image = TestImage.OPENTELEMETRY)
void runCreatesConnectionDetails(OtlpTracingConnectionDetails connectionDetails) {
assertThat(connectionDetails.getGrpcEndpoint()).startsWith("http://").endsWith("/v1/traces");
assertThat(connectionDetails.getUrl()).startsWith("http://").endsWith("/v1/traces");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ services:
otlp:
image: '{imageName}'
ports:
- '4317'
- '4318'
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
Expand Down Expand Up @@ -30,7 +30,9 @@
class OpenTelemetryTracingDockerComposeConnectionDetailsFactory
extends DockerComposeConnectionDetailsFactory<OtlpTracingConnectionDetails> {

private static final int OTLP_PORT = 4318;
private static final int OTLP_GRPC_PORT = 4317;

private static final int OTLP_HTTP_PORT = 4318;

OpenTelemetryTracingDockerComposeConnectionDetailsFactory() {
super("otel/opentelemetry-collector-contrib",
Expand All @@ -47,17 +49,25 @@ private static final class OpenTelemetryTracingDockerComposeConnectionDetails ex

private final String host;

private final int port;
private final int grpcPort;

private final int httPort;

private OpenTelemetryTracingDockerComposeConnectionDetails(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/traces".formatted(this.host, this.port);
return "http://%s:%d/v1/traces".formatted(this.host, this.httPort);
}

@Override
public String getGrpcEndpoint() {
return "http://%s:%d/v1/traces".formatted(this.host, this.grpcPort);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ class OpenTelemetryTracingContainerConnectionDetailsFactoryIntegrationTests {

@Container
@ServiceConnection
static final GenericContainer<?> container = TestImage.OPENTELEMETRY.genericContainer().withExposedPorts(4318);
static final GenericContainer<?> container = TestImage.OPENTELEMETRY.genericContainer()
.withExposedPorts(4317, 4318);

@Autowired
private OtlpTracingConnectionDetails connectionDetails;

@Test
void connectionCanBeMadeToOpenTelemetryContainer() {
assertThat(this.connectionDetails.getGrpcEndpoint())
.isEqualTo("http://" + container.getHost() + ":" + container.getMappedPort(4317) + "/v1/traces");
assertThat(this.connectionDetails.getUrl())
.isEqualTo("http://" + container.getHost() + ":" + container.getMappedPort(4318) + "/v1/traces");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
Expand Down Expand Up @@ -35,6 +35,10 @@
class OpenTelemetryTracingContainerConnectionDetailsFactory
extends ContainerConnectionDetailsFactory<Container<?>, OtlpTracingConnectionDetails> {

private static final int OTLP_GRPC_PORT = 4317;

private static final int OTLP_HTTP_PORT = 4318;

OpenTelemetryTracingContainerConnectionDetailsFactory() {
super("otel/opentelemetry-collector-contrib",
"org.springframework.boot.actuate.autoconfigure.tracing.otlp.OtlpAutoConfiguration");
Expand All @@ -55,7 +59,14 @@ private OpenTelemetryTracingContainerConnectionDetails(ContainerConnectionSource

@Override
public String getUrl() {
return "http://%s:%d/v1/traces".formatted(getContainer().getHost(), getContainer().getMappedPort(4318));
return "http://%s:%d/v1/traces".formatted(getContainer().getHost(),
getContainer().getMappedPort(OTLP_HTTP_PORT));
}

@Override
public String getGrpcEndpoint() {
return "http://%s:%d/v1/traces".formatted(getContainer().getHost(),
getContainer().getMappedPort(OTLP_GRPC_PORT));
}

}
Expand Down

0 comments on commit 7d37001

Please sign in to comment.