Move code out of testcontainers-all into relevant modules

This commit is contained in:
Andy Wilkinson
2025-06-06 15:45:51 +01:00
committed by Phillip Webb
parent fb886a1818
commit 183a0fbdf5
30 changed files with 95 additions and 86 deletions

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2012-2025 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.tracing.testcontainers.otlp;
import org.junit.jupiter.api.Test;
import org.testcontainers.grafana.LgtmStackContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testsupport.container.TestImage;
import org.springframework.boot.tracing.autoconfigure.otlp.OtlpTracingAutoConfiguration;
import org.springframework.boot.tracing.autoconfigure.otlp.OtlpTracingConnectionDetails;
import org.springframework.boot.tracing.autoconfigure.otlp.Transport;
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 GrafanaOpenTelemetryTracingContainerConnectionDetailsFactory}.
*
* @author Eddú Meléndez
*/
@SpringJUnitConfig
@Testcontainers(disabledWithoutDocker = true)
class GrafanaOpenTelemetryTracingContainerConnectionDetailsFactoryIntegrationTests {
@Container
@ServiceConnection
static final LgtmStackContainer container = TestImage.container(LgtmStackContainer.class);
@Autowired
private OtlpTracingConnectionDetails connectionDetails;
@Test
void connectionCanBeMadeToOpenTelemetryContainer() {
assertThat(this.connectionDetails.getUrl(Transport.HTTP))
.isEqualTo("%s/v1/traces".formatted(container.getOtlpHttpUrl()));
assertThat(this.connectionDetails.getUrl(Transport.GRPC))
.isEqualTo("%s/v1/traces".formatted(container.getOtlpGrpcUrl()));
}
@Configuration(proxyBeanMethods = false)
@ImportAutoConfiguration(OtlpTracingAutoConfiguration.class)
static class TestConfiguration {
}
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2012-2025 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.tracing.testcontainers.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.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testsupport.container.TestImage;
import org.springframework.boot.tracing.autoconfigure.otlp.OtlpTracingAutoConfiguration;
import org.springframework.boot.tracing.autoconfigure.otlp.OtlpTracingConnectionDetails;
import org.springframework.boot.tracing.autoconfigure.otlp.Transport;
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 OpenTelemetryTracingContainerConnectionDetailsFactory}.
*
* @author Eddú Meléndez
*/
@SpringJUnitConfig
@Testcontainers(disabledWithoutDocker = true)
class OpenTelemetryTracingContainerConnectionDetailsFactoryIntegrationTests {
@Container
@ServiceConnection
static final GenericContainer<?> container = TestImage.OPENTELEMETRY.genericContainer()
.withExposedPorts(4317, 4318);
@Autowired
private OtlpTracingConnectionDetails connectionDetails;
@Test
void connectionCanBeMadeToOpenTelemetryContainer() {
assertThat(this.connectionDetails.getUrl(Transport.HTTP))
.isEqualTo("http://" + container.getHost() + ":" + container.getMappedPort(4318) + "/v1/traces");
assertThat(this.connectionDetails.getUrl(Transport.GRPC))
.isEqualTo("http://" + container.getHost() + ":" + container.getMappedPort(4317) + "/v1/traces");
}
@Configuration(proxyBeanMethods = false)
@ImportAutoConfiguration(OtlpTracingAutoConfiguration.class)
static class TestConfiguration {
}
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2012-2025 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.tracing.testcontainers.otlp;
import org.testcontainers.grafana.LgtmStackContainer;
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionDetailsFactory;
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionSource;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.tracing.autoconfigure.otlp.OtlpTracingConnectionDetails;
import org.springframework.boot.tracing.autoconfigure.otlp.Transport;
/**
* {@link ContainerConnectionDetailsFactory} to create
* {@link OtlpTracingConnectionDetails} from a
* {@link ServiceConnection @ServiceConnection}-annotated {@link LgtmStackContainer} using
* the {@code "grafana/otel-lgtm"} image.
*
* @author Eddú Meléndez
*/
class GrafanaOpenTelemetryTracingContainerConnectionDetailsFactory
extends ContainerConnectionDetailsFactory<LgtmStackContainer, OtlpTracingConnectionDetails> {
GrafanaOpenTelemetryTracingContainerConnectionDetailsFactory() {
super(ANY_CONNECTION_NAME, "org.springframework.boot.tracing.autoconfigure.otlp.OtlpTracingAutoConfiguration");
}
@Override
protected OtlpTracingConnectionDetails getContainerConnectionDetails(
ContainerConnectionSource<LgtmStackContainer> source) {
return new OpenTelemetryTracingContainerConnectionDetails(source);
}
private static final class OpenTelemetryTracingContainerConnectionDetails
extends ContainerConnectionDetails<LgtmStackContainer> implements OtlpTracingConnectionDetails {
private OpenTelemetryTracingContainerConnectionDetails(ContainerConnectionSource<LgtmStackContainer> source) {
super(source);
}
@Override
public String getUrl(Transport transport) {
String url = switch (transport) {
case HTTP -> getContainer().getOtlpHttpUrl();
case GRPC -> getContainer().getOtlpGrpcUrl();
};
return "%s/v1/traces".formatted(url);
}
}
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright 2012-2025 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.tracing.testcontainers.otlp;
import org.testcontainers.containers.Container;
import org.testcontainers.containers.GenericContainer;
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionDetailsFactory;
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionSource;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.tracing.autoconfigure.otlp.OtlpTracingConnectionDetails;
import org.springframework.boot.tracing.autoconfigure.otlp.Transport;
/**
* {@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 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.tracing.autoconfigure.otlp.OtlpTracingAutoConfiguration");
}
@Override
protected OtlpTracingConnectionDetails getContainerConnectionDetails(
ContainerConnectionSource<Container<?>> source) {
return new OpenTelemetryTracingContainerConnectionDetails(source);
}
private static final class OpenTelemetryTracingContainerConnectionDetails
extends ContainerConnectionDetails<Container<?>> implements OtlpTracingConnectionDetails {
private OpenTelemetryTracingContainerConnectionDetails(ContainerConnectionSource<Container<?>> source) {
super(source);
}
@Override
public String getUrl(Transport transport) {
int port = switch (transport) {
case HTTP -> OTLP_HTTP_PORT;
case GRPC -> OTLP_GRPC_PORT;
};
return "http://%s:%d/v1/traces".formatted(getContainer().getHost(), getContainer().getMappedPort(port));
}
}
}

View File

@@ -0,0 +1,20 @@
/*
* Copyright 2012-2025 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.
*/
/**
* Support for Testcontainers OpenTelemetry tracing service connections.
*/
package org.springframework.boot.tracing.testcontainers.otlp;

View File

@@ -1,7 +1,13 @@
# Environment Post Processors
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.boot.tracing.autoconfigure.LogCorrelationEnvironmentPostProcessor
# Connection Details Factories
org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsFactory=\
org.springframework.boot.tracing.testcontainers.otlp.GrafanaOpenTelemetryTracingContainerConnectionDetailsFactory,\
org.springframework.boot.tracing.testcontainers.otlp.OpenTelemetryTracingContainerConnectionDetailsFactory
# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.tracing.autoconfigure.OpenTelemetryEventPublisherBeansApplicationListener
# Environment Post Processors
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.boot.tracing.autoconfigure.LogCorrelationEnvironmentPostProcessor