1717package org .springframework .boot .micrometer .metrics .testcontainers .otlp ;
1818
1919import java .time .Duration ;
20+ import java .util .function .Consumer ;
2021
2122import io .micrometer .core .instrument .Clock ;
2223import io .micrometer .core .instrument .Counter ;
2324import io .micrometer .core .instrument .DistributionSummary ;
2425import io .micrometer .core .instrument .Gauge ;
2526import io .micrometer .core .instrument .MeterRegistry ;
2627import io .micrometer .core .instrument .Timer ;
27- import io .restassured .RestAssured ;
28- import io .restassured .response .Response ;
2928import org .awaitility .Awaitility ;
29+ import org .jspecify .annotations .Nullable ;
3030import org .junit .jupiter .api .Test ;
3131import org .testcontainers .containers .GenericContainer ;
3232import org .testcontainers .junit .jupiter .Container ;
4040import org .springframework .boot .testsupport .container .TestImage ;
4141import org .springframework .context .annotation .Bean ;
4242import org .springframework .context .annotation .Configuration ;
43+ import org .springframework .http .MediaType ;
4344import org .springframework .test .context .TestPropertySource ;
4445import org .springframework .test .context .junit .jupiter .SpringJUnitConfig ;
46+ import org .springframework .test .web .servlet .client .RestTestClient ;
47+ import org .springframework .test .web .servlet .client .RestTestClient .ResponseSpec ;
4548
46- import static org .hamcrest .Matchers .containsString ;
47- import static org .hamcrest .Matchers .endsWith ;
48- import static org .hamcrest .Matchers .matchesPattern ;
49+ import static org .assertj .core .api .Assertions .assertThat ;
4950
5051/**
5152 * Tests for {@link OpenTelemetryMetricsContainerConnectionDetailsFactory}.
5960@ Testcontainers (disabledWithoutDocker = true )
6061class OpenTelemetryMetricsContainerConnectionDetailsFactoryIntegrationTests {
6162
62- private static final String OPENMETRICS_001 = "application/openmetrics-text; version=0.0.1; charset=utf-8" ;
63+ private static final MediaType OPENMETRICS_001 = MediaType
64+ .parseMediaType ("application/openmetrics-text; version=0.0.1; charset=utf-8" );
6365
6466 private static final String CONFIG_FILE_NAME = "collector-config.yml" ;
6567
@@ -81,23 +83,33 @@ void connectionCanBeMadeToOpenTelemetryCollectorContainer() {
8183 DistributionSummary .builder ("test.distributionsummary" ).register (this .meterRegistry ).record (24 );
8284 Awaitility .await ()
8385 .atMost (Duration .ofSeconds (30 ))
84- .untilAsserted (() -> whenPrometheusScraped ().then ()
85- .statusCode (200 )
86+ .untilAsserted (() -> whenPrometheusScraped ().expectStatus ()
87+ .isOk ()
88+ .expectHeader ()
8689 .contentType (OPENMETRICS_001 )
87- .body (endsWith ("# EOF\n " ), containsString (
88- "{job=\" test\" ,service_name=\" test\" ,telemetry_sdk_language=\" java\" ,telemetry_sdk_name=\" io.micrometer\" " ),
89- matchesPattern ("(?s)^.*test_counter\\ {.+} 42\\ .0\\ n.*$" ),
90- matchesPattern ("(?s)^.*test_gauge\\ {.+} 12\\ .0\\ n.*$" ),
91- matchesPattern ("(?s)^.*test_timer_count\\ {.+} 1\\ n.*$" ),
92- matchesPattern ("(?s)^.*test_timer_sum\\ {.+} 123\\ .0\\ n.*$" ),
93- matchesPattern ("(?s)^.*test_timer_bucket\\ {.+,le=\" \\ +Inf\" } 1\\ n.*$" ),
94- matchesPattern ("(?s)^.*test_distributionsummary_count\\ {.+} 1\\ n.*$" ),
95- matchesPattern ("(?s)^.*test_distributionsummary_sum\\ {.+} 24\\ .0\\ n.*$" ),
96- matchesPattern ("(?s)^.*test_distributionsummary_bucket\\ {.+,le=\" \\ +Inf\" } 1\\ n.*$" )));
90+ .expectBody (String .class )
91+ .value ((Consumer <@ Nullable String >) (body ) -> assertThat (body ).endsWith ("# EOF\n " )
92+ .contains (
93+ "{job=\" test\" ,service_name=\" test\" ,telemetry_sdk_language=\" java\" ,telemetry_sdk_name=\" io.micrometer\" " )
94+ .matches ("(?s)^.*test_counter\\ {.+} 42\\ .0\\ n.*$" )
95+ .matches ("(?s)^.*test_gauge\\ {.+} 12\\ .0\\ n.*$" )
96+ .matches ("(?s)^.*test_timer_count\\ {.+} 1\\ n.*$" )
97+ .matches ("(?s)^.*test_timer_sum\\ {.+} 123\\ .0\\ n.*$" )
98+ .matches ("(?s)^.*test_timer_bucket\\ {.+,le=\" \\ +Inf\" } 1\\ n.*$" )
99+ .matches ("(?s)^.*test_distributionsummary_count\\ {.+} 1\\ n.*$" )
100+ .matches ("(?s)^.*test_distributionsummary_sum\\ {.+} 24\\ .0\\ n.*$" )
101+ .matches ("(?s)^.*test_distributionsummary_bucket\\ {.+,le=\" \\ +Inf\" } 1\\ n.*$" )));
102+
97103 }
98104
99- private Response whenPrometheusScraped () {
100- return RestAssured .given ().port (container .getMappedPort (9090 )).accept (OPENMETRICS_001 ).when ().get ("/metrics" );
105+ private ResponseSpec whenPrometheusScraped () {
106+ return RestTestClient .bindToServer ()
107+ .baseUrl ("http://" + container .getHost () + ":" + container .getMappedPort (9090 ))
108+ .build ()
109+ .get ()
110+ .uri ("/metrics" )
111+ .accept (OPENMETRICS_001 )
112+ .exchange ();
101113 }
102114
103115 @ Configuration (proxyBeanMethods = false )
0 commit comments