33import static java .util .Collections .emptyList ;
44import static java .util .concurrent .TimeUnit .MINUTES ;
55import static java .util .concurrent .TimeUnit .SECONDS ;
6+ import static java .util .stream .Collectors .toUnmodifiableList ;
7+ import static org .hypertrace .core .serviceframework .metrics .PlatformMetricsRegistry .registerResizeableGauge ;
68
79import io .micrometer .common .lang .Nullable ;
810import java .time .Duration ;
1113import java .util .concurrent .ScheduledExecutorService ;
1214import java .util .concurrent .atomic .AtomicLong ;
1315import lombok .NonNull ;
16+ import lombok .extern .slf4j .Slf4j ;
1417import org .hypertrace .core .documentstore .Datastore ;
1518import org .hypertrace .core .documentstore .metric .DocStoreMetric ;
1619import org .hypertrace .core .documentstore .metric .DocStoreMetricProvider ;
17- import org .hypertrace .core .documentstore . model . config . CustomMetricConfig ;
20+ import org .hypertrace .core .serviceframework . metrics . Measurement ;
1821import org .hypertrace .core .serviceframework .metrics .PlatformMetricsRegistry ;
22+ import org .hypertrace .core .serviceframework .metrics .ResizeableGauge ;
1923import org .hypertrace .core .serviceframework .spi .PlatformServiceLifecycle ;
2024
25+ @ Slf4j
2126@ SuppressWarnings ("unused" )
2227public class DocStoreMetricsRegistry {
2328 private static final long INITIAL_DELAY_SECONDS = MINUTES .toSeconds (5 );
@@ -93,11 +98,6 @@ public void monitor() {
9398 monitorCustomMetrics ();
9499 }
95100
96- /** Instantly query the datastore and report the custom metric once */
97- public void report (final CustomMetricConfig customMetricConfig ) {
98- metricProvider .getCustomMetrics (customMetricConfig ).forEach (this ::report );
99- }
100-
101101 /** Stop monitoring the database */
102102 public void shutdown () {
103103 if (executor != null ) {
@@ -112,17 +112,40 @@ private void addShutdownHook() {
112112 }
113113
114114 private void monitorCustomMetrics () {
115- customMetricConfigs .forEach (
116- reportingConfig ->
117- executor .scheduleAtFixedRate (
118- () -> report (reportingConfig .config ()),
119- INITIAL_DELAY_SECONDS ,
120- reportingConfig .reportingInterval ().toSeconds (),
121- SECONDS ));
115+ customMetricConfigs .forEach (this ::monitorCustomMetric );
122116 }
123117
124- private void report (final DocStoreMetric metric ) {
125- PlatformMetricsRegistry .registerGauge (metric .name (), metric .labels (), metric .value ());
118+ private void monitorCustomMetric (final DocStoreCustomMetricReportingConfig reportingConfig ) {
119+ final ResizeableGauge resizeableGauge =
120+ registerResizeableGauge (reportingConfig .config ().metricName ());
121+ executor .scheduleAtFixedRate (
122+ () -> report (reportingConfig , resizeableGauge ),
123+ INITIAL_DELAY_SECONDS ,
124+ reportingConfig .reportingInterval ().toSeconds (),
125+ SECONDS );
126+ }
127+
128+ private void report (
129+ final DocStoreCustomMetricReportingConfig reportingConfig ,
130+ final ResizeableGauge resizeableGauge ) {
131+ try {
132+ final List <DocStoreMetric > customMetrics =
133+ metricProvider .getCustomMetrics (reportingConfig .config ());
134+
135+ log .debug (
136+ "Reporting custom database metrics {} for configuration {}" ,
137+ customMetrics ,
138+ reportingConfig );
139+
140+ final List <Measurement > measurements =
141+ customMetrics .stream ()
142+ .map (metric -> new Measurement (metric .value (), metric .labels ()))
143+ .collect (toUnmodifiableList ());
144+
145+ resizeableGauge .report (measurements );
146+ } catch (final Exception e ) {
147+ log .warn ("Unable to report custom database metric for config: {}" , reportingConfig , e );
148+ }
126149 }
127150
128151 private class StandardDocStoreMetricsRegistry {
0 commit comments