|
| 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 java.time.Duration; |
| 20 | + |
| 21 | +import io.micrometer.core.instrument.Clock; |
| 22 | +import io.micrometer.core.instrument.Counter; |
| 23 | +import io.micrometer.core.instrument.DistributionSummary; |
| 24 | +import io.micrometer.core.instrument.Gauge; |
| 25 | +import io.micrometer.core.instrument.MeterRegistry; |
| 26 | +import io.micrometer.core.instrument.Timer; |
| 27 | +import io.restassured.RestAssured; |
| 28 | +import io.restassured.response.Response; |
| 29 | +import org.awaitility.Awaitility; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | +import org.testcontainers.grafana.LgtmStackContainer; |
| 32 | +import org.testcontainers.junit.jupiter.Container; |
| 33 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 34 | + |
| 35 | +import org.springframework.beans.factory.annotation.Autowired; |
| 36 | +import org.springframework.boot.actuate.autoconfigure.metrics.export.otlp.OtlpMetricsExportAutoConfiguration; |
| 37 | +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; |
| 38 | +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; |
| 39 | +import org.springframework.boot.testsupport.container.TestImage; |
| 40 | +import org.springframework.context.annotation.Bean; |
| 41 | +import org.springframework.context.annotation.Configuration; |
| 42 | +import org.springframework.test.context.TestPropertySource; |
| 43 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 44 | + |
| 45 | +import static org.assertj.core.api.Assertions.assertThat; |
| 46 | + |
| 47 | +/** |
| 48 | + * Tests for {@link GrafanaOpenTelemetryMetricsContainerConnectionDetailsFactory}. |
| 49 | + * |
| 50 | + * @author Eddú Meléndez |
| 51 | + */ |
| 52 | +@SpringJUnitConfig |
| 53 | +@TestPropertySource(properties = { "management.otlp.metrics.export.resource-attributes.service.name=test", |
| 54 | + "management.otlp.metrics.export.step=1s" }) |
| 55 | +@Testcontainers(disabledWithoutDocker = true) |
| 56 | +class GrafanaOpenTelemetryMetricsContainerConnectionDetailsFactoryIntegrationTests { |
| 57 | + |
| 58 | + @Container |
| 59 | + @ServiceConnection |
| 60 | + static final LgtmStackContainer container = TestImage.container(LgtmStackContainer.class); |
| 61 | + |
| 62 | + @Autowired |
| 63 | + private MeterRegistry meterRegistry; |
| 64 | + |
| 65 | + @Test |
| 66 | + void connectionCanBeMadeToOpenTelemetryCollectorContainer() { |
| 67 | + Counter.builder("test.counter").register(this.meterRegistry).increment(42); |
| 68 | + Gauge.builder("test.gauge", () -> 12).register(this.meterRegistry); |
| 69 | + Timer.builder("test.timer").register(this.meterRegistry).record(Duration.ofMillis(123)); |
| 70 | + DistributionSummary.builder("test.distributionsummary").register(this.meterRegistry).record(24); |
| 71 | + |
| 72 | + Awaitility.given() |
| 73 | + .pollInterval(Duration.ofSeconds(2)) |
| 74 | + .atMost(Duration.ofSeconds(10)) |
| 75 | + .ignoreExceptions() |
| 76 | + .untilAsserted(() -> { |
| 77 | + Response response = RestAssured.given() |
| 78 | + .queryParam("query", "{job=\"test\"}") |
| 79 | + .get("%s/api/v1/query".formatted(container.getPromehteusHttpUrl())) |
| 80 | + .prettyPeek() |
| 81 | + .thenReturn(); |
| 82 | + assertThat(response.getStatusCode()).isEqualTo(200); |
| 83 | + assertThat(response.body() |
| 84 | + .jsonPath() |
| 85 | + .getList("data.result.find { it.metric.__name__ == 'test_counter_total' }.value")).contains("42"); |
| 86 | + assertThat(response.body() |
| 87 | + .jsonPath() |
| 88 | + .getList("data.result.find { it.metric.__name__ == 'test_gauge' }.value")).contains("12"); |
| 89 | + assertThat(response.body() |
| 90 | + .jsonPath() |
| 91 | + .getList("data.result.find { it.metric.__name__ == 'test_timer_milliseconds_count' }.value")) |
| 92 | + .contains("1"); |
| 93 | + assertThat(response.body() |
| 94 | + .jsonPath() |
| 95 | + .getList("data.result.find { it.metric.__name__ == 'test_timer_milliseconds_sum' }.value")) |
| 96 | + .contains("123"); |
| 97 | + assertThat(response.body() |
| 98 | + .jsonPath() |
| 99 | + .getList( |
| 100 | + "data.result.find { it.metric.__name__ == 'test_timer_milliseconds_bucket' & it.metric.le == '+Inf' }.value")) |
| 101 | + .contains("1"); |
| 102 | + assertThat(response.body() |
| 103 | + .jsonPath() |
| 104 | + .getList("data.result.find { it.metric.__name__ == 'test_distributionsummary_count' }.value")) |
| 105 | + .contains("1"); |
| 106 | + assertThat(response.body() |
| 107 | + .jsonPath() |
| 108 | + .getList("data.result.find { it.metric.__name__ == 'test_distributionsummary_sum' }.value")) |
| 109 | + .contains("24"); |
| 110 | + assertThat(response.body() |
| 111 | + .jsonPath() |
| 112 | + .getList( |
| 113 | + "data.result.find { it.metric.__name__ == 'test_distributionsummary_bucket' & it.metric.le == '+Inf' }.value")) |
| 114 | + .contains("1"); |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + @Configuration(proxyBeanMethods = false) |
| 119 | + @ImportAutoConfiguration(OtlpMetricsExportAutoConfiguration.class) |
| 120 | + static class TestConfiguration { |
| 121 | + |
| 122 | + @Bean |
| 123 | + Clock customClock() { |
| 124 | + return Clock.SYSTEM; |
| 125 | + } |
| 126 | + |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments