Skip to content

Commit 7adf843

Browse files
eddumelendezmhalbritter
authored andcommitted
Add service connection from Opentelemetry Collector for Logging
Adds ConnectionDetails from Docker Compose and Testcontainers. See gh-41324
1 parent 9a81796 commit 7adf843

File tree

9 files changed

+238
-1
lines changed

9 files changed

+238
-1
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/logging/opentelemetry/otlp/OtlpLoggingProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class OtlpLoggingProperties {
3535
/**
3636
* URL to the OTel collector's HTTP API.
3737
*/
38-
private String endpoint;
38+
private String endpoint = "http://localhost:4318/v1/logs";
3939

4040
/**
4141
* Call timeout for the OTel Collector to process an exported batch of data. This
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2012-2024 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.docker.compose.service.connection.otlp;
18+
19+
import org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp.OtlpLoggingConnectionDetails;
20+
import org.springframework.boot.docker.compose.service.connection.test.DockerComposeTest;
21+
import org.springframework.boot.testsupport.container.TestImage;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
/**
26+
* Integration tests for
27+
* {@link OpenTelemetryLoggingDockerComposeConnectionDetailsFactory}.
28+
*
29+
* @author Eddú Meléndez
30+
*/
31+
class OpenTelemetryLoggingDockerComposeConnectionDetailsFactoryIntegrationTests {
32+
33+
@DockerComposeTest(composeFile = "otlp-compose.yaml", image = TestImage.OPENTELEMETRY)
34+
void runCreatesConnectionDetails(OtlpLoggingConnectionDetails connectionDetails) {
35+
assertThat(connectionDetails.getEndpoint()).startsWith("http://").endsWith("/v1/logs");
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2012-2024 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.docker.compose.service.connection.otlp;
18+
19+
import org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp.OtlpLoggingConnectionDetails;
20+
import org.springframework.boot.docker.compose.core.RunningService;
21+
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
22+
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource;
23+
24+
/**
25+
* {@link DockerComposeConnectionDetailsFactory} to create
26+
* {@link OtlpLoggingConnectionDetails} for an OTLP service.
27+
*
28+
* @author Eddú Meléndez
29+
*/
30+
class OpenTelemetryLoggingDockerComposeConnectionDetailsFactory
31+
extends DockerComposeConnectionDetailsFactory<OtlpLoggingConnectionDetails> {
32+
33+
private static final int OTLP_PORT = 4318;
34+
35+
OpenTelemetryLoggingDockerComposeConnectionDetailsFactory() {
36+
super("otel/opentelemetry-collector-contrib",
37+
"org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp.OtlpLoggingAutoConfiguration");
38+
}
39+
40+
@Override
41+
protected OtlpLoggingConnectionDetails getDockerComposeConnectionDetails(DockerComposeConnectionSource source) {
42+
return new OpenTelemetryLoggingDockerComposeConnectionDetails(source.getRunningService());
43+
}
44+
45+
private static final class OpenTelemetryLoggingDockerComposeConnectionDetails extends DockerComposeConnectionDetails
46+
implements OtlpLoggingConnectionDetails {
47+
48+
private final String host;
49+
50+
private final int port;
51+
52+
private OpenTelemetryLoggingDockerComposeConnectionDetails(RunningService source) {
53+
super(source);
54+
this.host = source.host();
55+
this.port = source.ports().get(OTLP_PORT);
56+
}
57+
58+
@Override
59+
public String getEndpoint() {
60+
return "http://%s:%d/v1/logs".formatted(this.host, this.port);
61+
}
62+
63+
}
64+
65+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ org.springframework.boot.docker.compose.service.connection.oracle.OracleFreeJdbc
2323
org.springframework.boot.docker.compose.service.connection.oracle.OracleXeJdbcDockerComposeConnectionDetailsFactory,\
2424
org.springframework.boot.docker.compose.service.connection.oracle.OracleFreeR2dbcDockerComposeConnectionDetailsFactory,\
2525
org.springframework.boot.docker.compose.service.connection.oracle.OracleXeR2dbcDockerComposeConnectionDetailsFactory,\
26+
org.springframework.boot.docker.compose.service.connection.otlp.OpenTelemetryLoggingDockerComposeConnectionDetailsFactory,\
2627
org.springframework.boot.docker.compose.service.connection.otlp.OpenTelemetryMetricsDockerComposeConnectionDetailsFactory,\
2728
org.springframework.boot.docker.compose.service.connection.otlp.OpenTelemetryTracingDockerComposeConnectionDetailsFactory,\
2829
org.springframework.boot.docker.compose.service.connection.postgres.PostgresJdbcDockerComposeConnectionDetailsFactory,\

spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/features/dev-services.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ The following service connections are currently supported:
107107
| `Neo4jConnectionDetails`
108108
| Containers named "neo4j" or "bitnami/neo4j"
109109

110+
| `OtlpLoggingConnectionDetails`
111+
| Containers named "otel/opentelemetry-collector-contrib"
112+
110113
| `OtlpMetricsConnectionDetails`
111114
| Containers named "otel/opentelemetry-collector-contrib", "grafana/otel-lgtm"
112115

spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/testing/testcontainers.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ The following service connection factories are provided in the `spring-boot-test
7171
| `Neo4jConnectionDetails`
7272
| Containers of type `Neo4jContainer`
7373

74+
| `OtlpLoggingConnectionDetails`
75+
| Containers named "otel/opentelemetry-collector-contrib"
76+
7477
| `OtlpMetricsConnectionDetails`
7578
| Containers named "otel/opentelemetry-collector-contrib" or of type `LgtmStackContainer`
7679

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2012-2024 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.otlp;
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.logging.opentelemetry.otlp.OtlpLoggingConnectionDetails;
26+
import org.springframework.boot.actuate.autoconfigure.tracing.otlp.OtlpAutoConfiguration;
27+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
28+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
29+
import org.springframework.boot.testsupport.container.TestImage;
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 OpenTelemetryLoggingContainerConnectionDetailsFactory}.
37+
*
38+
* @author Eddú Meléndez
39+
*/
40+
@SpringJUnitConfig
41+
@Testcontainers(disabledWithoutDocker = true)
42+
class OpenTelemetryLoggingContainerConnectionDetailsFactoryIntegrationTests {
43+
44+
@Container
45+
@ServiceConnection
46+
static final GenericContainer<?> container = TestImage.OPENTELEMETRY.genericContainer().withExposedPorts(4318);
47+
48+
@Autowired
49+
private OtlpLoggingConnectionDetails connectionDetails;
50+
51+
@Test
52+
void connectionCanBeMadeToOpenTelemetryContainer() {
53+
assertThat(this.connectionDetails.getEndpoint())
54+
.isEqualTo("http://" + container.getHost() + ":" + container.getMappedPort(4318) + "/v1/logs");
55+
}
56+
57+
@Configuration(proxyBeanMethods = false)
58+
@ImportAutoConfiguration(OtlpAutoConfiguration.class)
59+
static class TestConfiguration {
60+
61+
}
62+
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2012-2024 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.otlp;
18+
19+
import org.testcontainers.containers.Container;
20+
import org.testcontainers.containers.GenericContainer;
21+
22+
import org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp.OtlpLoggingConnectionDetails;
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
29+
* {@link OtlpLoggingConnectionDetails} from a
30+
* {@link ServiceConnection @ServiceConnection}-annotated {@link GenericContainer} using
31+
* the {@code "otel/opentelemetry-collector-contrib"} image.
32+
*
33+
* @author Eddú Meléndez
34+
*/
35+
class OpenTelemetryLoggingContainerConnectionDetailsFactory
36+
extends ContainerConnectionDetailsFactory<Container<?>, OtlpLoggingConnectionDetails> {
37+
38+
OpenTelemetryLoggingContainerConnectionDetailsFactory() {
39+
super("otel/opentelemetry-collector-contrib",
40+
"org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp.OtlpLoggingAutoConfiguration");
41+
}
42+
43+
@Override
44+
protected OtlpLoggingConnectionDetails getContainerConnectionDetails(
45+
ContainerConnectionSource<Container<?>> source) {
46+
return new OpenTelemetryLoggingContainerConnectionDetails(source);
47+
}
48+
49+
private static final class OpenTelemetryLoggingContainerConnectionDetails
50+
extends ContainerConnectionDetails<Container<?>> implements OtlpLoggingConnectionDetails {
51+
52+
private OpenTelemetryLoggingContainerConnectionDetails(ContainerConnectionSource<Container<?>> source) {
53+
super(source);
54+
}
55+
56+
@Override
57+
public String getEndpoint() {
58+
return "http://%s:%d/v1/logs".formatted(getContainer().getHost(), getContainer().getMappedPort(4318));
59+
}
60+
61+
}
62+
63+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ org.springframework.boot.testcontainers.service.connection.mongo.MongoContainerC
2525
org.springframework.boot.testcontainers.service.connection.neo4j.Neo4jContainerConnectionDetailsFactory,\
2626
org.springframework.boot.testcontainers.service.connection.otlp.GrafanaOpenTelemetryMetricsContainerConnectionDetailsFactory,\
2727
org.springframework.boot.testcontainers.service.connection.otlp.GrafanaOpenTelemetryTracingContainerConnectionDetailsFactory,\
28+
org.springframework.boot.testcontainers.service.connection.otlp.OpenTelemetryLoggingContainerConnectionDetailsFactory,\
2829
org.springframework.boot.testcontainers.service.connection.otlp.OpenTelemetryMetricsContainerConnectionDetailsFactory,\
2930
org.springframework.boot.testcontainers.service.connection.otlp.OpenTelemetryTracingContainerConnectionDetailsFactory,\
3031
org.springframework.boot.testcontainers.service.connection.pulsar.PulsarContainerConnectionDetailsFactory,\

0 commit comments

Comments
 (0)