From 9d2f70a7060ca4cd41862bc8f105f81106199d76 Mon Sep 17 00:00:00 2001 From: Suresh Prakash Date: Wed, 9 Aug 2023 17:57:02 +0530 Subject: [PATCH 01/23] Added a monitor method for database monitoring through document store --- platform-metrics/build.gradle.kts | 1 + .../metrics/PlatformMetricsRegistry.java | 10 +++ .../metrics/binder/DocumentStoreMetrics.java | 73 +++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/binder/DocumentStoreMetrics.java diff --git a/platform-metrics/build.gradle.kts b/platform-metrics/build.gradle.kts index 04b9d78..6ff6c51 100644 --- a/platform-metrics/build.gradle.kts +++ b/platform-metrics/build.gradle.kts @@ -26,6 +26,7 @@ dependencies { implementation("io.prometheus:simpleclient_pushgateway:0.12.0") implementation("org.eclipse.jetty:jetty-servlet:9.4.51.v20230217") implementation("com.google.guava:guava:32.0.1-jre") + implementation("org.hypertrace.core.documentstore:document-store:0.7.37-SNAPSHOT") testImplementation("org.junit.jupiter:junit-jupiter:5.9.0") testImplementation("org.mockito:mockito-core:4.8.0") diff --git a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java index 272da72..d36ef47 100644 --- a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java +++ b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java @@ -49,6 +49,9 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; +import org.hypertrace.core.documentstore.Datastore; +import org.hypertrace.core.serviceframework.metrics.binder.DocumentStoreMetrics; +import org.hypertrace.core.serviceframework.metrics.binder.DocumentStoreMetrics.CustomMetricReportingConfig; import org.hypertrace.core.serviceframework.metrics.config.PrometheusPushRegistryConfig; import org.hypertrace.core.serviceframework.metrics.registry.PrometheusPushMeterRegistry; import org.slf4j.Logger; @@ -451,6 +454,13 @@ public static void registerCache( GuavaCacheMetrics.monitor(meterRegistry, guavaCache, cacheName, toIterable(tags)); } + public static void monitorDatabase( + final Datastore datastore, + final List metrics, + final int numThreadsInPool) { + new DocumentStoreMetrics(datastore, metrics, numThreadsInPool).bindTo(meterRegistry); + } + /** * Registers metrics for the given executor service with the service's metric registry and reports * them periodically to the configured reporters. Apart from the given tags, the reporting diff --git a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/binder/DocumentStoreMetrics.java b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/binder/DocumentStoreMetrics.java new file mode 100644 index 0000000..979192a --- /dev/null +++ b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/binder/DocumentStoreMetrics.java @@ -0,0 +1,73 @@ +package org.hypertrace.core.serviceframework.metrics.binder; + +import static java.util.concurrent.TimeUnit.MINUTES; +import static java.util.concurrent.TimeUnit.SECONDS; + +import io.micrometer.common.lang.NonNull; +import io.micrometer.core.instrument.Gauge; +import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.core.instrument.Tags; +import io.micrometer.core.instrument.binder.MeterBinder; +import java.time.Duration; +import java.util.List; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import org.hypertrace.core.documentstore.Datastore; +import org.hypertrace.core.documentstore.metric.DocStoreMetric; +import org.hypertrace.core.documentstore.metric.DocStoreMetricProvider; +import org.hypertrace.core.documentstore.model.config.CustomMetricConfig; + +public class DocumentStoreMetrics implements MeterBinder { + private final Datastore datastore; + private final List reportingConfigs; + private final ScheduledExecutorService executors; + private final Duration connectionCountReportingInterval = Duration.ofDays(1); + + public DocumentStoreMetrics( + final Datastore datastore, + final List reportingConfigs, + final int numThreadsInPool) { + this.datastore = datastore; + this.reportingConfigs = reportingConfigs; + this.executors = Executors.newScheduledThreadPool(numThreadsInPool); + } + + @Override + public void bindTo(@NonNull final MeterRegistry registry) { + final DocStoreMetricProvider source = datastore.getDocStoreMetricProvider(); + executors.scheduleAtFixedRate( + () -> report(source.getConnectionCountMetric(), registry), + MINUTES.toSeconds(5), + connectionCountReportingInterval.toSeconds(), + SECONDS); + reportingConfigs.forEach( + reportingConfig -> + executors.scheduleAtFixedRate( + () -> + source + .getCustomMetrics(reportingConfig.config) + .forEach(metric -> report(metric, registry)), + MINUTES.toSeconds(5), + reportingConfig.reportingInterval.toSeconds(), + SECONDS)); + } + + private void report(final DocStoreMetric metric, final MeterRegistry registry) { + final Tags tags = + metric.labels().entrySet().stream() + .map(entry -> Tags.of(entry.getKey(), entry.getValue())) + .reduce(Tags.empty(), Tags::and); + Gauge.builder(metric.name(), metric::value).tags(tags).register(registry); + } + + public static class CustomMetricReportingConfig { + private final CustomMetricConfig config; + private final Duration reportingInterval; + + public CustomMetricReportingConfig( + final CustomMetricConfig config, final Duration reportingInterval) { + this.config = config; + this.reportingInterval = reportingInterval; + } + } +} From 1b89401ce888449885c65674e0421ca8abddddd7 Mon Sep 17 00:00:00 2001 From: Suresh Prakash Date: Wed, 9 Aug 2023 18:16:00 +0530 Subject: [PATCH 02/23] Minor fixes --- .../metrics/PlatformMetricsRegistry.java | 2 +- .../metrics/binder/DocumentStoreMetrics.java | 30 ++++++++++++++----- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java index d36ef47..6612a26 100644 --- a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java +++ b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java @@ -454,7 +454,7 @@ public static void registerCache( GuavaCacheMetrics.monitor(meterRegistry, guavaCache, cacheName, toIterable(tags)); } - public static void monitorDatabase( + public static void monitorDatastore( final Datastore datastore, final List metrics, final int numThreadsInPool) { diff --git a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/binder/DocumentStoreMetrics.java b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/binder/DocumentStoreMetrics.java index 979192a..0740233 100644 --- a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/binder/DocumentStoreMetrics.java +++ b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/binder/DocumentStoreMetrics.java @@ -2,10 +2,13 @@ import static java.util.concurrent.TimeUnit.MINUTES; import static java.util.concurrent.TimeUnit.SECONDS; +import static java.util.stream.Collectors.collectingAndThen; +import static java.util.stream.Collectors.toUnmodifiableList; import io.micrometer.common.lang.NonNull; import io.micrometer.core.instrument.Gauge; import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.core.instrument.Tag; import io.micrometer.core.instrument.Tags; import io.micrometer.core.instrument.binder.MeterBinder; import java.time.Duration; @@ -22,6 +25,7 @@ public class DocumentStoreMetrics implements MeterBinder { private final List reportingConfigs; private final ScheduledExecutorService executors; private final Duration connectionCountReportingInterval = Duration.ofDays(1); + private final long initialDelay = MINUTES.toSeconds(5); public DocumentStoreMetrics( final Datastore datastore, @@ -35,11 +39,12 @@ public DocumentStoreMetrics( @Override public void bindTo(@NonNull final MeterRegistry registry) { final DocStoreMetricProvider source = datastore.getDocStoreMetricProvider(); - executors.scheduleAtFixedRate( - () -> report(source.getConnectionCountMetric(), registry), - MINUTES.toSeconds(5), - connectionCountReportingInterval.toSeconds(), - SECONDS); + reportCustomMetrics(source, registry); + reportConnectionCount(source, registry); + } + + private void reportCustomMetrics( + final DocStoreMetricProvider source, final MeterRegistry registry) { reportingConfigs.forEach( reportingConfig -> executors.scheduleAtFixedRate( @@ -47,16 +52,25 @@ public void bindTo(@NonNull final MeterRegistry registry) { source .getCustomMetrics(reportingConfig.config) .forEach(metric -> report(metric, registry)), - MINUTES.toSeconds(5), + initialDelay, reportingConfig.reportingInterval.toSeconds(), SECONDS)); } + private void reportConnectionCount( + final DocStoreMetricProvider source, final MeterRegistry registry) { + executors.scheduleAtFixedRate( + () -> report(source.getConnectionCountMetric(), registry), + MINUTES.toSeconds(5), + connectionCountReportingInterval.toSeconds(), + SECONDS); + } + private void report(final DocStoreMetric metric, final MeterRegistry registry) { final Tags tags = metric.labels().entrySet().stream() - .map(entry -> Tags.of(entry.getKey(), entry.getValue())) - .reduce(Tags.empty(), Tags::and); + .map(entry -> Tag.of(entry.getKey(), entry.getValue())) + .collect(collectingAndThen(toUnmodifiableList(), Tags::of)); Gauge.builder(metric.name(), metric::value).tags(tags).register(registry); } From 9b91ee92722a2f10ca614074a2e66c0ea7b709a5 Mon Sep 17 00:00:00 2001 From: Suresh Prakash Date: Wed, 9 Aug 2023 18:21:48 +0530 Subject: [PATCH 03/23] Minor fix --- .../metrics/binder/DocumentStoreMetrics.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/binder/DocumentStoreMetrics.java b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/binder/DocumentStoreMetrics.java index 0740233..75ad4a1 100644 --- a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/binder/DocumentStoreMetrics.java +++ b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/binder/DocumentStoreMetrics.java @@ -25,7 +25,7 @@ public class DocumentStoreMetrics implements MeterBinder { private final List reportingConfigs; private final ScheduledExecutorService executors; private final Duration connectionCountReportingInterval = Duration.ofDays(1); - private final long initialDelay = MINUTES.toSeconds(5); + private final long initialDelaySeconds = MINUTES.toSeconds(5); public DocumentStoreMetrics( final Datastore datastore, @@ -40,7 +40,7 @@ public DocumentStoreMetrics( public void bindTo(@NonNull final MeterRegistry registry) { final DocStoreMetricProvider source = datastore.getDocStoreMetricProvider(); reportCustomMetrics(source, registry); - reportConnectionCount(source, registry); + reportConnectionCountMetric(source, registry); } private void reportCustomMetrics( @@ -52,16 +52,16 @@ private void reportCustomMetrics( source .getCustomMetrics(reportingConfig.config) .forEach(metric -> report(metric, registry)), - initialDelay, + initialDelaySeconds, reportingConfig.reportingInterval.toSeconds(), SECONDS)); } - private void reportConnectionCount( + private void reportConnectionCountMetric( final DocStoreMetricProvider source, final MeterRegistry registry) { executors.scheduleAtFixedRate( () -> report(source.getConnectionCountMetric(), registry), - MINUTES.toSeconds(5), + initialDelaySeconds, connectionCountReportingInterval.toSeconds(), SECONDS); } From 7d477f27dc5fcb895d9b7812389e9604729951ca Mon Sep 17 00:00:00 2001 From: Suresh Prakash Date: Thu, 10 Aug 2023 10:24:01 +0530 Subject: [PATCH 04/23] Create a separate module for document store metrics --- docstore-metrics/build.gradle.kts | 14 +++ .../DocStoreCustomMetricReportingConfig.java | 15 ++++ .../metrics/DocStoreMetricsRegistry.java | 59 +++++++++++++ gradle/libs.versions.toml | 3 + platform-metrics/build.gradle.kts | 1 - .../metrics/PlatformMetricsRegistry.java | 10 --- .../metrics/binder/DocumentStoreMetrics.java | 87 ------------------- settings.gradle.kts | 1 + 8 files changed, 92 insertions(+), 98 deletions(-) create mode 100644 docstore-metrics/build.gradle.kts create mode 100644 docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreCustomMetricReportingConfig.java create mode 100644 docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java create mode 100644 gradle/libs.versions.toml delete mode 100644 platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/binder/DocumentStoreMetrics.java diff --git a/docstore-metrics/build.gradle.kts b/docstore-metrics/build.gradle.kts new file mode 100644 index 0000000..b47c44c --- /dev/null +++ b/docstore-metrics/build.gradle.kts @@ -0,0 +1,14 @@ +plugins { + `java-library` + jacoco + id("org.hypertrace.publish-plugin") + id("org.hypertrace.jacoco-report-plugin") +} + +dependencies { + annotationProcessor(libs.lombok) + compileOnly(libs.lombok) + + implementation(project(":platform-metrics")) + implementation(libs.hypertrace.documentStore) +} diff --git a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreCustomMetricReportingConfig.java b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreCustomMetricReportingConfig.java new file mode 100644 index 0000000..2eae53a --- /dev/null +++ b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreCustomMetricReportingConfig.java @@ -0,0 +1,15 @@ +package org.hypertrace.core.serviceframework.docstore.metrics; + +import java.time.Duration; +import lombok.Builder; +import lombok.Value; +import lombok.experimental.Accessors; +import org.hypertrace.core.documentstore.model.config.CustomMetricConfig; + +@Value +@Builder +@Accessors(fluent = true) +public class DocStoreCustomMetricReportingConfig { + CustomMetricConfig config; + Duration reportingInterval; +} diff --git a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java new file mode 100644 index 0000000..ed4d114 --- /dev/null +++ b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java @@ -0,0 +1,59 @@ +package org.hypertrace.core.serviceframework.docstore.metrics; + +import static java.util.concurrent.TimeUnit.MINUTES; +import static java.util.concurrent.TimeUnit.SECONDS; + +import java.time.Duration; +import java.util.List; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import org.hypertrace.core.documentstore.Datastore; +import org.hypertrace.core.documentstore.metric.DocStoreMetric; +import org.hypertrace.core.documentstore.metric.DocStoreMetricProvider; +import org.hypertrace.core.serviceframework.metrics.PlatformMetricsRegistry; + +@SuppressWarnings("unused") +public class DocStoreMetricsRegistry { + private static final Duration connectionCountReportingInterval = Duration.ofDays(1); + private static final long initialDelaySeconds = MINUTES.toSeconds(5); + + public static ScheduledExecutorService monitor( + final Datastore datastore, + final List reportingConfigs, + final int threadPoolSize) { + final DocStoreMetricProvider source = datastore.getDocStoreMetricProvider(); + final ScheduledExecutorService executor = Executors.newScheduledThreadPool(threadPoolSize); + monitorCustomMetrics(source, reportingConfigs, executor); + monitorConnectionCountMetric(source.getConnectionCountMetric(), executor); + return executor; + } + + private static void monitorCustomMetrics( + final DocStoreMetricProvider source, + final List reportingConfigs, + final ScheduledExecutorService executor) { + reportingConfigs.forEach( + reportingConfig -> + executor.scheduleAtFixedRate( + () -> + source + .getCustomMetrics(reportingConfig.config()) + .forEach(DocStoreMetricsRegistry::report), + initialDelaySeconds, + reportingConfig.reportingInterval().toSeconds(), + SECONDS)); + } + + private static void monitorConnectionCountMetric( + final DocStoreMetric connectionCountMetric, final ScheduledExecutorService executor) { + executor.scheduleAtFixedRate( + () -> report(connectionCountMetric), + initialDelaySeconds, + connectionCountReportingInterval.toSeconds(), + SECONDS); + } + + private static void report(final DocStoreMetric metric) { + PlatformMetricsRegistry.registerGauge(metric.name(), metric.labels(), metric.value()); + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml new file mode 100644 index 0000000..075ffd5 --- /dev/null +++ b/gradle/libs.versions.toml @@ -0,0 +1,3 @@ +[libraries] +lombok = { module = "org.projectlombok:lombok", version = "1.18.28" } +hypertrace-documentStore = { module = "org.hypertrace.core.documentstore:document-store", version = "0.7.37-SNAPSHOT" } diff --git a/platform-metrics/build.gradle.kts b/platform-metrics/build.gradle.kts index 6ff6c51..04b9d78 100644 --- a/platform-metrics/build.gradle.kts +++ b/platform-metrics/build.gradle.kts @@ -26,7 +26,6 @@ dependencies { implementation("io.prometheus:simpleclient_pushgateway:0.12.0") implementation("org.eclipse.jetty:jetty-servlet:9.4.51.v20230217") implementation("com.google.guava:guava:32.0.1-jre") - implementation("org.hypertrace.core.documentstore:document-store:0.7.37-SNAPSHOT") testImplementation("org.junit.jupiter:junit-jupiter:5.9.0") testImplementation("org.mockito:mockito-core:4.8.0") diff --git a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java index 6612a26..272da72 100644 --- a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java +++ b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java @@ -49,9 +49,6 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; -import org.hypertrace.core.documentstore.Datastore; -import org.hypertrace.core.serviceframework.metrics.binder.DocumentStoreMetrics; -import org.hypertrace.core.serviceframework.metrics.binder.DocumentStoreMetrics.CustomMetricReportingConfig; import org.hypertrace.core.serviceframework.metrics.config.PrometheusPushRegistryConfig; import org.hypertrace.core.serviceframework.metrics.registry.PrometheusPushMeterRegistry; import org.slf4j.Logger; @@ -454,13 +451,6 @@ public static void registerCache( GuavaCacheMetrics.monitor(meterRegistry, guavaCache, cacheName, toIterable(tags)); } - public static void monitorDatastore( - final Datastore datastore, - final List metrics, - final int numThreadsInPool) { - new DocumentStoreMetrics(datastore, metrics, numThreadsInPool).bindTo(meterRegistry); - } - /** * Registers metrics for the given executor service with the service's metric registry and reports * them periodically to the configured reporters. Apart from the given tags, the reporting diff --git a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/binder/DocumentStoreMetrics.java b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/binder/DocumentStoreMetrics.java deleted file mode 100644 index 75ad4a1..0000000 --- a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/binder/DocumentStoreMetrics.java +++ /dev/null @@ -1,87 +0,0 @@ -package org.hypertrace.core.serviceframework.metrics.binder; - -import static java.util.concurrent.TimeUnit.MINUTES; -import static java.util.concurrent.TimeUnit.SECONDS; -import static java.util.stream.Collectors.collectingAndThen; -import static java.util.stream.Collectors.toUnmodifiableList; - -import io.micrometer.common.lang.NonNull; -import io.micrometer.core.instrument.Gauge; -import io.micrometer.core.instrument.MeterRegistry; -import io.micrometer.core.instrument.Tag; -import io.micrometer.core.instrument.Tags; -import io.micrometer.core.instrument.binder.MeterBinder; -import java.time.Duration; -import java.util.List; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import org.hypertrace.core.documentstore.Datastore; -import org.hypertrace.core.documentstore.metric.DocStoreMetric; -import org.hypertrace.core.documentstore.metric.DocStoreMetricProvider; -import org.hypertrace.core.documentstore.model.config.CustomMetricConfig; - -public class DocumentStoreMetrics implements MeterBinder { - private final Datastore datastore; - private final List reportingConfigs; - private final ScheduledExecutorService executors; - private final Duration connectionCountReportingInterval = Duration.ofDays(1); - private final long initialDelaySeconds = MINUTES.toSeconds(5); - - public DocumentStoreMetrics( - final Datastore datastore, - final List reportingConfigs, - final int numThreadsInPool) { - this.datastore = datastore; - this.reportingConfigs = reportingConfigs; - this.executors = Executors.newScheduledThreadPool(numThreadsInPool); - } - - @Override - public void bindTo(@NonNull final MeterRegistry registry) { - final DocStoreMetricProvider source = datastore.getDocStoreMetricProvider(); - reportCustomMetrics(source, registry); - reportConnectionCountMetric(source, registry); - } - - private void reportCustomMetrics( - final DocStoreMetricProvider source, final MeterRegistry registry) { - reportingConfigs.forEach( - reportingConfig -> - executors.scheduleAtFixedRate( - () -> - source - .getCustomMetrics(reportingConfig.config) - .forEach(metric -> report(metric, registry)), - initialDelaySeconds, - reportingConfig.reportingInterval.toSeconds(), - SECONDS)); - } - - private void reportConnectionCountMetric( - final DocStoreMetricProvider source, final MeterRegistry registry) { - executors.scheduleAtFixedRate( - () -> report(source.getConnectionCountMetric(), registry), - initialDelaySeconds, - connectionCountReportingInterval.toSeconds(), - SECONDS); - } - - private void report(final DocStoreMetric metric, final MeterRegistry registry) { - final Tags tags = - metric.labels().entrySet().stream() - .map(entry -> Tag.of(entry.getKey(), entry.getValue())) - .collect(collectingAndThen(toUnmodifiableList(), Tags::of)); - Gauge.builder(metric.name(), metric::value).tags(tags).register(registry); - } - - public static class CustomMetricReportingConfig { - private final CustomMetricConfig config; - private final Duration reportingInterval; - - public CustomMetricReportingConfig( - final CustomMetricConfig config, final Duration reportingInterval) { - this.config = config; - this.reportingInterval = reportingInterval; - } - } -} diff --git a/settings.gradle.kts b/settings.gradle.kts index 8bd7475..d54c8f8 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -16,5 +16,6 @@ include(":platform-grpc-service-framework") include(":platform-http-service-framework") include(":platform-service-framework") include(":platform-metrics") +include(":docstore-metrics") include(":integrationtest-service-framework") include(":service-framework-spi") From a0160f6fdd7c4bf2bc98715e17ebab47936ab020 Mon Sep 17 00:00:00 2001 From: Suresh Prakash Date: Fri, 11 Aug 2023 13:18:20 +0530 Subject: [PATCH 05/23] Handled review comments --- docstore-metrics/build.gradle.kts | 2 +- .../metrics/DocStoreMetricsRegistry.java | 92 +++++++++++++++---- gradle/libs.versions.toml | 2 +- 3 files changed, 77 insertions(+), 19 deletions(-) diff --git a/docstore-metrics/build.gradle.kts b/docstore-metrics/build.gradle.kts index b47c44c..1c2e186 100644 --- a/docstore-metrics/build.gradle.kts +++ b/docstore-metrics/build.gradle.kts @@ -9,6 +9,6 @@ dependencies { annotationProcessor(libs.lombok) compileOnly(libs.lombok) + api(libs.hypertrace.documentStore) implementation(project(":platform-metrics")) - implementation(libs.hypertrace.documentStore) } diff --git a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java index ed4d114..931f74a 100644 --- a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java +++ b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java @@ -7,53 +7,111 @@ import java.util.List; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.atomic.AtomicLong; import org.hypertrace.core.documentstore.Datastore; import org.hypertrace.core.documentstore.metric.DocStoreMetric; import org.hypertrace.core.documentstore.metric.DocStoreMetricProvider; +import org.hypertrace.core.documentstore.model.config.CustomMetricConfig; import org.hypertrace.core.serviceframework.metrics.PlatformMetricsRegistry; @SuppressWarnings("unused") public class DocStoreMetricsRegistry { - private static final Duration connectionCountReportingInterval = Duration.ofDays(1); private static final long initialDelaySeconds = MINUTES.toSeconds(5); + /** + * To continuously monitor a database and periodically report (standard and custom) metrics. + * + *

The standard metrics (like the database connection count this service is holding) are + * reported immediately after this method is invoked and subsequently reported once in every 24 + * hours. + * + *

The custom metrics are reported at the interval scheduled per metric + * + * @param datastore The datastore to be monitored + * @param reportingConfigs The custom metric configurations to be reported, if any + * @param threadPoolSize The number of threads to be allocated for scheduling. Can just be 1 in + * most cases + * @return The created scheduled executor pool + */ public static ScheduledExecutorService monitor( final Datastore datastore, final List reportingConfigs, final int threadPoolSize) { final DocStoreMetricProvider source = datastore.getDocStoreMetricProvider(); final ScheduledExecutorService executor = Executors.newScheduledThreadPool(threadPoolSize); - monitorCustomMetrics(source, reportingConfigs, executor); - monitorConnectionCountMetric(source.getConnectionCountMetric(), executor); + + new StandardDocStoreMetricsRegistry(source, executor).monitor(); + monitorCustomMetrics(datastore, reportingConfigs, executor); + return executor; } + /** + * Instantly query the datastore and report the custom metric once + * + * @param datastore The datastore to be queried + * @param customMetricConfig The custom metric configuration to be reported + */ + public static void report( + final Datastore datastore, final CustomMetricConfig customMetricConfig) { + final DocStoreMetricProvider source = datastore.getDocStoreMetricProvider(); + source.getCustomMetrics(customMetricConfig).forEach(DocStoreMetricsRegistry::report); + } + private static void monitorCustomMetrics( - final DocStoreMetricProvider source, + final Datastore datastore, final List reportingConfigs, final ScheduledExecutorService executor) { reportingConfigs.forEach( reportingConfig -> executor.scheduleAtFixedRate( - () -> - source - .getCustomMetrics(reportingConfig.config()) - .forEach(DocStoreMetricsRegistry::report), + () -> report(datastore, reportingConfig.config()), initialDelaySeconds, reportingConfig.reportingInterval().toSeconds(), SECONDS)); } - private static void monitorConnectionCountMetric( - final DocStoreMetric connectionCountMetric, final ScheduledExecutorService executor) { - executor.scheduleAtFixedRate( - () -> report(connectionCountMetric), - initialDelaySeconds, - connectionCountReportingInterval.toSeconds(), - SECONDS); - } - private static void report(final DocStoreMetric metric) { PlatformMetricsRegistry.registerGauge(metric.name(), metric.labels(), metric.value()); } + + private static class StandardDocStoreMetricsRegistry { + private static final Duration dbMetricReportingInterval = Duration.ofDays(1); + + private final DocStoreMetricProvider source; + private final ScheduledExecutorService executor; + private final AtomicLong connectionCount; + + public StandardDocStoreMetricsRegistry( + final DocStoreMetricProvider source, final ScheduledExecutorService executor) { + this.source = source; + this.executor = executor; + + this.connectionCount = registerConnectionCountMetric(); + } + + private void monitor() { + executor.scheduleAtFixedRate( + this::queryDocStoreAndSetMetricValues, + initialDelaySeconds, + dbMetricReportingInterval.toSeconds(), + SECONDS); + } + + private AtomicLong registerConnectionCountMetric() { + final DocStoreMetric docStoreMetric = source.getConnectionCountMetric(); + return PlatformMetricsRegistry.registerGauge( + docStoreMetric.name(), + docStoreMetric.labels(), + new AtomicLong(castToLong(docStoreMetric.value()))); + } + + private void queryDocStoreAndSetMetricValues() { + connectionCount.set(castToLong(source.getConnectionCountMetric().value())); + } + + private long castToLong(final double value) { + return Double.valueOf(value).longValue(); + } + } } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 075ffd5..025bc69 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,3 +1,3 @@ [libraries] lombok = { module = "org.projectlombok:lombok", version = "1.18.28" } -hypertrace-documentStore = { module = "org.hypertrace.core.documentstore:document-store", version = "0.7.37-SNAPSHOT" } +hypertrace-documentStore = { module = "org.hypertrace.core.documentstore:document-store", version = "0.7.37" } From 70e1a113417f60588813f997b979115039c106dd Mon Sep 17 00:00:00 2001 From: Suresh Prakash Date: Fri, 11 Aug 2023 13:28:21 +0530 Subject: [PATCH 06/23] Minor fix --- .../docstore/metrics/DocStoreMetricsRegistry.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java index 931f74a..65fc6f6 100644 --- a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java +++ b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java @@ -25,7 +25,7 @@ public class DocStoreMetricsRegistry { * reported immediately after this method is invoked and subsequently reported once in every 24 * hours. * - *

The custom metrics are reported at the interval scheduled per metric + *

The custom metrics are reported at the interval scheduled per metric. * * @param datastore The datastore to be monitored * @param reportingConfigs The custom metric configurations to be reported, if any @@ -47,7 +47,7 @@ public static ScheduledExecutorService monitor( } /** - * Instantly query the datastore and report the custom metric once + * Instantly query the datastore and report the custom metric once. * * @param datastore The datastore to be queried * @param customMetricConfig The custom metric configuration to be reported From 95a3f158bc7275e00854cb86c04a48b04b3d7f66 Mon Sep 17 00:00:00 2001 From: Suresh Prakash Date: Sat, 12 Aug 2023 18:48:03 +0530 Subject: [PATCH 07/23] Handled review comments --- docstore-metrics/build.gradle.kts | 1 + .../metrics/DocStoreMetricsRegistry.java | 112 +++++++++++------- 2 files changed, 67 insertions(+), 46 deletions(-) diff --git a/docstore-metrics/build.gradle.kts b/docstore-metrics/build.gradle.kts index 1c2e186..e7e1f67 100644 --- a/docstore-metrics/build.gradle.kts +++ b/docstore-metrics/build.gradle.kts @@ -10,5 +10,6 @@ dependencies { compileOnly(libs.lombok) api(libs.hypertrace.documentStore) + api(project(":service-framework-spi")) implementation(project(":platform-metrics")) } diff --git a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java index 65fc6f6..13f7e62 100644 --- a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java +++ b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java @@ -1,8 +1,10 @@ package org.hypertrace.core.serviceframework.docstore.metrics; +import static java.util.Collections.emptyList; import static java.util.concurrent.TimeUnit.MINUTES; import static java.util.concurrent.TimeUnit.SECONDS; +import io.micrometer.common.lang.Nullable; import java.time.Duration; import java.util.List; import java.util.concurrent.Executors; @@ -13,93 +15,111 @@ import org.hypertrace.core.documentstore.metric.DocStoreMetricProvider; import org.hypertrace.core.documentstore.model.config.CustomMetricConfig; import org.hypertrace.core.serviceframework.metrics.PlatformMetricsRegistry; +import org.hypertrace.core.serviceframework.spi.PlatformServiceLifecycle; @SuppressWarnings("unused") public class DocStoreMetricsRegistry { - private static final long initialDelaySeconds = MINUTES.toSeconds(5); + private static final long INITIAL_DELAY_SECONDS = MINUTES.toSeconds(5); + + private final DocStoreMetricProvider metricProvider; + @Nullable private PlatformServiceLifecycle platformLifecycle; + private int threadPoolSize; + private List customMetricConfigs; + private Duration standardMetricReportingInterval; + + public DocStoreMetricsRegistry(final Datastore datastore) { + metricProvider = datastore.getDocStoreMetricProvider(); + platformLifecycle = null; + threadPoolSize = 1; + customMetricConfigs = emptyList(); + standardMetricReportingInterval = Duration.ofMinutes(30); + } + + public DocStoreMetricsRegistry withPlatformLifecycle( + final PlatformServiceLifecycle platformLifecycle) { + this.platformLifecycle = platformLifecycle; + return this; + } + + public DocStoreMetricsRegistry withCustomMetrics( + final List customMetricConfigs) { + this.customMetricConfigs = customMetricConfigs; + return this; + } + + public DocStoreMetricsRegistry withThreadPoolSize(final int threadPoolSize) { + this.threadPoolSize = threadPoolSize; + return this; + } + + public DocStoreMetricsRegistry withStandardMetricReportingInterval( + final Duration standardMetricReportingInterval) { + this.standardMetricReportingInterval = standardMetricReportingInterval; + return this; + } /** - * To continuously monitor a database and periodically report (standard and custom) metrics. + * Continuously monitor a database and periodically report (standard and custom) metrics. * *

The standard metrics (like the database connection count this service is holding) are * reported immediately after this method is invoked and subsequently reported once in every 24 * hours. * *

The custom metrics are reported at the interval scheduled per metric. - * - * @param datastore The datastore to be monitored - * @param reportingConfigs The custom metric configurations to be reported, if any - * @param threadPoolSize The number of threads to be allocated for scheduling. Can just be 1 in - * most cases - * @return The created scheduled executor pool */ - public static ScheduledExecutorService monitor( - final Datastore datastore, - final List reportingConfigs, - final int threadPoolSize) { - final DocStoreMetricProvider source = datastore.getDocStoreMetricProvider(); + public void monitor() { final ScheduledExecutorService executor = Executors.newScheduledThreadPool(threadPoolSize); - new StandardDocStoreMetricsRegistry(source, executor).monitor(); - monitorCustomMetrics(datastore, reportingConfigs, executor); + addShutdownHook(executor); - return executor; + new StandardDocStoreMetricsRegistry(executor).monitor(); + monitorCustomMetrics(executor); } - /** - * Instantly query the datastore and report the custom metric once. - * - * @param datastore The datastore to be queried - * @param customMetricConfig The custom metric configuration to be reported - */ - public static void report( - final Datastore datastore, final CustomMetricConfig customMetricConfig) { - final DocStoreMetricProvider source = datastore.getDocStoreMetricProvider(); - source.getCustomMetrics(customMetricConfig).forEach(DocStoreMetricsRegistry::report); + /** Instantly query the datastore and report the custom metric once */ + public void report(final CustomMetricConfig customMetricConfig) { + metricProvider.getCustomMetrics(customMetricConfig).forEach(this::report); } - private static void monitorCustomMetrics( - final Datastore datastore, - final List reportingConfigs, - final ScheduledExecutorService executor) { - reportingConfigs.forEach( + private void addShutdownHook(ScheduledExecutorService executor) { + if (platformLifecycle != null) { + platformLifecycle.shutdownComplete().thenRun(executor::shutdown); + } + } + + private void monitorCustomMetrics(final ScheduledExecutorService executor) { + customMetricConfigs.forEach( reportingConfig -> executor.scheduleAtFixedRate( - () -> report(datastore, reportingConfig.config()), - initialDelaySeconds, + () -> report(reportingConfig.config()), + INITIAL_DELAY_SECONDS, reportingConfig.reportingInterval().toSeconds(), SECONDS)); } - private static void report(final DocStoreMetric metric) { + private void report(final DocStoreMetric metric) { PlatformMetricsRegistry.registerGauge(metric.name(), metric.labels(), metric.value()); } - private static class StandardDocStoreMetricsRegistry { - private static final Duration dbMetricReportingInterval = Duration.ofDays(1); - - private final DocStoreMetricProvider source; + private class StandardDocStoreMetricsRegistry { private final ScheduledExecutorService executor; private final AtomicLong connectionCount; - public StandardDocStoreMetricsRegistry( - final DocStoreMetricProvider source, final ScheduledExecutorService executor) { - this.source = source; + public StandardDocStoreMetricsRegistry(final ScheduledExecutorService executor) { this.executor = executor; - this.connectionCount = registerConnectionCountMetric(); } private void monitor() { executor.scheduleAtFixedRate( this::queryDocStoreAndSetMetricValues, - initialDelaySeconds, - dbMetricReportingInterval.toSeconds(), + INITIAL_DELAY_SECONDS, + standardMetricReportingInterval.toSeconds(), SECONDS); } private AtomicLong registerConnectionCountMetric() { - final DocStoreMetric docStoreMetric = source.getConnectionCountMetric(); + final DocStoreMetric docStoreMetric = metricProvider.getConnectionCountMetric(); return PlatformMetricsRegistry.registerGauge( docStoreMetric.name(), docStoreMetric.labels(), @@ -107,7 +127,7 @@ private AtomicLong registerConnectionCountMetric() { } private void queryDocStoreAndSetMetricValues() { - connectionCount.set(castToLong(source.getConnectionCountMetric().value())); + connectionCount.set(castToLong(metricProvider.getConnectionCountMetric().value())); } private long castToLong(final double value) { From 9c90ab3c2d74cdce912cae46959931af34bf90e5 Mon Sep 17 00:00:00 2001 From: Suresh Prakash Date: Sat, 12 Aug 2023 18:50:48 +0530 Subject: [PATCH 08/23] Minor fix --- .../docstore/metrics/DocStoreMetricsRegistry.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java index 13f7e62..3f0bc6b 100644 --- a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java +++ b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java @@ -81,7 +81,7 @@ public void report(final CustomMetricConfig customMetricConfig) { metricProvider.getCustomMetrics(customMetricConfig).forEach(this::report); } - private void addShutdownHook(ScheduledExecutorService executor) { + private void addShutdownHook(final ScheduledExecutorService executor) { if (platformLifecycle != null) { platformLifecycle.shutdownComplete().thenRun(executor::shutdown); } From d9f622d769ebbe6005ddf27568da91d8d05ef6f7 Mon Sep 17 00:00:00 2001 From: Suresh Prakash Date: Sat, 12 Aug 2023 18:57:01 +0530 Subject: [PATCH 09/23] Comment fix --- .../docstore/metrics/DocStoreMetricsRegistry.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java index 3f0bc6b..d93c0a3 100644 --- a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java +++ b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java @@ -62,10 +62,11 @@ public DocStoreMetricsRegistry withStandardMetricReportingInterval( * Continuously monitor a database and periodically report (standard and custom) metrics. * *

The standard metrics (like the database connection count this service is holding) are - * reported immediately after this method is invoked and subsequently reported once in every 24 - * hours. + * reported immediately after this method is invoked and subsequently reported at the standard + * metrics reporting interval (configurable using {@link #withStandardMetricReportingInterval)}) * - *

The custom metrics are reported at the interval scheduled per metric. + *

The custom metrics, provided through {@link #withCustomMetrics)}, are reported immediately + * after this method is invoked and subsequently reported at the interval scheduled per metric. */ public void monitor() { final ScheduledExecutorService executor = Executors.newScheduledThreadPool(threadPoolSize); From 3a5040af86c23dc44ae3dda170eb9833ac701d1a Mon Sep 17 00:00:00 2001 From: Suresh Prakash Date: Sat, 12 Aug 2023 19:04:59 +0530 Subject: [PATCH 10/23] Added code comments --- .../metrics/DocStoreMetricsRegistry.java | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java index d93c0a3..5543c3a 100644 --- a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java +++ b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java @@ -25,36 +25,49 @@ public class DocStoreMetricsRegistry { @Nullable private PlatformServiceLifecycle platformLifecycle; private int threadPoolSize; private List customMetricConfigs; - private Duration standardMetricReportingInterval; + private Duration standardMetricsReportingInterval; public DocStoreMetricsRegistry(final Datastore datastore) { metricProvider = datastore.getDocStoreMetricProvider(); platformLifecycle = null; threadPoolSize = 1; customMetricConfigs = emptyList(); - standardMetricReportingInterval = Duration.ofMinutes(30); + standardMetricsReportingInterval = Duration.ofMinutes(30); } + /** + * Supply the platform lifecycle to stop monitoring the datastore when the service is shutting + * down + */ public DocStoreMetricsRegistry withPlatformLifecycle( final PlatformServiceLifecycle platformLifecycle) { this.platformLifecycle = platformLifecycle; return this; } + /** Define the custom metrics to be reported */ public DocStoreMetricsRegistry withCustomMetrics( final List customMetricConfigs) { this.customMetricConfigs = customMetricConfigs; return this; } + /** + * Override the number of threads dedicated for datastore metric reporting. The default value is + * 1. + */ public DocStoreMetricsRegistry withThreadPoolSize(final int threadPoolSize) { this.threadPoolSize = threadPoolSize; return this; } - public DocStoreMetricsRegistry withStandardMetricReportingInterval( - final Duration standardMetricReportingInterval) { - this.standardMetricReportingInterval = standardMetricReportingInterval; + /** + * Override the reporting interval of the standard datastore metrics. The default value is 30 + * minutes. + */ + public DocStoreMetricsRegistry withStandardMetricsReportingInterval( + final Duration standardMetricsReportingInterval) { + this.standardMetricsReportingInterval = standardMetricsReportingInterval; return this; } @@ -63,9 +76,9 @@ public DocStoreMetricsRegistry withStandardMetricReportingInterval( * *

The standard metrics (like the database connection count this service is holding) are * reported immediately after this method is invoked and subsequently reported at the standard - * metrics reporting interval (configurable using {@link #withStandardMetricReportingInterval)}) + * metrics reporting interval (configurable using {@link #withStandardMetricsReportingInterval}) * - *

The custom metrics, provided through {@link #withCustomMetrics)}, are reported immediately + *

The custom metrics, provided through {@link #withCustomMetrics}, are reported immediately * after this method is invoked and subsequently reported at the interval scheduled per metric. */ public void monitor() { @@ -115,7 +128,7 @@ private void monitor() { executor.scheduleAtFixedRate( this::queryDocStoreAndSetMetricValues, INITIAL_DELAY_SECONDS, - standardMetricReportingInterval.toSeconds(), + standardMetricsReportingInterval.toSeconds(), SECONDS); } From acc0fe28c91f06700aada512ab54bc7c49408ebb Mon Sep 17 00:00:00 2001 From: Suresh Prakash Date: Sat, 12 Aug 2023 19:08:39 +0530 Subject: [PATCH 11/23] Minor fixes --- .../docstore/metrics/DocStoreMetricsRegistry.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java index 5543c3a..0642be5 100644 --- a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java +++ b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java @@ -10,6 +10,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.atomic.AtomicLong; +import lombok.NonNull; import org.hypertrace.core.documentstore.Datastore; import org.hypertrace.core.documentstore.metric.DocStoreMetric; import org.hypertrace.core.documentstore.metric.DocStoreMetricProvider; @@ -27,7 +28,7 @@ public class DocStoreMetricsRegistry { private List customMetricConfigs; private Duration standardMetricsReportingInterval; - public DocStoreMetricsRegistry(final Datastore datastore) { + public DocStoreMetricsRegistry(@NonNull final Datastore datastore) { metricProvider = datastore.getDocStoreMetricProvider(); platformLifecycle = null; threadPoolSize = 1; @@ -47,7 +48,7 @@ public DocStoreMetricsRegistry withPlatformLifecycle( /** Define the custom metrics to be reported */ public DocStoreMetricsRegistry withCustomMetrics( - final List customMetricConfigs) { + @NonNull final List customMetricConfigs) { this.customMetricConfigs = customMetricConfigs; return this; } @@ -66,7 +67,7 @@ public DocStoreMetricsRegistry withThreadPoolSize(final int threadPoolSize) { * minutes. */ public DocStoreMetricsRegistry withStandardMetricsReportingInterval( - final Duration standardMetricsReportingInterval) { + @NonNull final Duration standardMetricsReportingInterval) { this.standardMetricsReportingInterval = standardMetricsReportingInterval; return this; } From e551b950667806a795c3da3c33c0a280b3d4b07d Mon Sep 17 00:00:00 2001 From: Suresh Prakash Date: Mon, 14 Aug 2023 08:47:40 +0530 Subject: [PATCH 12/23] Handled review comments --- .../metrics/DocStoreMetricsRegistry.java | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java index 0642be5..a68a3d2 100644 --- a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java +++ b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java @@ -27,6 +27,7 @@ public class DocStoreMetricsRegistry { private int threadPoolSize; private List customMetricConfigs; private Duration standardMetricsReportingInterval; + private ScheduledExecutorService executor; public DocStoreMetricsRegistry(@NonNull final Datastore datastore) { metricProvider = datastore.getDocStoreMetricProvider(); @@ -83,12 +84,13 @@ public DocStoreMetricsRegistry withStandardMetricsReportingInterval( * after this method is invoked and subsequently reported at the interval scheduled per metric. */ public void monitor() { - final ScheduledExecutorService executor = Executors.newScheduledThreadPool(threadPoolSize); + shutdown(); + executor = Executors.newScheduledThreadPool(threadPoolSize); - addShutdownHook(executor); + addShutdownHook(); - new StandardDocStoreMetricsRegistry(executor).monitor(); - monitorCustomMetrics(executor); + new StandardDocStoreMetricsRegistry().monitor(); + monitorCustomMetrics(); } /** Instantly query the datastore and report the custom metric once */ @@ -96,13 +98,20 @@ public void report(final CustomMetricConfig customMetricConfig) { metricProvider.getCustomMetrics(customMetricConfig).forEach(this::report); } - private void addShutdownHook(final ScheduledExecutorService executor) { + /** Stop monitoring the database */ + public void shutdown() { + if (executor != null) { + executor.shutdown(); + } + } + + private void addShutdownHook() { if (platformLifecycle != null) { - platformLifecycle.shutdownComplete().thenRun(executor::shutdown); + platformLifecycle.shutdownComplete().thenRun(this::shutdown); } } - private void monitorCustomMetrics(final ScheduledExecutorService executor) { + private void monitorCustomMetrics() { customMetricConfigs.forEach( reportingConfig -> executor.scheduleAtFixedRate( @@ -117,11 +126,9 @@ private void report(final DocStoreMetric metric) { } private class StandardDocStoreMetricsRegistry { - private final ScheduledExecutorService executor; private final AtomicLong connectionCount; - public StandardDocStoreMetricsRegistry(final ScheduledExecutorService executor) { - this.executor = executor; + public StandardDocStoreMetricsRegistry() { this.connectionCount = registerConnectionCountMetric(); } From a1e143464409bc47ccfe06a8be355f287d6058b1 Mon Sep 17 00:00:00 2001 From: Suresh Prakash Date: Tue, 23 Jan 2024 15:50:55 +0530 Subject: [PATCH 13/23] Avoid strong references to custom database metrics --- .../metrics/DocStoreMetricsRegistry.java | 26 +++++++++++++---- .../metrics/model/GaugeBuilderProvider.java | 29 +++++++++++++++++++ .../metrics/PlatformMetricsRegistry.java | 13 ++++++++- 3 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/model/GaugeBuilderProvider.java diff --git a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java index a68a3d2..93a16eb 100644 --- a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java +++ b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java @@ -11,13 +11,15 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.atomic.AtomicLong; import lombok.NonNull; +import lombok.extern.slf4j.Slf4j; import org.hypertrace.core.documentstore.Datastore; import org.hypertrace.core.documentstore.metric.DocStoreMetric; import org.hypertrace.core.documentstore.metric.DocStoreMetricProvider; -import org.hypertrace.core.documentstore.model.config.CustomMetricConfig; +import org.hypertrace.core.serviceframework.docstore.metrics.model.GaugeBuilderProvider; import org.hypertrace.core.serviceframework.metrics.PlatformMetricsRegistry; import org.hypertrace.core.serviceframework.spi.PlatformServiceLifecycle; +@Slf4j @SuppressWarnings("unused") public class DocStoreMetricsRegistry { private static final long INITIAL_DELAY_SECONDS = MINUTES.toSeconds(5); @@ -94,8 +96,10 @@ public void monitor() { } /** Instantly query the datastore and report the custom metric once */ - public void report(final CustomMetricConfig customMetricConfig) { - metricProvider.getCustomMetrics(customMetricConfig).forEach(this::report); + public void report(final DocStoreCustomMetricReportingConfig reportingConfig) { + metricProvider + .getCustomMetrics(reportingConfig.config()) + .forEach(metric -> report(reportingConfig, metric)); } /** Stop monitoring the database */ @@ -115,14 +119,24 @@ private void monitorCustomMetrics() { customMetricConfigs.forEach( reportingConfig -> executor.scheduleAtFixedRate( - () -> report(reportingConfig.config()), + () -> report(reportingConfig), INITIAL_DELAY_SECONDS, reportingConfig.reportingInterval().toSeconds(), SECONDS)); } - private void report(final DocStoreMetric metric) { - PlatformMetricsRegistry.registerGauge(metric.name(), metric.labels(), metric.value()); + private void report( + final DocStoreCustomMetricReportingConfig reportingConfig, final DocStoreMetric metric) { + final GaugeBuilderProvider builderProvider = + GaugeBuilderProvider.builder() + .name(metric.name()) + .tags(metric.labels()) + .number(metric.value()) + .duration(reportingConfig.reportingInterval()) + .executor(executor) + .build(); + log.debug("Reporting custom database metrics {} for configuration {}", metric, reportingConfig); + PlatformMetricsRegistry.registerGauge(builderProvider.get()); } private class StandardDocStoreMetricsRegistry { diff --git a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/model/GaugeBuilderProvider.java b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/model/GaugeBuilderProvider.java new file mode 100644 index 0000000..3eb468e --- /dev/null +++ b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/model/GaugeBuilderProvider.java @@ -0,0 +1,29 @@ +package org.hypertrace.core.serviceframework.docstore.metrics.model; + +import static org.hypertrace.core.serviceframework.metrics.PlatformMetricsRegistry.toIterable; + +import io.micrometer.core.instrument.Gauge; +import java.time.Duration; +import java.util.Map; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import lombok.Builder; + +@Builder +public class GaugeBuilderProvider { + private final String name; + private final Map tags; + private final T number; + private final Duration duration; + private final ScheduledExecutorService executor; + + public Gauge.Builder get() { + // Not creating a strong reference which indefinitely avoids the number from being GCed + holdNumberInMemoryForDuration(); + return Gauge.builder(name, number, Number::doubleValue).tags(toIterable(tags)); + } + + private void holdNumberInMemoryForDuration() { + executor.schedule(() -> number, duration.toSeconds(), TimeUnit.SECONDS); + } +} diff --git a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java index 272da72..aff271e 100644 --- a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java +++ b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java @@ -384,6 +384,17 @@ public static T registerGauge( return number; } + /** + * Registers the given Gauge Builder with the service's metric registry and reports it + * periodically to the configured reporters. Apart from the given tags, the reporting service's + * default tags also will be reported with the metrics. + * + *

See https://micrometer.io/docs/concepts#_gauges for more details on the Gauges. + */ + public static void registerGauge(final Gauge.Builder builder) { + builder.register(meterRegistry); + } + /** * Registers a DistributionSummary (with predefined percentiles computed locally) for the given * name with the service's metric registry and reports it periodically to the configured @@ -463,7 +474,7 @@ public static void monitorExecutorService( new ExecutorServiceMetrics(executorService, name, toIterable(tags)).bindTo(meterRegistry); } - private static Iterable toIterable(Map tags) { + public static Iterable toIterable(Map tags) { List newTags = new ArrayList<>(); if (tags != null) { From 22a507c9e0ee21a0c47016aaeee7487938b0e871 Mon Sep 17 00:00:00 2001 From: Suresh Prakash Date: Wed, 24 Jan 2024 15:49:26 +0530 Subject: [PATCH 14/23] Handled review comments by using a MultiGauge --- .../metrics/DocStoreMetricsRegistry.java | 54 ++++++++++--------- .../metrics/model/GaugeBuilderProvider.java | 29 ---------- .../metrics/PlatformMetricsRegistry.java | 5 ++ 3 files changed, 34 insertions(+), 54 deletions(-) delete mode 100644 docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/model/GaugeBuilderProvider.java diff --git a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java index 93a16eb..59faae3 100644 --- a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java +++ b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java @@ -3,8 +3,13 @@ import static java.util.Collections.emptyList; import static java.util.concurrent.TimeUnit.MINUTES; import static java.util.concurrent.TimeUnit.SECONDS; +import static java.util.stream.Collectors.toUnmodifiableList; +import static org.hypertrace.core.serviceframework.metrics.PlatformMetricsRegistry.toIterable; import io.micrometer.common.lang.Nullable; +import io.micrometer.core.instrument.MultiGauge; +import io.micrometer.core.instrument.MultiGauge.Row; +import io.micrometer.core.instrument.Tags; import java.time.Duration; import java.util.List; import java.util.concurrent.Executors; @@ -15,7 +20,6 @@ import org.hypertrace.core.documentstore.Datastore; import org.hypertrace.core.documentstore.metric.DocStoreMetric; import org.hypertrace.core.documentstore.metric.DocStoreMetricProvider; -import org.hypertrace.core.serviceframework.docstore.metrics.model.GaugeBuilderProvider; import org.hypertrace.core.serviceframework.metrics.PlatformMetricsRegistry; import org.hypertrace.core.serviceframework.spi.PlatformServiceLifecycle; @@ -95,13 +99,6 @@ public void monitor() { monitorCustomMetrics(); } - /** Instantly query the datastore and report the custom metric once */ - public void report(final DocStoreCustomMetricReportingConfig reportingConfig) { - metricProvider - .getCustomMetrics(reportingConfig.config()) - .forEach(metric -> report(reportingConfig, metric)); - } - /** Stop monitoring the database */ public void shutdown() { if (executor != null) { @@ -117,26 +114,33 @@ private void addShutdownHook() { private void monitorCustomMetrics() { customMetricConfigs.forEach( - reportingConfig -> - executor.scheduleAtFixedRate( - () -> report(reportingConfig), - INITIAL_DELAY_SECONDS, - reportingConfig.reportingInterval().toSeconds(), - SECONDS)); + reportingConfig -> { + final MultiGauge multiGauge = + PlatformMetricsRegistry.registerMultiGauge(reportingConfig.config().metricName()); + executor.scheduleAtFixedRate( + () -> report(reportingConfig, multiGauge), + INITIAL_DELAY_SECONDS, + reportingConfig.reportingInterval().toSeconds(), + SECONDS); + }); } private void report( - final DocStoreCustomMetricReportingConfig reportingConfig, final DocStoreMetric metric) { - final GaugeBuilderProvider builderProvider = - GaugeBuilderProvider.builder() - .name(metric.name()) - .tags(metric.labels()) - .number(metric.value()) - .duration(reportingConfig.reportingInterval()) - .executor(executor) - .build(); - log.debug("Reporting custom database metrics {} for configuration {}", metric, reportingConfig); - PlatformMetricsRegistry.registerGauge(builderProvider.get()); + final DocStoreCustomMetricReportingConfig reportingConfig, final MultiGauge multiGauge) { + final List customMetrics = + metricProvider.getCustomMetrics(reportingConfig.config()); + + log.debug( + "Reporting custom database metrics {} for configuration {}", + customMetrics, + reportingConfig); + + final List> rows = + customMetrics.stream() + .map(metric -> Row.of(Tags.of(toIterable(metric.labels())), metric::value)) + .collect(toUnmodifiableList()); + + multiGauge.register(rows); } private class StandardDocStoreMetricsRegistry { diff --git a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/model/GaugeBuilderProvider.java b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/model/GaugeBuilderProvider.java deleted file mode 100644 index 3eb468e..0000000 --- a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/model/GaugeBuilderProvider.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.hypertrace.core.serviceframework.docstore.metrics.model; - -import static org.hypertrace.core.serviceframework.metrics.PlatformMetricsRegistry.toIterable; - -import io.micrometer.core.instrument.Gauge; -import java.time.Duration; -import java.util.Map; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; -import lombok.Builder; - -@Builder -public class GaugeBuilderProvider { - private final String name; - private final Map tags; - private final T number; - private final Duration duration; - private final ScheduledExecutorService executor; - - public Gauge.Builder get() { - // Not creating a strong reference which indefinitely avoids the number from being GCed - holdNumberInMemoryForDuration(); - return Gauge.builder(name, number, Number::doubleValue).tags(toIterable(tags)); - } - - private void holdNumberInMemoryForDuration() { - executor.schedule(() -> number, duration.toSeconds(), TimeUnit.SECONDS); - } -} diff --git a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java index aff271e..8ec08e4 100644 --- a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java +++ b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java @@ -14,6 +14,7 @@ import io.micrometer.core.instrument.Gauge; import io.micrometer.core.instrument.ImmutableTag; import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.core.instrument.MultiGauge; import io.micrometer.core.instrument.Tag; import io.micrometer.core.instrument.Timer; import io.micrometer.core.instrument.binder.cache.GuavaCacheMetrics; @@ -384,6 +385,10 @@ public static T registerGauge( return number; } + public static MultiGauge registerMultiGauge(final String gaugeName) { + return MultiGauge.builder(gaugeName).register(meterRegistry); + } + /** * Registers the given Gauge Builder with the service's metric registry and reports it * periodically to the configured reporters. Apart from the given tags, the reporting service's From c33b629d6c667c4ae5d0a4cdfd0a58e911e043de Mon Sep 17 00:00:00 2001 From: Suresh Prakash Date: Wed, 24 Jan 2024 15:56:40 +0530 Subject: [PATCH 15/23] Removed unused method --- .../metrics/PlatformMetricsRegistry.java | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java index 8ec08e4..e8ce7e5 100644 --- a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java +++ b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java @@ -389,17 +389,6 @@ public static MultiGauge registerMultiGauge(final String gaugeName) { return MultiGauge.builder(gaugeName).register(meterRegistry); } - /** - * Registers the given Gauge Builder with the service's metric registry and reports it - * periodically to the configured reporters. Apart from the given tags, the reporting service's - * default tags also will be reported with the metrics. - * - *

See https://micrometer.io/docs/concepts#_gauges for more details on the Gauges. - */ - public static void registerGauge(final Gauge.Builder builder) { - builder.register(meterRegistry); - } - /** * Registers a DistributionSummary (with predefined percentiles computed locally) for the given * name with the service's metric registry and reports it periodically to the configured From 097f77acd54431ba92fc8db30031478a37793dcb Mon Sep 17 00:00:00 2001 From: Suresh Prakash Date: Wed, 24 Jan 2024 16:00:37 +0530 Subject: [PATCH 16/23] Revert unwanted change --- docstore-metrics/build.gradle.kts | 1 + 1 file changed, 1 insertion(+) diff --git a/docstore-metrics/build.gradle.kts b/docstore-metrics/build.gradle.kts index e7e1f67..9942cfe 100644 --- a/docstore-metrics/build.gradle.kts +++ b/docstore-metrics/build.gradle.kts @@ -11,5 +11,6 @@ dependencies { api(libs.hypertrace.documentStore) api(project(":service-framework-spi")) + api(platform("com.fasterxml.jackson:jackson-bom:2.16.0")) implementation(project(":platform-metrics")) } From 80cf04a71054903f577bcf0755dc79d863e63153 Mon Sep 17 00:00:00 2001 From: Suresh Prakash Date: Mon, 29 Jan 2024 07:49:59 +0530 Subject: [PATCH 17/23] Created an abstraction for MultiGauge --- .../metrics/DocStoreMetricsRegistry.java | 21 ++++---- .../metrics/PlatformMetricsRegistry.java | 48 +++++++++++++++++-- 2 files changed, 53 insertions(+), 16 deletions(-) diff --git a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java index 59faae3..9731a30 100644 --- a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java +++ b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java @@ -4,12 +4,8 @@ import static java.util.concurrent.TimeUnit.MINUTES; import static java.util.concurrent.TimeUnit.SECONDS; import static java.util.stream.Collectors.toUnmodifiableList; -import static org.hypertrace.core.serviceframework.metrics.PlatformMetricsRegistry.toIterable; import io.micrometer.common.lang.Nullable; -import io.micrometer.core.instrument.MultiGauge; -import io.micrometer.core.instrument.MultiGauge.Row; -import io.micrometer.core.instrument.Tags; import java.time.Duration; import java.util.List; import java.util.concurrent.Executors; @@ -21,6 +17,8 @@ import org.hypertrace.core.documentstore.metric.DocStoreMetric; import org.hypertrace.core.documentstore.metric.DocStoreMetricProvider; import org.hypertrace.core.serviceframework.metrics.PlatformMetricsRegistry; +import org.hypertrace.core.serviceframework.metrics.PlatformMetricsRegistry.Measurement; +import org.hypertrace.core.serviceframework.metrics.PlatformMetricsRegistry.ResizeableMetricRegistry; import org.hypertrace.core.serviceframework.spi.PlatformServiceLifecycle; @Slf4j @@ -115,10 +113,10 @@ private void addShutdownHook() { private void monitorCustomMetrics() { customMetricConfigs.forEach( reportingConfig -> { - final MultiGauge multiGauge = - PlatformMetricsRegistry.registerMultiGauge(reportingConfig.config().metricName()); + final ResizeableMetricRegistry resizeableMetricRegistry = + new ResizeableMetricRegistry(reportingConfig.config().metricName()); executor.scheduleAtFixedRate( - () -> report(reportingConfig, multiGauge), + () -> report(reportingConfig, resizeableMetricRegistry), INITIAL_DELAY_SECONDS, reportingConfig.reportingInterval().toSeconds(), SECONDS); @@ -126,7 +124,8 @@ private void monitorCustomMetrics() { } private void report( - final DocStoreCustomMetricReportingConfig reportingConfig, final MultiGauge multiGauge) { + final DocStoreCustomMetricReportingConfig reportingConfig, + final ResizeableMetricRegistry resizeableMetricRegistry) { final List customMetrics = metricProvider.getCustomMetrics(reportingConfig.config()); @@ -135,12 +134,12 @@ private void report( customMetrics, reportingConfig); - final List> rows = + final List measurements = customMetrics.stream() - .map(metric -> Row.of(Tags.of(toIterable(metric.labels())), metric::value)) + .map(metric -> new Measurement(metric.labels(), metric.value())) .collect(toUnmodifiableList()); - multiGauge.register(rows); + resizeableMetricRegistry.report(measurements); } private class StandardDocStoreMetricsRegistry { diff --git a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java index e8ce7e5..d61fc8a 100644 --- a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java +++ b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java @@ -1,5 +1,7 @@ package org.hypertrace.core.serviceframework.metrics; +import static java.util.stream.Collectors.toUnmodifiableList; + import com.codahale.metrics.ConsoleReporter; import com.codahale.metrics.Metric; import com.codahale.metrics.MetricRegistry; @@ -15,7 +17,9 @@ import io.micrometer.core.instrument.ImmutableTag; import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.MultiGauge; +import io.micrometer.core.instrument.MultiGauge.Row; import io.micrometer.core.instrument.Tag; +import io.micrometer.core.instrument.Tags; import io.micrometer.core.instrument.Timer; import io.micrometer.core.instrument.binder.cache.GuavaCacheMetrics; import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics; @@ -42,6 +46,7 @@ import io.prometheus.client.exporter.PushGateway; import java.time.Duration; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -385,10 +390,6 @@ public static T registerGauge( return number; } - public static MultiGauge registerMultiGauge(final String gaugeName) { - return MultiGauge.builder(gaugeName).register(meterRegistry); - } - /** * Registers a DistributionSummary (with predefined percentiles computed locally) for the given * name with the service's metric registry and reports it periodically to the configured @@ -468,7 +469,7 @@ public static void monitorExecutorService( new ExecutorServiceMetrics(executorService, name, toIterable(tags)).bindTo(meterRegistry); } - public static Iterable toIterable(Map tags) { + private static Iterable toIterable(Map tags) { List newTags = new ArrayList<>(); if (tags != null) { @@ -502,10 +503,47 @@ public static synchronized void stop() { isInit = false; } + public static class Measurement { + private final Map labels; + private final double value; + + public Measurement(final Map labels, final double value) { + this.labels = labels; + this.value = value; + } + + public Map labels() { + return labels; + } + + public double value() { + return value; + } + } + + public static class ResizeableMetricRegistry { + private final MultiGauge gauge; + + public ResizeableMetricRegistry(final String gaugeName) { + gauge = MultiGauge.builder(gaugeName).register(meterRegistry); + } + + public void report(final Collection measurements) { + final List> rows = + measurements.stream() + .map( + measurement -> + Row.of(Tags.of(toIterable(measurement.labels())), measurement::value)) + .collect(toUnmodifiableList()); + + gauge.register(rows); + } + } /* * This is needed because ConsoleMetricReporter.stop() doesn't call report for the last time * before closing the scheduled thread */ + private static void stopConsoleMetricsReporter() { if (consoleReporter == null) { return; From c6f6f4cfcfb3045a780b61760f4a559d5c8e117a Mon Sep 17 00:00:00 2001 From: Suresh Prakash <93120060+suresh-prakash@users.noreply.github.com> Date: Mon, 29 Jan 2024 07:52:01 +0530 Subject: [PATCH 18/23] Update platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java --- .../core/serviceframework/metrics/PlatformMetricsRegistry.java | 1 - 1 file changed, 1 deletion(-) diff --git a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java index d61fc8a..3b2b087 100644 --- a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java +++ b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java @@ -543,7 +543,6 @@ public void report(final Collection measurements) { * This is needed because ConsoleMetricReporter.stop() doesn't call report for the last time * before closing the scheduled thread */ - private static void stopConsoleMetricsReporter() { if (consoleReporter == null) { return; From aab12eac4f67ff859d8516de607e102713234d03 Mon Sep 17 00:00:00 2001 From: Suresh Prakash Date: Tue, 30 Jan 2024 12:17:41 +0530 Subject: [PATCH 19/23] Revert "Update platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java" This reverts commit c6f6f4cfcfb3045a780b61760f4a559d5c8e117a. --- .../core/serviceframework/metrics/PlatformMetricsRegistry.java | 1 + 1 file changed, 1 insertion(+) diff --git a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java index 3b2b087..d61fc8a 100644 --- a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java +++ b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java @@ -543,6 +543,7 @@ public void report(final Collection measurements) { * This is needed because ConsoleMetricReporter.stop() doesn't call report for the last time * before closing the scheduled thread */ + private static void stopConsoleMetricsReporter() { if (consoleReporter == null) { return; From 884ccfd9fee061ca41ff33cb425656a104f0f5a3 Mon Sep 17 00:00:00 2001 From: Suresh Prakash Date: Tue, 30 Jan 2024 12:17:53 +0530 Subject: [PATCH 20/23] Revert "Created an abstraction for MultiGauge" This reverts commit 80cf04a71054903f577bcf0755dc79d863e63153. --- .../metrics/DocStoreMetricsRegistry.java | 21 ++++---- .../metrics/PlatformMetricsRegistry.java | 48 ++----------------- 2 files changed, 16 insertions(+), 53 deletions(-) diff --git a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java index 9731a30..59faae3 100644 --- a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java +++ b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java @@ -4,8 +4,12 @@ import static java.util.concurrent.TimeUnit.MINUTES; import static java.util.concurrent.TimeUnit.SECONDS; import static java.util.stream.Collectors.toUnmodifiableList; +import static org.hypertrace.core.serviceframework.metrics.PlatformMetricsRegistry.toIterable; import io.micrometer.common.lang.Nullable; +import io.micrometer.core.instrument.MultiGauge; +import io.micrometer.core.instrument.MultiGauge.Row; +import io.micrometer.core.instrument.Tags; import java.time.Duration; import java.util.List; import java.util.concurrent.Executors; @@ -17,8 +21,6 @@ import org.hypertrace.core.documentstore.metric.DocStoreMetric; import org.hypertrace.core.documentstore.metric.DocStoreMetricProvider; import org.hypertrace.core.serviceframework.metrics.PlatformMetricsRegistry; -import org.hypertrace.core.serviceframework.metrics.PlatformMetricsRegistry.Measurement; -import org.hypertrace.core.serviceframework.metrics.PlatformMetricsRegistry.ResizeableMetricRegistry; import org.hypertrace.core.serviceframework.spi.PlatformServiceLifecycle; @Slf4j @@ -113,10 +115,10 @@ private void addShutdownHook() { private void monitorCustomMetrics() { customMetricConfigs.forEach( reportingConfig -> { - final ResizeableMetricRegistry resizeableMetricRegistry = - new ResizeableMetricRegistry(reportingConfig.config().metricName()); + final MultiGauge multiGauge = + PlatformMetricsRegistry.registerMultiGauge(reportingConfig.config().metricName()); executor.scheduleAtFixedRate( - () -> report(reportingConfig, resizeableMetricRegistry), + () -> report(reportingConfig, multiGauge), INITIAL_DELAY_SECONDS, reportingConfig.reportingInterval().toSeconds(), SECONDS); @@ -124,8 +126,7 @@ private void monitorCustomMetrics() { } private void report( - final DocStoreCustomMetricReportingConfig reportingConfig, - final ResizeableMetricRegistry resizeableMetricRegistry) { + final DocStoreCustomMetricReportingConfig reportingConfig, final MultiGauge multiGauge) { final List customMetrics = metricProvider.getCustomMetrics(reportingConfig.config()); @@ -134,12 +135,12 @@ private void report( customMetrics, reportingConfig); - final List measurements = + final List> rows = customMetrics.stream() - .map(metric -> new Measurement(metric.labels(), metric.value())) + .map(metric -> Row.of(Tags.of(toIterable(metric.labels())), metric::value)) .collect(toUnmodifiableList()); - resizeableMetricRegistry.report(measurements); + multiGauge.register(rows); } private class StandardDocStoreMetricsRegistry { diff --git a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java index d61fc8a..e8ce7e5 100644 --- a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java +++ b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java @@ -1,7 +1,5 @@ package org.hypertrace.core.serviceframework.metrics; -import static java.util.stream.Collectors.toUnmodifiableList; - import com.codahale.metrics.ConsoleReporter; import com.codahale.metrics.Metric; import com.codahale.metrics.MetricRegistry; @@ -17,9 +15,7 @@ import io.micrometer.core.instrument.ImmutableTag; import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.MultiGauge; -import io.micrometer.core.instrument.MultiGauge.Row; import io.micrometer.core.instrument.Tag; -import io.micrometer.core.instrument.Tags; import io.micrometer.core.instrument.Timer; import io.micrometer.core.instrument.binder.cache.GuavaCacheMetrics; import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics; @@ -46,7 +42,6 @@ import io.prometheus.client.exporter.PushGateway; import java.time.Duration; import java.util.ArrayList; -import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -390,6 +385,10 @@ public static T registerGauge( return number; } + public static MultiGauge registerMultiGauge(final String gaugeName) { + return MultiGauge.builder(gaugeName).register(meterRegistry); + } + /** * Registers a DistributionSummary (with predefined percentiles computed locally) for the given * name with the service's metric registry and reports it periodically to the configured @@ -469,7 +468,7 @@ public static void monitorExecutorService( new ExecutorServiceMetrics(executorService, name, toIterable(tags)).bindTo(meterRegistry); } - private static Iterable toIterable(Map tags) { + public static Iterable toIterable(Map tags) { List newTags = new ArrayList<>(); if (tags != null) { @@ -503,47 +502,10 @@ public static synchronized void stop() { isInit = false; } - public static class Measurement { - private final Map labels; - private final double value; - - public Measurement(final Map labels, final double value) { - this.labels = labels; - this.value = value; - } - - public Map labels() { - return labels; - } - - public double value() { - return value; - } - } - - public static class ResizeableMetricRegistry { - private final MultiGauge gauge; - - public ResizeableMetricRegistry(final String gaugeName) { - gauge = MultiGauge.builder(gaugeName).register(meterRegistry); - } - - public void report(final Collection measurements) { - final List> rows = - measurements.stream() - .map( - measurement -> - Row.of(Tags.of(toIterable(measurement.labels())), measurement::value)) - .collect(toUnmodifiableList()); - - gauge.register(rows); - } - } /* * This is needed because ConsoleMetricReporter.stop() doesn't call report for the last time * before closing the scheduled thread */ - private static void stopConsoleMetricsReporter() { if (consoleReporter == null) { return; From dac65007a48d7e4b6004c498404b38cdfef619d9 Mon Sep 17 00:00:00 2001 From: Suresh Prakash Date: Tue, 30 Jan 2024 12:43:39 +0530 Subject: [PATCH 21/23] Handled review comments --- .../metrics/DocStoreMetricsRegistry.java | 36 +++++++++---------- .../serviceframework/metrics/Measurement.java | 21 +++++++++++ .../metrics/PlatformMetricsRegistry.java | 28 +++++++-------- .../metrics/ResizeableGauge.java | 30 ++++++++++++++++ 4 files changed, 83 insertions(+), 32 deletions(-) create mode 100644 platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/Measurement.java create mode 100644 platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/ResizeableGauge.java diff --git a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java index 59faae3..2a065c1 100644 --- a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java +++ b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java @@ -4,12 +4,8 @@ import static java.util.concurrent.TimeUnit.MINUTES; import static java.util.concurrent.TimeUnit.SECONDS; import static java.util.stream.Collectors.toUnmodifiableList; -import static org.hypertrace.core.serviceframework.metrics.PlatformMetricsRegistry.toIterable; import io.micrometer.common.lang.Nullable; -import io.micrometer.core.instrument.MultiGauge; -import io.micrometer.core.instrument.MultiGauge.Row; -import io.micrometer.core.instrument.Tags; import java.time.Duration; import java.util.List; import java.util.concurrent.Executors; @@ -20,7 +16,9 @@ import org.hypertrace.core.documentstore.Datastore; import org.hypertrace.core.documentstore.metric.DocStoreMetric; import org.hypertrace.core.documentstore.metric.DocStoreMetricProvider; +import org.hypertrace.core.serviceframework.metrics.Measurement; import org.hypertrace.core.serviceframework.metrics.PlatformMetricsRegistry; +import org.hypertrace.core.serviceframework.metrics.ResizeableGauge; import org.hypertrace.core.serviceframework.spi.PlatformServiceLifecycle; @Slf4j @@ -113,20 +111,22 @@ private void addShutdownHook() { } private void monitorCustomMetrics() { - customMetricConfigs.forEach( - reportingConfig -> { - final MultiGauge multiGauge = - PlatformMetricsRegistry.registerMultiGauge(reportingConfig.config().metricName()); - executor.scheduleAtFixedRate( - () -> report(reportingConfig, multiGauge), - INITIAL_DELAY_SECONDS, - reportingConfig.reportingInterval().toSeconds(), - SECONDS); - }); + customMetricConfigs.forEach(this::monitorCustomMetric); + } + + private void monitorCustomMetric(final DocStoreCustomMetricReportingConfig reportingConfig) { + final ResizeableGauge resizeableGauge = + new ResizeableGauge(reportingConfig.config().metricName()); + executor.scheduleAtFixedRate( + () -> report(reportingConfig, resizeableGauge), + INITIAL_DELAY_SECONDS, + reportingConfig.reportingInterval().toSeconds(), + SECONDS); } private void report( - final DocStoreCustomMetricReportingConfig reportingConfig, final MultiGauge multiGauge) { + final DocStoreCustomMetricReportingConfig reportingConfig, + final ResizeableGauge resizeableGauge) { final List customMetrics = metricProvider.getCustomMetrics(reportingConfig.config()); @@ -135,12 +135,12 @@ private void report( customMetrics, reportingConfig); - final List> rows = + final List measurements = customMetrics.stream() - .map(metric -> Row.of(Tags.of(toIterable(metric.labels())), metric::value)) + .map(metric -> new Measurement(metric.value(), metric.labels())) .collect(toUnmodifiableList()); - multiGauge.register(rows); + resizeableGauge.report(measurements); } private class StandardDocStoreMetricsRegistry { diff --git a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/Measurement.java b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/Measurement.java new file mode 100644 index 0000000..8b1d92b --- /dev/null +++ b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/Measurement.java @@ -0,0 +1,21 @@ +package org.hypertrace.core.serviceframework.metrics; + +import java.util.Map; + +public class Measurement { + private final double value; + private final Map labels; + + public Measurement(final double value, final Map labels) { + this.value = value; + this.labels = labels; + } + + double value() { + return value; + } + + Map labels() { + return labels; + } +} diff --git a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java index e8ce7e5..e7db26d 100644 --- a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java +++ b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java @@ -385,10 +385,6 @@ public static T registerGauge( return number; } - public static MultiGauge registerMultiGauge(final String gaugeName) { - return MultiGauge.builder(gaugeName).register(meterRegistry); - } - /** * Registers a DistributionSummary (with predefined percentiles computed locally) for the given * name with the service's metric registry and reports it periodically to the configured @@ -468,16 +464,6 @@ public static void monitorExecutorService( new ExecutorServiceMetrics(executorService, name, toIterable(tags)).bindTo(meterRegistry); } - public static Iterable toIterable(Map tags) { - List newTags = new ArrayList<>(); - - if (tags != null) { - tags.forEach((k, v) -> newTags.add(new ImmutableTag(k, v))); - } - - return newTags; - } - public static MetricRegistry getMetricRegistry() { return METRIC_REGISTRY; } @@ -502,6 +488,20 @@ public static synchronized void stop() { isInit = false; } + static MultiGauge registerMultiGauge(final String gaugeName) { + return MultiGauge.builder(gaugeName).register(meterRegistry); + } + + static Iterable toIterable(Map tags) { + List newTags = new ArrayList<>(); + + if (tags != null) { + tags.forEach((k, v) -> newTags.add(new ImmutableTag(k, v))); + } + + return newTags; + } + /* * This is needed because ConsoleMetricReporter.stop() doesn't call report for the last time * before closing the scheduled thread diff --git a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/ResizeableGauge.java b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/ResizeableGauge.java new file mode 100644 index 0000000..aad9cbe --- /dev/null +++ b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/ResizeableGauge.java @@ -0,0 +1,30 @@ +package org.hypertrace.core.serviceframework.metrics; + +import static java.util.stream.Collectors.toUnmodifiableList; +import static org.hypertrace.core.serviceframework.metrics.PlatformMetricsRegistry.registerMultiGauge; +import static org.hypertrace.core.serviceframework.metrics.PlatformMetricsRegistry.toIterable; + +import io.micrometer.core.instrument.MultiGauge; +import io.micrometer.core.instrument.MultiGauge.Row; +import io.micrometer.core.instrument.Tags; +import java.util.Collection; +import java.util.List; + +public class ResizeableGauge { + private final MultiGauge multiGauge; + + public ResizeableGauge(final String name) { + this.multiGauge = registerMultiGauge(name); + } + + public void report(final Collection measurements) { + final List> rows = + measurements.stream() + .map( + measurement -> + Row.of(Tags.of(toIterable(measurement.labels())), measurement::value)) + .collect(toUnmodifiableList()); + + multiGauge.register(rows); + } +} From 7af34274d517f9cbc8ba37ab32326efa18da432f Mon Sep 17 00:00:00 2001 From: Suresh Prakash Date: Tue, 30 Jan 2024 16:27:46 +0530 Subject: [PATCH 22/23] Wrap metric reporting under try-catch block --- .../metrics/DocStoreMetricsRegistry.java | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java index 2a065c1..5a3e42d 100644 --- a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java +++ b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java @@ -127,20 +127,24 @@ private void monitorCustomMetric(final DocStoreCustomMetricReportingConfig repor private void report( final DocStoreCustomMetricReportingConfig reportingConfig, final ResizeableGauge resizeableGauge) { - final List customMetrics = - metricProvider.getCustomMetrics(reportingConfig.config()); - - log.debug( - "Reporting custom database metrics {} for configuration {}", - customMetrics, - reportingConfig); - - final List measurements = - customMetrics.stream() - .map(metric -> new Measurement(metric.value(), metric.labels())) - .collect(toUnmodifiableList()); - - resizeableGauge.report(measurements); + try { + final List customMetrics = + metricProvider.getCustomMetrics(reportingConfig.config()); + + log.debug( + "Reporting custom database metrics {} for configuration {}", + customMetrics, + reportingConfig); + + final List measurements = + customMetrics.stream() + .map(metric -> new Measurement(metric.value(), metric.labels())) + .collect(toUnmodifiableList()); + + resizeableGauge.report(measurements); + } catch (final Exception e) { + log.warn("Unable to report custom database metric for config: {}", reportingConfig, e); + } } private class StandardDocStoreMetricsRegistry { From 2aeaf180a43acfa6702036124a40fc6832cac140 Mon Sep 17 00:00:00 2001 From: Suresh Prakash Date: Wed, 31 Jan 2024 13:01:36 +0530 Subject: [PATCH 23/23] Handled review comments --- .../docstore/metrics/DocStoreMetricsRegistry.java | 3 ++- .../serviceframework/metrics/PlatformMetricsRegistry.java | 4 ++-- .../core/serviceframework/metrics/ResizeableGauge.java | 5 ++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java index 5a3e42d..dac2774 100644 --- a/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java +++ b/docstore-metrics/src/main/java/org/hypertrace/core/serviceframework/docstore/metrics/DocStoreMetricsRegistry.java @@ -4,6 +4,7 @@ import static java.util.concurrent.TimeUnit.MINUTES; import static java.util.concurrent.TimeUnit.SECONDS; import static java.util.stream.Collectors.toUnmodifiableList; +import static org.hypertrace.core.serviceframework.metrics.PlatformMetricsRegistry.registerResizeableGauge; import io.micrometer.common.lang.Nullable; import java.time.Duration; @@ -116,7 +117,7 @@ private void monitorCustomMetrics() { private void monitorCustomMetric(final DocStoreCustomMetricReportingConfig reportingConfig) { final ResizeableGauge resizeableGauge = - new ResizeableGauge(reportingConfig.config().metricName()); + registerResizeableGauge(reportingConfig.config().metricName()); executor.scheduleAtFixedRate( () -> report(reportingConfig, resizeableGauge), INITIAL_DELAY_SECONDS, diff --git a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java index e7db26d..8e3e50d 100644 --- a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java +++ b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/PlatformMetricsRegistry.java @@ -488,8 +488,8 @@ public static synchronized void stop() { isInit = false; } - static MultiGauge registerMultiGauge(final String gaugeName) { - return MultiGauge.builder(gaugeName).register(meterRegistry); + public static ResizeableGauge registerResizeableGauge(final String name) { + return new ResizeableGauge(MultiGauge.builder(name).register(meterRegistry)); } static Iterable toIterable(Map tags) { diff --git a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/ResizeableGauge.java b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/ResizeableGauge.java index aad9cbe..1c8a3b0 100644 --- a/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/ResizeableGauge.java +++ b/platform-metrics/src/main/java/org/hypertrace/core/serviceframework/metrics/ResizeableGauge.java @@ -1,7 +1,6 @@ package org.hypertrace.core.serviceframework.metrics; import static java.util.stream.Collectors.toUnmodifiableList; -import static org.hypertrace.core.serviceframework.metrics.PlatformMetricsRegistry.registerMultiGauge; import static org.hypertrace.core.serviceframework.metrics.PlatformMetricsRegistry.toIterable; import io.micrometer.core.instrument.MultiGauge; @@ -13,8 +12,8 @@ public class ResizeableGauge { private final MultiGauge multiGauge; - public ResizeableGauge(final String name) { - this.multiGauge = registerMultiGauge(name); + ResizeableGauge(final MultiGauge multiGauge) { + this.multiGauge = multiGauge; } public void report(final Collection measurements) {