Skip to content

Commit

Permalink
Support service connection for OtlpSpanExporter
Browse files Browse the repository at this point in the history
  • Loading branch information
eddumelendez committed Apr 26, 2023
1 parent ffe7872 commit b25c240
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
* define an {@link OtlpGrpcSpanExporter} and this auto-configuration will back off.
*
* @author Jonatan Ivanov
* @author Eddú Meléndez
* @since 3.1.0
*/
@AutoConfiguration
Expand All @@ -53,12 +54,19 @@
@EnableConfigurationProperties(OtlpProperties.class)
public class OtlpAutoConfiguration {

@Bean
@ConditionalOnMissingBean(OtlpTracingConnectionDetails.class)
OtlpTracingConnectionDetails otlpTracingConnectionDetails(OtlpProperties properties) {
return new PropertiesOtlpTracingConnectionDetails(properties);
}

@Bean
@ConditionalOnMissingBean(value = OtlpHttpSpanExporter.class,
type = "io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter")
OtlpHttpSpanExporter otlpHttpSpanExporter(OtlpProperties properties) {
OtlpHttpSpanExporter otlpHttpSpanExporter(OtlpProperties properties,
OtlpTracingConnectionDetails connectionDetails) {
OtlpHttpSpanExporterBuilder builder = OtlpHttpSpanExporter.builder()
.setEndpoint(properties.getEndpoint())
.setEndpoint(connectionDetails.getEndpoint())
.setTimeout(properties.getTimeout())
.setCompression(properties.getCompression().name().toLowerCase());
for (Entry<String, String> header : properties.getHeaders().entrySet()) {
Expand All @@ -67,4 +75,22 @@ OtlpHttpSpanExporter otlpHttpSpanExporter(OtlpProperties properties) {
return builder.build();
}

/**
* Adapts {@link OtlpProperties} to {@link OtlpTracingConnectionDetails}.
*/
static class PropertiesOtlpTracingConnectionDetails implements OtlpTracingConnectionDetails {

private final OtlpProperties properties;

PropertiesOtlpTracingConnectionDetails(OtlpProperties properties) {
this.properties = properties;
}

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

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2012-2023 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.actuate.autoconfigure.tracing.otlp;

import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;

/**
* Details required to establish a connection to a OpenTelemetry service.
*
* @author Eddú Meléndez
* @since 3.1.0
*/
public interface OtlpTracingConnectionDetails extends ConnectionDetails {

String getEndpoint();

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter;
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import okhttp3.HttpUrl;
import org.junit.jupiter.api.Test;

import org.springframework.boot.autoconfigure.AutoConfigurations;
Expand All @@ -33,6 +34,7 @@
* Tests for {@link OtlpAutoConfiguration}.
*
* @author Jonatan Ivanov
* @author Eddú Meléndez
*/
class OtlpAutoConfigurationTests {

Expand Down Expand Up @@ -89,6 +91,23 @@ void shouldBackOffWhenCustomGrpcExporterIsDefined() {
.hasSingleBean(SpanExporter.class));
}

@Test
void definesPropertiesBasedConnectionDetailsByDefault() {
this.contextRunner.run((context) -> assertThat(context)
.hasSingleBean(OtlpAutoConfiguration.PropertiesOtlpTracingConnectionDetails.class));
}

@Test
void testConnectionFactoryWithOverridesWhenUsingCustomConnectionDetails() {
this.contextRunner.withUserConfiguration(ConnectionDetailsConfiguration.class).run((context) -> {
assertThat(context).hasSingleBean(OtlpTracingConnectionDetails.class)
.doesNotHaveBean(OtlpAutoConfiguration.PropertiesOtlpTracingConnectionDetails.class);
OtlpHttpSpanExporter otlpHttpSpanExporter = context.getBean(OtlpHttpSpanExporter.class);
assertThat(otlpHttpSpanExporter).extracting("delegate.url")
.isEqualTo(HttpUrl.get("http://localhost:12345/v1/traces"));
});
}

@Configuration(proxyBeanMethods = false)
private static class CustomHttpExporterConfiguration {

Expand All @@ -109,4 +128,14 @@ OtlpGrpcSpanExporter customOtlpGrpcSpanExporter() {

}

@Configuration(proxyBeanMethods = false)
static class ConnectionDetailsConfiguration {

@Bean
OtlpTracingConnectionDetails otlpTracingConnectionDetails() {
return () -> "http://localhost:12345/v1/traces";
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2012-2023 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.testcontainers.service.connection.otlp;

import org.testcontainers.containers.Container;
import org.testcontainers.containers.GenericContainer;

import org.springframework.boot.actuate.autoconfigure.tracing.otlp.OtlpTracingConnectionDetails;
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionDetailsFactory;
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionSource;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;

/**
* {@link ContainerConnectionDetailsFactory} to create
* {@link OtlpTracingConnectionDetails} from a
* {@link ServiceConnection @ServiceConnection}-annotated {@link GenericContainer} using
* the {@code "otel/opentelemetry-collector-contrib"} image.
*
* @author Eddú Meléndez
*/
class OpenTelemetryTracingConnectionDetailsFactory
extends ContainerConnectionDetailsFactory<OtlpTracingConnectionDetails, Container<?>> {

OpenTelemetryTracingConnectionDetailsFactory() {
super("otel/opentelemetry-collector-contrib",
"org.springframework.boot.actuate.autoconfigure.tracing.otlp.OtlpAutoConfiguration");
}

@Override
protected OtlpTracingConnectionDetails getContainerConnectionDetails(
ContainerConnectionSource<Container<?>> source) {
return new OpenTelemetryTracingConnectionDetails(source);
}

private static final class OpenTelemetryTracingConnectionDetails extends ContainerConnectionDetails
implements OtlpTracingConnectionDetails {

private final String endpoint;

private OpenTelemetryTracingConnectionDetails(ContainerConnectionSource<Container<?>> source) {
super(source);
this.endpoint = "http://" + source.getContainer().getHost() + ":"
+ source.getContainer().getMappedPort(4318) + "/v1/traces";
}

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

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ org.springframework.boot.testcontainers.service.connection.liquibase.LiquibaseCo
org.springframework.boot.testcontainers.service.connection.mongo.MongoContainerConnectionDetailsFactory,\
org.springframework.boot.testcontainers.service.connection.neo4j.Neo4jContainerConnectionDetailsFactory,\
org.springframework.boot.testcontainers.service.connection.otlp.OpenTelemetryConnectionDetailsFactory,\
org.springframework.boot.testcontainers.service.connection.otlp.OpenTelemetryTracingConnectionDetailsFactory,\
org.springframework.boot.testcontainers.service.connection.r2dbc.MariaDbR2dbcContainerConnectionDetailsFactory,\
org.springframework.boot.testcontainers.service.connection.r2dbc.MsSqlServerR2dbcContainerConnectionDetailsFactory,\
org.springframework.boot.testcontainers.service.connection.r2dbc.MySqlR2dbcContainerConnectionDetailsFactory,\
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2012-2023 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.testcontainers.service.connection.otlp;

import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.tracing.otlp.OtlpAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.tracing.otlp.OtlpTracingConnectionDetails;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link OpenTelemetryTracingConnectionDetailsFactory}.
*
* @author Eddú Meléndez
*/
@SpringJUnitConfig
@Testcontainers(disabledWithoutDocker = true)
class OtlpTracingContainerConnectionDetailsFactoryIntegrationTests {

@Container
@ServiceConnection
static final GenericContainer<?> container = new GenericContainer<>("otel/opentelemetry-collector-contrib:0.75.0")
.withExposedPorts(4318);

@Autowired
private OtlpTracingConnectionDetails connectionDetails;

@Test
void connectionCanBeMadeToOpenTelemetryContainer() {
assertThat(this.connectionDetails.getEndpoint())
.isEqualTo("http://" + container.getHost() + ":" + container.getMappedPort(4318) + "/v1/traces");
}

@Configuration(proxyBeanMethods = false)
@ImportAutoConfiguration(OtlpAutoConfiguration.class)
static class TestConfiguration {

}

}

0 comments on commit b25c240

Please sign in to comment.