2121import io .micrometer .core .instrument .Tags ;
2222import io .quarkus .micrometer .runtime .HttpServerMetricsTagsContributor ;
2323import io .vertx .core .http .HttpServerRequest ;
24- import jakarta .annotation .Nullable ;
2524import jakarta .enterprise .context .ApplicationScoped ;
2625import jakarta .inject .Inject ;
2726import org .apache .polaris .core .context .RealmContext ;
3130public class RealmIdTagContributor implements HttpServerMetricsTagsContributor {
3231
3332 public static final String TAG_REALM = "realm_id" ;
33+ public static final String TAG_REALM_RESOLUTION_FAILURE = "realm_resolution_failure" ;
34+
35+ private static final Tags UNFINISHED_RESOLUTION_TAGS = Tags .of (TAG_REALM , "???" );
36+ private static final Tags FAILED_RESOLUTION_TAGS = Tags .of (TAG_REALM , "!!!" );
3437
3538 @ Inject RealmContextResolver realmContextResolver ;
3639
@@ -39,26 +42,31 @@ public Tags contribute(Context context) {
3942 // FIXME request scope does not work here, so we have to resolve the realm context manually
4043 HttpServerRequest request = context .request ();
4144 try {
42- RealmContext realmContext = resolveRealmContext (request );
43- return realmContext == null
44- ? Tags .empty ()
45- : Tags .of (TAG_REALM , realmContext .getRealmIdentifier ());
46- } catch (Exception ignored ) {
47- // ignore, the RealmContextFilter will handle the error
48- return Tags .empty ();
45+ return realmContextResolver
46+ .resolveRealmContext (
47+ request .absoluteURI (),
48+ request .method ().name (),
49+ request .path (),
50+ request .headers ()::get )
51+ .thenApply (this ::successfulResolutionTags )
52+ .exceptionally (this ::failedResolutionTags )
53+ .toCompletableFuture ()
54+ // get the result of the CompletableFuture if it's already completed,
55+ // otherwise return UNFINISHED_RESOLUTION_TAGS as this code is executed on
56+ // an event loop thread, and we don't want to block it.
57+ .getNow (UNFINISHED_RESOLUTION_TAGS );
58+ } catch (Exception e ) {
59+ return failedResolutionTags (e );
4960 }
5061 }
5162
52- @ Nullable
53- private RealmContext resolveRealmContext (HttpServerRequest request ) {
54- return realmContextResolver
55- .resolveRealmContext (
56- request .absoluteURI (), request .method ().name (), request .path (), request .headers ()::get )
57- .exceptionally (error -> null )
58- .toCompletableFuture ()
59- // get the result of the CompletableFuture if it's already completed,
60- // otherwise return null as this code is executed on an event loop thread,
61- // and we don't want to block it.
62- .getNow (null );
63+ private Tags successfulResolutionTags (RealmContext realmContext ) {
64+ return Tags .of (TAG_REALM , realmContext .getRealmIdentifier ());
65+ }
66+
67+ private Tags failedResolutionTags (Throwable error ) {
68+ return FAILED_RESOLUTION_TAGS .and (
69+ TAG_REALM_RESOLUTION_FAILURE ,
70+ error .getMessage () == null ? error .getClass ().getSimpleName () : error .getMessage ());
6371 }
6472}
0 commit comments