11package com .uid2 .operator .model ;
22
3+ import com .uid2 .operator .util .Tuple ;
34import com .uid2 .operator .vertx .UIDOperatorVerticle ;
45import com .uid2 .shared .Const ;
56import com .uid2 .shared .auth .Keyset ;
67import com .uid2 .shared .model .KeysetKey ;
78import com .uid2 .shared .store .IKeysetKeyStore ;
89import com .uid2 .shared .store .reader .RotatingKeysetProvider ;
10+ import io .micrometer .core .instrument .Counter ;
11+ import io .micrometer .core .instrument .Metrics ;
912import org .slf4j .Logger ;
1013import org .slf4j .LoggerFactory ;
1114
1720
1821public class KeyManager {
1922 private static final Logger LOGGER = LoggerFactory .getLogger (KeyManager .class );
23+ private static final String SITE_KEYSET_STATUS = "site_keyset_found" ;
24+ private static final String FALLBACK_KEYSET_STATUS = "fallback_keyset_found" ;
25+ private static final String KEYSET_NOT_FOUND_STATUS = "keyset_not_found" ;
26+
2027 private final IKeysetKeyStore keysetKeyStore ;
2128 private final RotatingKeysetProvider keysetProvider ;
2229
@@ -34,12 +41,45 @@ public KeyManagerSnapshot getKeyManagerSnapshot(int siteId) {
3441 this .getDefaultKeysetBySiteId (siteId ));
3542 }
3643
37- public KeysetKey getActiveKeyBySiteIdWithFallback (int siteId , int fallbackSiteId , Instant asOf ) {
44+ private void recordSiteKeysetStatusMetrics (int siteId , Boolean keysetFound , Boolean isFallback , Map <Tuple .Tuple2 <String , String >, Counter > siteKeysetStatusMetrics ) {
45+ String status ;
46+ if (!keysetFound ) {
47+ status = KEYSET_NOT_FOUND_STATUS ;
48+ } else if (isFallback ) {
49+ status = FALLBACK_KEYSET_STATUS ;
50+ } else {
51+ status = SITE_KEYSET_STATUS ;
52+ }
53+
54+ siteKeysetStatusMetrics .computeIfAbsent (
55+ new Tuple .Tuple2 <>(String .valueOf (siteId ), status ),
56+ tuple -> Counter
57+ .builder ("uid2_site_keyset_status" )
58+ .description ("counts site keyset status by site ID" )
59+ .tags (
60+ "site_id" , tuple .getItem1 (),
61+ "status" , tuple .getItem1 ()
62+ )
63+ .register (Metrics .globalRegistry )
64+ ).increment ();
65+ }
66+
67+ public KeysetKey getActiveKeyBySiteIdWithFallback (int siteId , int fallbackSiteId , Instant asOf , Map <Tuple .Tuple2 <String , String >, Counter > siteKeysetStatusMetrics ) {
68+ boolean isFallback = false ;
69+
3870 KeysetKey key = getActiveKeyBySiteId (siteId , asOf );
39- if ( key == null ) key = getActiveKeyBySiteId ( fallbackSiteId , asOf );
71+
4072 if (key == null ) {
73+ isFallback = true ;
74+ key = getActiveKeyBySiteId (fallbackSiteId , asOf );
75+ }
76+
77+ if (key == null ) {
78+ recordSiteKeysetStatusMetrics (siteId , false , null , siteKeysetStatusMetrics );
4179 throw new NoActiveKeyException (String .format ("Cannot get active key in default keyset with SITE ID %d or %d." , siteId , fallbackSiteId ));
4280 }
81+
82+ recordSiteKeysetStatusMetrics (siteId , true , isFallback , siteKeysetStatusMetrics );
4383 return key ;
4484 }
4585
0 commit comments