Skip to content

Commit 5b10714

Browse files
committed
Merge pull request #35107 from eddumelendez
* pr/35107: Polish "Add service connection from Testcontainers Zipkin" Add service connection from Testcontainers Zipkin Closes gh-35107
2 parents 27169bd + 9b5a264 commit 5b10714

File tree

7 files changed

+171
-1
lines changed

7 files changed

+171
-1
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,9 @@ The following service connection factories are provided in the `spring-boot-test
981981

982982
| `RedisConnectionDetails`
983983
| Containers named "redis"
984+
985+
| `ZipkinConnectionDetails`
986+
| Containers named "openzipkin/zipkin"
984987
|===
985988

986989
[TIP]

spring-boot-project/spring-boot-testcontainers/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ dependencies {
1212
api(project(":spring-boot-project:spring-boot-autoconfigure"))
1313
api("org.testcontainers:testcontainers")
1414

15+
optional(project(":spring-boot-project:spring-boot-actuator-autoconfigure"))
1516
optional("org.springframework:spring-test")
1617
optional("org.springframework.data:spring-data-mongodb")
1718
optional("org.springframework.data:spring-data-neo4j")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.testcontainers.service.connection.zipkin;
18+
19+
import org.testcontainers.containers.Container;
20+
import org.testcontainers.containers.GenericContainer;
21+
22+
import org.springframework.boot.actuate.autoconfigure.tracing.zipkin.ZipkinConnectionDetails;
23+
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionDetailsFactory;
24+
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionSource;
25+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
26+
27+
/**
28+
* {@link ContainerConnectionDetailsFactory} to create {@link ZipkinConnectionDetails}
29+
* from a {@link ServiceConnection @ServiceConnection}-annotated {@link GenericContainer}
30+
* using the {@code "openzipkin/zipkin"} image.
31+
*
32+
* @author Eddú Meléndez
33+
* @author Moritz Halbritter
34+
*/
35+
class ZipkinContainerConnectionDetailsFactory
36+
extends ContainerConnectionDetailsFactory<Container<?>, ZipkinConnectionDetails> {
37+
38+
private static final int ZIPKIN_PORT = 9411;
39+
40+
ZipkinContainerConnectionDetailsFactory() {
41+
super("openzipkin/zipkin",
42+
"org.springframework.boot.actuate.autoconfigure.tracing.zipkin.ZipkinAutoConfiguration");
43+
}
44+
45+
@Override
46+
protected ZipkinConnectionDetails getContainerConnectionDetails(ContainerConnectionSource<Container<?>> source) {
47+
return new ZipkinContainerConnectionDetails(source);
48+
}
49+
50+
/**
51+
* {@link ZipkinConnectionDetails} backed by a {@link ContainerConnectionSource}.
52+
*/
53+
private static class ZipkinContainerConnectionDetails extends ContainerConnectionDetails<Container<?>>
54+
implements ZipkinConnectionDetails {
55+
56+
ZipkinContainerConnectionDetails(ContainerConnectionSource<Container<?>> source) {
57+
super(source);
58+
}
59+
60+
@Override
61+
public String getSpanEndpoint() {
62+
return "http://" + getContainer().getHost() + ":" + getContainer().getMappedPort(ZIPKIN_PORT)
63+
+ "/api/v2/spans";
64+
}
65+
66+
}
67+
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Support for testcontainers Zipkin service connections.
19+
*/
20+
package org.springframework.boot.testcontainers.service.connection.zipkin;

spring-boot-project/spring-boot-testcontainers/src/main/resources/META-INF/spring.factories

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ org.springframework.boot.testcontainers.service.connection.r2dbc.MySqlR2dbcConta
2424
org.springframework.boot.testcontainers.service.connection.r2dbc.OracleR2dbcContainerConnectionDetailsFactory,\
2525
org.springframework.boot.testcontainers.service.connection.r2dbc.PostgresR2dbcContainerConnectionDetailsFactory,\
2626
org.springframework.boot.testcontainers.service.connection.redis.RedisContainerConnectionDetailsFactory,\
27-
org.springframework.boot.testcontainers.service.connection.redpanda.RedpandaContainerConnectionDetailsFactory
27+
org.springframework.boot.testcontainers.service.connection.redpanda.RedpandaContainerConnectionDetailsFactory,\
28+
org.springframework.boot.testcontainers.service.connection.zipkin.ZipkinContainerConnectionDetailsFactory
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.testcontainers.service.connection.zipkin;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.testcontainers.containers.GenericContainer;
21+
import org.testcontainers.junit.jupiter.Container;
22+
import org.testcontainers.junit.jupiter.Testcontainers;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.actuate.autoconfigure.tracing.zipkin.ZipkinAutoConfiguration;
26+
import org.springframework.boot.actuate.autoconfigure.tracing.zipkin.ZipkinConnectionDetails;
27+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
28+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
29+
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
30+
import org.springframework.context.annotation.Configuration;
31+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
32+
33+
import static org.assertj.core.api.Assertions.assertThat;
34+
35+
/**
36+
* Tests for {@link ZipkinContainerConnectionDetailsFactory}.
37+
*
38+
* @author Eddú Meléndez
39+
* @author Moritz Halbritter
40+
*/
41+
@SpringJUnitConfig
42+
@Testcontainers(disabledWithoutDocker = true)
43+
class ZipkinContainerConnectionDetailsFactoryIntegrationTests {
44+
45+
@Container
46+
@ServiceConnection
47+
static final GenericContainer<?> zipkin = new GenericContainer<>(DockerImageNames.zipkin()).withExposedPorts(9411);
48+
49+
@Autowired(required = false)
50+
private ZipkinConnectionDetails connectionDetails;
51+
52+
@Test
53+
void connectionCanBeMadeToZipkinContainer() {
54+
assertThat(this.connectionDetails).isNotNull();
55+
assertThat(this.connectionDetails.getSpanEndpoint())
56+
.startsWith("http://" + zipkin.getHost() + ":" + zipkin.getMappedPort(9411));
57+
}
58+
59+
@Configuration(proxyBeanMethods = false)
60+
@ImportAutoConfiguration(ZipkinAutoConfiguration.class)
61+
static class TestConfiguration {
62+
63+
}
64+
65+
}

spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/testcontainers/DockerImageNames.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*
2424
* @author Stephane Nicoll
2525
* @author Eddú Meléndez
26+
* @author Moritz Halbritter
2627
* @since 2.3.6
2728
*/
2829
public final class DockerImageNames {
@@ -53,6 +54,8 @@ public final class DockerImageNames {
5354

5455
private static final String REGISTRY_VERSION = "2.7.1";
5556

57+
private static final String ZIPKIN_VERSION = "2.24.1";
58+
5659
private DockerImageNames() {
5760
}
5861

@@ -163,4 +166,13 @@ public static DockerImageName registry() {
163166
return DockerImageName.parse("registry").withTag(REGISTRY_VERSION);
164167
}
165168

169+
/**
170+
* Return a {@link DockerImageName} suitable for running Zipkin.
171+
* @return a docker image name for running Zipkin
172+
* @since 3.1.0
173+
*/
174+
public static DockerImageName zipkin() {
175+
return DockerImageName.parse("openzipkin/zipkin").withTag(ZIPKIN_VERSION);
176+
}
177+
166178
}

0 commit comments

Comments
 (0)