1+ package io .prometheus .client .it .servlet .jakarta ;
2+
3+ import okhttp3 .OkHttpClient ;
4+ import okhttp3 .Request ;
5+ import okhttp3 .Response ;
6+ import org .junit .Assert ;
7+ import org .junit .Rule ;
8+ import org .junit .Test ;
9+ import org .testcontainers .containers .GenericContainer ;
10+ import org .testcontainers .containers .wait .strategy .Wait ;
11+ import org .testcontainers .images .builder .ImageFromDockerfile ;
12+
13+ import java .io .IOException ;
14+ import java .nio .file .Paths ;
15+ import java .util .Arrays ;
16+ import java .util .List ;
17+
18+ public class ServletJakartaExporterWebXmlIT {
19+
20+ private final OkHttpClient client = new OkHttpClient ();
21+
22+ private static class DockerContainer extends GenericContainer <DockerContainer > {
23+ DockerContainer () {
24+ super (new ImageFromDockerfile ("servlet-jakarta-exporter-webxml" )
25+ .withFileFromPath ("servlet_jakarta_exporter_webxml.war" , Paths .get ("target/servlet_jakarta_exporter_webxml.war" ))
26+ .withFileFromClasspath ("Dockerfile" , "Dockerfile" ));
27+ }
28+ }
29+
30+ @ Rule
31+ public DockerContainer dockerContainer = new DockerContainer ()
32+ .withExposedPorts (8080 )
33+ .waitingFor (Wait .forLogMessage (".*oejs.Server:main: Started Server.*" , 1 ));
34+
35+ @ Test
36+ public void testSampleNameFilter () throws IOException , InterruptedException {
37+ callExampleServlet ();
38+ List <String > metrics = scrapeMetrics ();
39+ assertContains (metrics , "requests_bucket" );
40+ assertContains (metrics , "requests_count" );
41+ assertContains (metrics , "requests_sum" );
42+ assertContains (metrics , "requests_created" );
43+ assertContains (metrics , "requests_status_total" );
44+ assertContains (metrics , "requests_status_created" );
45+ assertContains (metrics , "jvm_gc_collection_seconds_count" );
46+ assertNotContains (metrics , "jvm_threads_deadlocked" );
47+ assertNotContains (metrics , "jvm_memory_pool" );
48+
49+ List <String > filteredMetrics = scrapeMetricsWithNameFilter ("requests_count" , "requests_sum" );
50+ assertNotContains (filteredMetrics , "requests_bucket" );
51+ assertContains (filteredMetrics , "requests_count" );
52+ assertContains (filteredMetrics , "requests_sum" );
53+ assertNotContains (filteredMetrics , "requests_created" );
54+ assertNotContains (filteredMetrics , "requests_status_total" );
55+ assertNotContains (filteredMetrics , "requests_status_created" );
56+ assertNotContains (filteredMetrics , "jvm_gc_collection_seconds_count" );
57+ assertNotContains (filteredMetrics , "jvm_threads_deadlocked" );
58+ assertNotContains (filteredMetrics , "jvm_memory_pool" );
59+ }
60+
61+ private void assertContains (List <String > metrics , String prefix ) {
62+ for (String metric : metrics ) {
63+ if (metric .startsWith (prefix )) {
64+ return ;
65+ }
66+ }
67+ Assert .fail ("metric not found: " + prefix );
68+ }
69+ private void assertNotContains (List <String > metrics , String prefix ) {
70+ for (String metric : metrics ) {
71+ if (metric .startsWith (prefix )) {
72+ Assert .fail ("unexpected metric found: " + metric );
73+ }
74+ }
75+ }
76+ private void callExampleServlet () throws IOException {
77+ Request request = new Request .Builder ()
78+ .url ("http://localhost:" + dockerContainer .getMappedPort (8080 ) + "/hello" )
79+ .build ();
80+ try (Response response = client .newCall (request ).execute ()) {
81+ Assert .assertEquals ("Hello, world!\n " , response .body ().string ());
82+ }
83+ }
84+
85+ private List <String > scrapeMetrics () throws IOException {
86+ Request request = new Request .Builder ()
87+ .url ("http://localhost:" + dockerContainer .getMappedPort (8080 ) + "/metrics" )
88+ .header ("Accept" , "application/openmetrics-text; version=1.0.0; charset=utf-8" )
89+ .build ();
90+ try (Response response = client .newCall (request ).execute ()) {
91+ return Arrays .asList (response .body ().string ().split ("\\ n" ));
92+ }
93+ }
94+
95+ private List <String > scrapeMetricsWithNameFilter (String ... names ) throws IOException {
96+ StringBuilder param = new StringBuilder ();
97+ boolean first = true ;
98+ for (String name : names ) {
99+ if (!first ) {
100+ param .append ("&" );
101+ }
102+ param .append ("name[]=" ).append (name );
103+ first = false ;
104+ }
105+ Request request = new Request .Builder ()
106+ .url ("http://localhost:" + dockerContainer .getMappedPort (8080 ) + "/metrics?" + param )
107+ .header ("Accept" , "application/openmetrics-text; version=1.0.0; charset=utf-8" )
108+ .build ();
109+ try (Response response = client .newCall (request ).execute ()) {
110+ return Arrays .asList (response .body ().string ().split ("\\ n" ));
111+ }
112+ }
113+ }
0 commit comments