6
6
import io .micrometer .prometheus .PrometheusConfig ;
7
7
import io .micrometer .prometheus .PrometheusMeterRegistry ;
8
8
import org .apache .atlas .ApplicationProperties ;
9
+ import org .apache .commons .lang .StringUtils ;
9
10
import org .slf4j .Logger ;
10
11
import org .slf4j .LoggerFactory ;
11
12
import org .springframework .stereotype .Component ;
12
13
13
- import java .util .Arrays ;
14
- import java .util .List ;
15
- import java . util . Objects ;
16
- import java . util . Optional ;
14
+ import java .util .* ;
15
+ import java .util .stream . Collectors ;
16
+
17
+ import static org . apache . commons . lang . StringUtils . EMPTY ;
17
18
18
19
@ Component
19
20
public class MetricUtils {
@@ -29,8 +30,9 @@ public class MetricUtils {
29
30
private static final String REGEX_URI_PLACEHOLDER = "\\ [\\ ^/\\ ]\\ +" ;
30
31
private static final String HTTP_SERVER_REQUESTS = "http.server.requests" ;
31
32
private static final String ATLAS_METRICS_URI_PATTERNS = "atlas.metrics.uri_patterns" ;
33
+ private static final double [] PERCENTILES = {0.5 , 0.90 , 0.99 };
32
34
33
- private static List <String > METRIC_URI_PATTERNS ;
35
+ private static Map <String , String > METRIC_URI_PATTERNS_MAP ;
34
36
private static final PrometheusMeterRegistry METER_REGISTRY ;
35
37
36
38
static {
@@ -41,7 +43,8 @@ public class MetricUtils {
41
43
42
44
public MetricUtils () {
43
45
try {
44
- METRIC_URI_PATTERNS = Arrays .asList (ApplicationProperties .get ().getStringArray (ATLAS_METRICS_URI_PATTERNS ));
46
+ METRIC_URI_PATTERNS_MAP = Arrays .stream (ApplicationProperties .get ().getStringArray (ATLAS_METRICS_URI_PATTERNS ))
47
+ .distinct ().collect (Collectors .toMap (uri ->uri , uri ->uri .replaceAll (REGEX_URI_PLACEHOLDER , "*" )));
45
48
} catch (Exception e ) {
46
49
LOG .error ("Failed to load 'atlas.metrics.uri_patterns from properties" );
47
50
}
@@ -64,7 +67,7 @@ private Timer getTimer(String timerName, String method, int code, String rawPath
64
67
tags = tags .and (additionalTags );
65
68
}
66
69
return Timer .builder (timerName )
67
- .publishPercentiles (0.5 , 0.90 , 0.99 )
70
+ .publishPercentiles (PERCENTILES )
68
71
.tags (tags )
69
72
.register (getMeterRegistry ());
70
73
}
@@ -83,10 +86,10 @@ private Optional<String> matchCanonicalPattern(String uri) {
83
86
uri = uri .substring (0 , uri .lastIndexOf ("/" ));
84
87
}
85
88
String updatedUrl = uri ;
86
- Optional <String > patternOp = METRIC_URI_PATTERNS .stream ()
89
+ Optional <String > patternOp = METRIC_URI_PATTERNS_MAP . keySet () .stream ()
87
90
.filter (pattern -> updatedUrl .matches (pattern + "$" ))
88
91
.findFirst ();
89
- return patternOp . map ( s -> s . replaceAll ( REGEX_URI_PLACEHOLDER , "*" ));
92
+ return Optional . ofNullable ( METRIC_URI_PATTERNS_MAP . get ( patternOp . orElse ( EMPTY ) ));
90
93
}
91
94
92
95
public static PrometheusMeterRegistry getMeterRegistry () {
0 commit comments