Skip to content

Add support for grafana/otel-lgtm and LgtmStackContainer from Service Connection #41551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spring-boot-project/spring-boot-dependencies/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2114,7 +2114,7 @@ bom {
releaseNotes("https://github.com/xerial/sqlite-jdbc/releases/tag/{version}")
}
}
library("Testcontainers", "1.19.8") {
library("Testcontainers", "1.20.0") {
group("org.testcontainers") {
imports = [
"testcontainers-bom"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2012-2024 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.docker.compose.service.connection.otlp;

import org.springframework.boot.actuate.autoconfigure.metrics.export.otlp.OtlpMetricsConnectionDetails;
import org.springframework.boot.docker.compose.service.connection.test.DockerComposeTest;
import org.springframework.boot.testsupport.container.TestImage;

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

/**
* Integration tests for
* {@link OpenTelemetryMetricsDockerComposeConnectionDetailsFactory}.
*
* @author Eddú Meléndez
*/
class GrafanaOtelMetricsDockerComposeConnectionDetailsFactoryIntegrationTests {

@DockerComposeTest(composeFile = "otlp-compose.yaml", image = TestImage.GRAFANA_OTEL_LGTM)
void runCreatesConnectionDetails(OtlpMetricsConnectionDetails connectionDetails) {
assertThat(connectionDetails.getUrl()).startsWith("http://").endsWith("/v1/metrics");
}

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

import org.springframework.boot.actuate.autoconfigure.tracing.otlp.OtlpTracingConnectionDetails;
import org.springframework.boot.docker.compose.service.connection.test.DockerComposeTest;
import org.springframework.boot.testsupport.container.TestImage;

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

/**
* Integration tests for
* {@link OpenTelemetryTracingDockerComposeConnectionDetailsFactory}.
*
* @author Eddú Meléndez
*/
class GrafanaOtelTracingDockerComposeConnectionDetailsFactoryIntegrationTests {

@DockerComposeTest(composeFile = "otlp-compose.yaml", image = TestImage.GRAFANA_OTEL_LGTM)
void runCreatesConnectionDetails(OtlpTracingConnectionDetails connectionDetails) {
assertThat(connectionDetails.getUrl()).startsWith("http://").endsWith("/v1/traces");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@
class OpenTelemetryMetricsDockerComposeConnectionDetailsFactory
extends DockerComposeConnectionDetailsFactory<OtlpMetricsConnectionDetails> {

private static final String[] OPENTELEMETRY_IMAGE_NAMES = { "otel/opentelemetry-collector-contrib",
"grafana/otel-lgtm" };

private static final int OTLP_PORT = 4318;

OpenTelemetryMetricsDockerComposeConnectionDetailsFactory() {
super("otel/opentelemetry-collector-contrib",
super(OPENTELEMETRY_IMAGE_NAMES,
"org.springframework.boot.actuate.autoconfigure.metrics.export.otlp.OtlpMetricsExportAutoConfiguration");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@
class OpenTelemetryTracingDockerComposeConnectionDetailsFactory
extends DockerComposeConnectionDetailsFactory<OtlpTracingConnectionDetails> {

private static final String[] OPENTELEMETRY_IMAGE_NAMES = { "otel/opentelemetry-collector-contrib",
"grafana/otel-lgtm" };

private static final int OTLP_PORT = 4318;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ The following service connections are currently supported:
| Containers named "neo4j" or "bitnami/neo4j"

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

| `OtlpTracingConnectionDetails`
| Containers named "otel/opentelemetry-collector-contrib"
| Containers named "otel/opentelemetry-collector-contrib", "grafana/otel-lgtm"

| `PulsarConnectionDetails`
| Containers named "apachepulsar/pulsar"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ The following service connection factories are provided in the `spring-boot-test
| Containers of type `Neo4jContainer`

| `OtlpMetricsConnectionDetails`
| Containers named "otel/opentelemetry-collector-contrib"
| Containers named "otel/opentelemetry-collector-contrib" or `LgtmStackContainer`

| `OtlpTracingConnectionDetails`
| Containers named "otel/opentelemetry-collector-contrib"
| Containers named "otel/opentelemetry-collector-contrib" or `LgtmStackContainer`

| `PulsarConnectionDetails`
| Containers of type `PulsarContainer`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ dependencies {
optional("org.testcontainers:cassandra")
optional("org.testcontainers:couchbase")
optional("org.testcontainers:elasticsearch")
optional("org.testcontainers:grafana")
optional("org.testcontainers:jdbc")
optional("org.testcontainers:kafka")
optional("org.testcontainers:mariadb")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Copyright 2012-2024 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 java.time.Duration;

import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.awaitility.Awaitility;
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.actuate.autoconfigure.metrics.export.otlp.OtlpMetricsExportAutoConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testsupport.container.TestImage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

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

/**
* Tests for {@link GrafanaOtelLgtmMetricsContainerConnectionDetailsFactory}.
*
* @author Eddú Meléndez
*/
@SpringJUnitConfig
@TestPropertySource(properties = { "management.otlp.metrics.export.resource-attributes.service.name=test",
"management.otlp.metrics.export.step=1s" })
@Testcontainers(disabledWithoutDocker = true)
class GrafanaOtelLgtmMetricsContainerConnectionDetailsFactoryIntegrationTests {

@Container
@ServiceConnection
static final LgtmStackContainer container = TestImage.container(LgtmStackContainer.class);

@Autowired
private MeterRegistry meterRegistry;

@Test
void connectionCanBeMadeToOpenTelemetryCollectorContainer() {
Counter.builder("test.counter").register(this.meterRegistry).increment(42);
Gauge.builder("test.gauge", () -> 12).register(this.meterRegistry);
Timer.builder("test.timer").register(this.meterRegistry).record(Duration.ofMillis(123));
DistributionSummary.builder("test.distributionsummary").register(this.meterRegistry).record(24);

Awaitility.given()
.pollInterval(Duration.ofSeconds(2))
.atMost(Duration.ofSeconds(10))
.ignoreExceptions()
.untilAsserted(() -> {
Response response = RestAssured.given()
.queryParam("query", "{job=\"test\"}")
.get("%s/api/v1/query".formatted(container.getPromehteusHttpUrl()))
.prettyPeek()
.thenReturn();
assertThat(response.getStatusCode()).isEqualTo(200);
assertThat(response.body()
.jsonPath()
.getList("data.result.find { it.metric.__name__ == 'test_counter_total' }.value")).contains("42");
assertThat(response.body()
.jsonPath()
.getList("data.result.find { it.metric.__name__ == 'test_gauge' }.value")).contains("12");
assertThat(response.body()
.jsonPath()
.getList("data.result.find { it.metric.__name__ == 'test_timer_milliseconds_count' }.value"))
.contains("1");
assertThat(response.body()
.jsonPath()
.getList("data.result.find { it.metric.__name__ == 'test_timer_milliseconds_sum' }.value"))
.contains("123");
assertThat(response.body()
.jsonPath()
.getList(
"data.result.find { it.metric.__name__ == 'test_timer_milliseconds_bucket' & it.metric.le == '+Inf' }.value"))
.contains("1");
assertThat(response.body()
.jsonPath()
.getList("data.result.find { it.metric.__name__ == 'test_distributionsummary_count' }.value"))
.contains("1");
assertThat(response.body()
.jsonPath()
.getList("data.result.find { it.metric.__name__ == 'test_distributionsummary_sum' }.value"))
.contains("24");
assertThat(response.body()
.jsonPath()
.getList(
"data.result.find { it.metric.__name__ == 'test_distributionsummary_bucket' & it.metric.le == '+Inf' }.value"))
.contains("1");
});
}

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

@Bean
Clock customClock() {
return Clock.SYSTEM;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2012-2024 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.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.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.boot.testsupport.container.TestImage;
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 GrafanaOtelLgtmTracingContainerConnectionDetailsFactoryIntegrationTests}.
*
* @author Eddú Meléndez
*/
@SpringJUnitConfig
@Testcontainers(disabledWithoutDocker = true)
class GrafanaOtelLgtmTracingContainerConnectionDetailsFactoryIntegrationTests {

@Container
@ServiceConnection
static final LgtmStackContainer container = TestImage.container(LgtmStackContainer.class);

@Autowired
private OtlpTracingConnectionDetails connectionDetails;

@Test
void connectionCanBeMadeToOpenTelemetryContainer() {
assertThat(this.connectionDetails.getUrl()).isEqualTo("%s/v1/traces".formatted(container.getOtlpHttpUrl()));
}

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

}

}
Loading
Loading