Skip to content

Commit 793e9a8

Browse files
eddumelendezmhalbritter
authored andcommitted
Add OpenTelemetry Logging Service Connection from LgtmStackContainer and Docker Compose
See gh-42174
1 parent 6cd6f75 commit 793e9a8

File tree

7 files changed

+180
-3
lines changed

7 files changed

+180
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.actuate.autoconfigure.opentelemetry.otlp.Transport;
21+
import org.springframework.boot.docker.compose.service.connection.test.DockerComposeTest;
22+
import org.springframework.boot.testsupport.container.TestImage;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
/**
27+
* Integration tests for {@link OpenTelemetryLoggingDockerComposeConnectionDetailsFactory}
28+
* using {@link TestImage#GRAFANA_OTEL_LGTM}.
29+
*
30+
* @author Eddú Meléndez
31+
*/
32+
class GrafanaOpenTelemetryLoggingDockerComposeConnectionDetailsFactoryIntegrationTests {
33+
34+
@DockerComposeTest(composeFile = "otlp-compose.yaml", image = TestImage.GRAFANA_OTEL_LGTM)
35+
void runCreatesConnectionDetails(OtlpLoggingConnectionDetails connectionDetails) {
36+
assertThat(connectionDetails.getUrl(Transport.HTTP)).startsWith("http://").endsWith("/v1/logs");
37+
assertThat(connectionDetails.getUrl(Transport.GRPC)).startsWith("http://").endsWith("/v1/logs");
38+
}
39+
40+
}

spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/service/connection/otlp/OpenTelemetryLoggingDockerComposeConnectionDetailsFactory.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@
3131
class OpenTelemetryLoggingDockerComposeConnectionDetailsFactory
3232
extends DockerComposeConnectionDetailsFactory<OtlpLoggingConnectionDetails> {
3333

34+
private static final String[] OPENTELEMETRY_IMAGE_NAMES = { "otel/opentelemetry-collector-contrib",
35+
"grafana/otel-lgtm" };
36+
3437
private static final int OTLP_GRPC_PORT = 4317;
3538

3639
private static final int OTLP_HTTP_PORT = 4318;
3740

3841
OpenTelemetryLoggingDockerComposeConnectionDetailsFactory() {
39-
super("otel/opentelemetry-collector-contrib",
42+
super(OPENTELEMETRY_IMAGE_NAMES,
4043
"org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp.OtlpLoggingAutoConfiguration");
4144
}
4245

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ The following service connections are currently supported:
108108
| Containers named "neo4j" or "bitnami/neo4j"
109109

110110
| `OtlpLoggingConnectionDetails`
111-
| Containers named "otel/opentelemetry-collector-contrib"
111+
| Containers named "otel/opentelemetry-collector-contrib", "grafana/otel-lgtm"
112112

113113
| `OtlpMetricsConnectionDetails`
114114
| Containers named "otel/opentelemetry-collector-contrib", "grafana/otel-lgtm"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ The following service connection factories are provided in the `spring-boot-test
7272
| Containers of type `Neo4jContainer`
7373

7474
| `OtlpLoggingConnectionDetails`
75-
| Containers named "otel/opentelemetry-collector-contrib"
75+
| Containers named "otel/opentelemetry-collector-contrib" or of type `LgtmStackContainer`
7676

7777
| `OtlpMetricsConnectionDetails`
7878
| Containers named "otel/opentelemetry-collector-contrib" or of type `LgtmStackContainer`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.grafana.LgtmStackContainer;
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.OtlpLoggingAutoConfiguration;
26+
import org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp.OtlpLoggingConnectionDetails;
27+
import org.springframework.boot.actuate.autoconfigure.opentelemetry.otlp.Transport;
28+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
29+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
30+
import org.springframework.boot.testsupport.container.TestImage;
31+
import org.springframework.context.annotation.Configuration;
32+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
36+
/**
37+
* Tests for {@link GrafanaOpenTelemetryLoggingContainerConnectionDetailsFactory}.
38+
*
39+
* @author Eddú Meléndez
40+
*/
41+
@SpringJUnitConfig
42+
@Testcontainers(disabledWithoutDocker = true)
43+
class GrafanaOpenTelemetryLoggingContainerConnectionDetailsFactoryIntegrationTests {
44+
45+
@Container
46+
@ServiceConnection
47+
static final LgtmStackContainer container = TestImage.container(LgtmStackContainer.class);
48+
49+
@Autowired
50+
private OtlpLoggingConnectionDetails connectionDetails;
51+
52+
@Test
53+
void connectionCanBeMadeToOpenTelemetryContainer() {
54+
assertThat(this.connectionDetails.getUrl(Transport.GRPC))
55+
.isEqualTo("%s/v1/logs".formatted(container.getOtlpGrpcUrl()));
56+
assertThat(this.connectionDetails.getUrl(Transport.HTTP))
57+
.isEqualTo("%s/v1/logs".formatted(container.getOtlpHttpUrl()));
58+
}
59+
60+
@Configuration(proxyBeanMethods = false)
61+
@ImportAutoConfiguration(OtlpLoggingAutoConfiguration.class)
62+
static class TestConfiguration {
63+
64+
}
65+
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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.grafana.LgtmStackContainer;
20+
21+
import org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp.OtlpLoggingConnectionDetails;
22+
import org.springframework.boot.actuate.autoconfigure.opentelemetry.otlp.Transport;
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 LgtmStackContainer} using
31+
* the {@code "grafana/otel-lgtm"} image.
32+
*
33+
* @author Eddú Meléndez
34+
*/
35+
class GrafanaOpenTelemetryLoggingContainerConnectionDetailsFactory
36+
extends ContainerConnectionDetailsFactory<LgtmStackContainer, OtlpLoggingConnectionDetails> {
37+
38+
GrafanaOpenTelemetryLoggingContainerConnectionDetailsFactory() {
39+
super(ANY_CONNECTION_NAME,
40+
"org.springframework.boot.actuate.autoconfigure.logging.opentelemetry.otlp.OtlpLoggingAutoConfiguration");
41+
}
42+
43+
@Override
44+
protected OtlpLoggingConnectionDetails getContainerConnectionDetails(
45+
ContainerConnectionSource<LgtmStackContainer> source) {
46+
return new OpenTelemetryLoggingContainerConnectionDetails(source);
47+
}
48+
49+
private static final class OpenTelemetryLoggingContainerConnectionDetails
50+
extends ContainerConnectionDetails<LgtmStackContainer> implements OtlpLoggingConnectionDetails {
51+
52+
private OpenTelemetryLoggingContainerConnectionDetails(ContainerConnectionSource<LgtmStackContainer> source) {
53+
super(source);
54+
}
55+
56+
@Override
57+
public String getUrl(Transport transport) {
58+
String url = switch (transport) {
59+
case HTTP -> getContainer().getOtlpHttpUrl();
60+
case GRPC -> getContainer().getOtlpGrpcUrl();
61+
};
62+
return "%s/v1/logs".formatted(url);
63+
}
64+
65+
}
66+
67+
}

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
@@ -23,6 +23,7 @@ org.springframework.boot.testcontainers.service.connection.ldap.OpenLdapContaine
2323
org.springframework.boot.testcontainers.service.connection.liquibase.LiquibaseContainerConnectionDetailsFactory,\
2424
org.springframework.boot.testcontainers.service.connection.mongo.MongoContainerConnectionDetailsFactory,\
2525
org.springframework.boot.testcontainers.service.connection.neo4j.Neo4jContainerConnectionDetailsFactory,\
26+
org.springframework.boot.testcontainers.service.connection.otlp.GrafanaOpenTelemetryLoggingContainerConnectionDetailsFactory,\
2627
org.springframework.boot.testcontainers.service.connection.otlp.GrafanaOpenTelemetryMetricsContainerConnectionDetailsFactory,\
2728
org.springframework.boot.testcontainers.service.connection.otlp.GrafanaOpenTelemetryTracingContainerConnectionDetailsFactory,\
2829
org.springframework.boot.testcontainers.service.connection.otlp.OpenTelemetryLoggingContainerConnectionDetailsFactory,\

0 commit comments

Comments
 (0)