1
1
package org .apache .atlas .service .metrics ;
2
2
3
- import io .micrometer .core .instrument .Meter ;
4
3
import io .micrometer .core .instrument .Metrics ;
5
4
import io .micrometer .core .instrument .Tags ;
6
- import io .micrometer .core .instrument .binder .BaseUnits ;
7
- import io .micrometer .core .instrument .distribution .DistributionStatisticConfig ;
5
+ import io .micrometer .core .instrument .Timer ;
8
6
import io .micrometer .prometheus .PrometheusMeterRegistry ;
9
7
import org .apache .atlas .ApplicationProperties ;
10
8
import org .apache .atlas .AtlasException ;
16
14
import javax .inject .Inject ;
17
15
import java .io .IOException ;
18
16
import java .io .PrintWriter ;
19
- import java .time .Duration ;
17
+ import java .util .Arrays ;
18
+ import java .util .List ;
19
+ import java .util .concurrent .TimeUnit ;
20
+ import java .util .stream .Collectors ;
20
21
21
22
import static org .apache .atlas .service .metrics .MetricUtils .getMeterRegistry ;
22
23
@@ -29,20 +30,13 @@ public class MetricsRegistryServiceImpl implements MetricsRegistry {
29
30
private static final String NAME = "name" ;
30
31
private static final String METHOD_DIST_SUMMARY = "method_dist_summary" ;
31
32
private static final double [] PERCENTILES = {0.99 };
32
- private static final int SEC_MILLIS_SCALE = 1 ;
33
33
private static final String METHOD_LEVEL_METRICS_ENABLE = "atlas.metrics.method_level.enable" ;
34
-
35
- private final DistributionStatisticConfig distributionStatisticConfig ;
34
+ private static final String ATLAS_METRICS_METHOD_PATTERNS = "atlas.metrics.method_patterns" ;
35
+ private final List < String > filteredMethods ;
36
36
37
37
@ Inject
38
- public MetricsRegistryServiceImpl () {
39
- this .distributionStatisticConfig = DistributionStatisticConfig .builder ().percentilePrecision (2 )
40
- .percentiles (PERCENTILES )
41
- .bufferLength (3 )
42
- .percentilesHistogram (false )
43
- .minimumExpectedValue (1.0 )
44
- .maximumExpectedValue (Double .MAX_VALUE )
45
- .expiry (Duration .ofMinutes (2 )).build ();
38
+ public MetricsRegistryServiceImpl () throws AtlasException {
39
+ this .filteredMethods = Arrays .stream (ApplicationProperties .get ().getStringArray (ATLAS_METRICS_METHOD_PATTERNS )).collect (Collectors .toList ());
46
40
}
47
41
48
42
@ Override
@@ -51,17 +45,18 @@ public void collect(String requestId, AtlasPerfMetrics metrics) {
51
45
if (!ApplicationProperties .get ().getBoolean (METHOD_LEVEL_METRICS_ENABLE , false )) {
52
46
return ;
53
47
}
54
- } catch (AtlasException e ) {
55
- LOG .error ("Failed to read {} property from atlas config" , METHOD_LEVEL_METRICS_ENABLE , e );
48
+
49
+ for (String name : this .filteredMethods ) {
50
+ if (metrics .hasMetric (name )) {
51
+ AtlasPerfMetrics .Metric metric = metrics .getMetric (name );
52
+ Timer .builder (METHOD_DIST_SUMMARY ).tags (Tags .of (NAME , metric .getName ())).publishPercentiles (PERCENTILES )
53
+ .register (getMeterRegistry ()).record (metric .getTotalTimeMSecs (), TimeUnit .MILLISECONDS );
54
+ }
55
+ }
56
+ } catch (Exception e ) {
57
+ LOG .error ("Failed to collect metrics" , e );
56
58
return ;
57
59
}
58
- for (String name : metrics .getMetricsNames ()) {
59
- AtlasPerfMetrics .Metric metric = metrics .getMetric (name );
60
- getMeterRegistry ().newDistributionSummary (new Meter .Id (METHOD_DIST_SUMMARY ,
61
- Tags .of (NAME , metric .getName ()), BaseUnits .MILLISECONDS , METHOD_DIST_SUMMARY ,
62
- Meter .Type .TIMER ), distributionStatisticConfig , SEC_MILLIS_SCALE )
63
- .record (metric .getTotalTimeMSecs ());
64
- }
65
60
}
66
61
67
62
@ Override
@@ -72,7 +67,7 @@ public void scrape(PrintWriter writer) {
72
67
writer .flush ();
73
68
} catch (IOException e ) {
74
69
LOG .warn ("Failed to write metrics while scraping" , e );
75
- }finally {
70
+ } finally {
76
71
writer .close ();
77
72
}
78
73
});
0 commit comments