1616 * specific language governing permissions and limitations
1717 * under the License.
1818 */
19- package org .apache .polaris .service .quarkus ;
19+ package org .apache .polaris .service .quarkus . metrics ;
2020
2121import static org .apache .polaris .service .context .TestRealmContextResolver .REALM_PROPERTY_KEY ;
2222import static org .assertj .core .api .Assertions .assertThat ;
2323import static org .assertj .core .api .InstanceOfAssertFactories .type ;
2424
2525import io .micrometer .core .instrument .MeterRegistry ;
26- import io .quarkus .test .junit .QuarkusTest ;
27- import io .quarkus .test .junit .QuarkusTestProfile ;
28- import io .quarkus .test .junit .TestProfile ;
2926import jakarta .inject .Inject ;
3027import jakarta .ws .rs .core .Response ;
3128import java .util .Map ;
32- import org .apache .polaris .service .quarkus .TimedApplicationEventListenerTest .Profile ;
3329import org .apache .polaris .service .quarkus .test .PolarisIntegrationTestFixture ;
3430import org .apache .polaris .service .quarkus .test .PolarisIntegrationTestHelper ;
3531import org .apache .polaris .service .quarkus .test .TestEnvironment ;
4339import org .junit .jupiter .api .TestInfo ;
4440import org .junit .jupiter .api .TestInstance ;
4541import org .junit .jupiter .api .extension .ExtendWith ;
46- import org .junit .jupiter .params .ParameterizedTest ;
47- import org .junit .jupiter .params .provider .ValueSource ;
4842
49- @ QuarkusTest
5043@ TestInstance (TestInstance .Lifecycle .PER_CLASS )
5144@ ExtendWith (TestEnvironmentExtension .class )
52- @ TestProfile (Profile .class )
53- public class TimedApplicationEventListenerTest {
54-
55- public static class Profile implements QuarkusTestProfile {
56-
57- @ Override
58- public Map <String , String > getConfigOverrides () {
59- return Map .of (
60- "polaris.metrics.tags.environment" , "prod" , "polaris.realm-context.type" , "test" );
61- }
62- }
45+ public abstract class MetricsTestBase {
6346
6447 private static final int ERROR_CODE = Response .Status .NOT_FOUND .getStatusCode ();
6548 private static final String ENDPOINT = "api/management/v1/principals" ;
66- private static final String METRIC_NAME = "polaris_principals_getPrincipal_seconds" ;
49+ private static final String API_METRIC_NAME = "polaris_principals_getPrincipal_seconds" ;
50+ private static final String HTTP_METRIC_NAME = "http_server_requests_seconds" ;
6751
6852 @ Inject PolarisIntegrationTestHelper helper ;
6953 @ Inject MeterRegistry registry ;
@@ -82,21 +66,19 @@ public void clearMetrics() {
8266 registry .clear ();
8367 }
8468
85- @ ParameterizedTest
86- @ ValueSource (strings = {"%s/metrics" , "%s/q/metrics" })
87- public void testMetricsEmittedOnSuccessfulRequest (String endpoint ) {
69+ protected void testMetricsEmittedOnSuccessfulRequest (String endpoint , boolean realmIdTagEnabled ) {
8870 sendSuccessfulRequest ();
8971 Map <String , MetricFamily > allMetrics =
9072 TestMetricsUtil .fetchMetrics (fixture .client , testEnv .baseManagementUri (), endpoint );
91- assertThat (allMetrics ).containsKey (METRIC_NAME );
92- assertThat (allMetrics .get (METRIC_NAME ).getMetrics ())
73+ assertThat (allMetrics ).containsKey (API_METRIC_NAME );
74+ assertThat (allMetrics .get (API_METRIC_NAME ).getMetrics ())
9375 .satisfiesOnlyOnce (
9476 metric -> {
9577 assertThat (metric .getLabels ())
9678 .contains (
9779 Map .entry ("application" , "Polaris" ),
9880 Map .entry ("environment" , "prod" ),
99- Map .entry ("realm_id" , fixture .realm ),
81+ Map .entry ("realm_id" , realmIdTagEnabled ? fixture .realm : "" ),
10082 Map .entry (
10183 "class" , "org.apache.polaris.service.admin.api.PolarisPrincipalsApi" ),
10284 Map .entry ("exception" , "none" ),
@@ -106,23 +88,43 @@ public void testMetricsEmittedOnSuccessfulRequest(String endpoint) {
10688 .extracting (Summary ::getSampleCount )
10789 .isEqualTo (1L );
10890 });
91+ assertThat (allMetrics ).containsKey (HTTP_METRIC_NAME );
92+ assertThat (allMetrics .get (HTTP_METRIC_NAME ).getMetrics ())
93+ .satisfiesOnlyOnce (
94+ metric -> {
95+ assertThat (metric .getLabels ())
96+ .contains (
97+ Map .entry ("application" , "Polaris" ),
98+ Map .entry ("environment" , "prod" ),
99+ Map .entry ("method" , "GET" ),
100+ Map .entry ("outcome" , "SUCCESS" ),
101+ Map .entry ("status" , "200" ),
102+ Map .entry ("uri" , "/api/management/v1/principals/{principalName}" ));
103+ if (realmIdTagEnabled ) {
104+ assertThat (metric .getLabels ()).containsEntry ("realm_id" , fixture .realm );
105+ } else {
106+ assertThat (metric .getLabels ()).doesNotContainKey ("realm_id" );
107+ }
108+ assertThat (metric )
109+ .asInstanceOf (type (Summary .class ))
110+ .extracting (Summary ::getSampleCount )
111+ .isEqualTo (1L );
112+ });
109113 }
110114
111- @ ParameterizedTest
112- @ ValueSource (strings = {"%s/metrics" , "%s/q/metrics" })
113- public void testMetricsEmittedOnFailedRequest (String endpoint ) {
115+ protected void testMetricsEmittedOnFailedRequest (String endpoint , boolean realmIdTagEnabled ) {
114116 sendFailingRequest ();
115117 Map <String , MetricFamily > allMetrics =
116118 TestMetricsUtil .fetchMetrics (fixture .client , testEnv .baseManagementUri (), endpoint );
117- assertThat (allMetrics ).containsKey (METRIC_NAME );
118- assertThat (allMetrics .get (METRIC_NAME ).getMetrics ())
119+ assertThat (allMetrics ).containsKey (API_METRIC_NAME );
120+ assertThat (allMetrics .get (API_METRIC_NAME ).getMetrics ())
119121 .satisfiesOnlyOnce (
120122 metric -> {
121123 assertThat (metric .getLabels ())
122124 .contains (
123125 Map .entry ("application" , "Polaris" ),
124126 Map .entry ("environment" , "prod" ),
125- Map .entry ("realm_id" , fixture .realm ),
127+ Map .entry ("realm_id" , realmIdTagEnabled ? fixture .realm : "" ),
126128 Map .entry (
127129 "class" , "org.apache.polaris.service.admin.api.PolarisPrincipalsApi" ),
128130 Map .entry ("exception" , "NotFoundException" ),
@@ -132,6 +134,27 @@ public void testMetricsEmittedOnFailedRequest(String endpoint) {
132134 .extracting (Summary ::getSampleCount )
133135 .isEqualTo (1L );
134136 });
137+ assertThat (allMetrics .get (HTTP_METRIC_NAME ).getMetrics ())
138+ .satisfiesOnlyOnce (
139+ metric -> {
140+ assertThat (metric .getLabels ())
141+ .contains (
142+ Map .entry ("application" , "Polaris" ),
143+ Map .entry ("environment" , "prod" ),
144+ Map .entry ("method" , "GET" ),
145+ Map .entry ("outcome" , "CLIENT_ERROR" ),
146+ Map .entry ("status" , "404" ),
147+ Map .entry ("uri" , "/api/management/v1/principals/{principalName}" ));
148+ if (realmIdTagEnabled ) {
149+ assertThat (metric .getLabels ()).containsEntry ("realm_id" , fixture .realm );
150+ } else {
151+ assertThat (metric .getLabels ()).doesNotContainKey ("realm_id" );
152+ }
153+ assertThat (metric )
154+ .asInstanceOf (type (Summary .class ))
155+ .extracting (Summary ::getSampleCount )
156+ .isEqualTo (1L );
157+ });
135158 }
136159
137160 private int sendRequest (String principalName ) {
0 commit comments