diff options
Diffstat (limited to 'standardization')
4 files changed, 58 insertions, 3 deletions
diff --git a/standardization/moher-api/metrics/pom.xml b/standardization/moher-api/metrics/pom.xml index fd0c9678..7c5a499f 100644 --- a/standardization/moher-api/metrics/pom.xml +++ b/standardization/moher-api/metrics/pom.xml @@ -42,6 +42,10 @@ <groupId>io.projectreactor</groupId> <artifactId>reactor-core</artifactId> </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + </dependency> <dependency> <groupId>io.projectreactor</groupId> @@ -49,6 +53,11 @@ <scope>test</scope> </dependency> <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <scope>test</scope> diff --git a/standardization/moher-api/metrics/src/main/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/api/MetricsFactory.java b/standardization/moher-api/metrics/src/main/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/api/MetricsFactory.java index 359f7470..33772c2f 100644 --- a/standardization/moher-api/metrics/src/main/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/api/MetricsFactory.java +++ b/standardization/moher-api/metrics/src/main/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/api/MetricsFactory.java @@ -20,6 +20,7 @@ package org.onap.dcaegen2.services.sdk.standardization.moher.metrics.api; +import io.micrometer.core.instrument.binder.MeterBinder; import io.micrometer.prometheus.PrometheusConfig; import io.micrometer.prometheus.PrometheusMeterRegistry; import org.onap.dcaegen2.services.sdk.standardization.moher.metrics.impl.MetricsImpl; @@ -42,6 +43,14 @@ public class MetricsFactory { /** * Method for creating default Prometheus registry. + * <p> + * Client is expected to populate registry with custom metrics. + * Exact procedure can be found at Micrometer project site. + * </p> + * <p> + * It is recommended to search through available implementations of {@link MeterBinder} + * as there exists a plenty of them for most common use cases. + * </p> * * @since 1.2.0 */ @@ -51,11 +60,16 @@ public class MetricsFactory { /** * Method for creating {@link Metrics} with configured Prometheus registry. + * <p> + * Returned object will add to registry few default JVM (memory and threads usage, garbage collection) + * and system metrics (CPU usage). For exact list of metrics added, please refer to implementation. * * @param registry Prometheus registry to be used * @since 1.2.0 */ public static Metrics createMetrics(PrometheusMeterRegistry registry) { - return new MetricsImpl(registry); + MetricsImpl metrics = new MetricsImpl(registry); + metrics.configureDefaultMetrics(); + return metrics; } } diff --git a/standardization/moher-api/metrics/src/main/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/impl/MetricsImpl.java b/standardization/moher-api/metrics/src/main/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/impl/MetricsImpl.java index 068a2db4..5fe4e12a 100644 --- a/standardization/moher-api/metrics/src/main/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/impl/MetricsImpl.java +++ b/standardization/moher-api/metrics/src/main/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/impl/MetricsImpl.java @@ -21,6 +21,11 @@ package org.onap.dcaegen2.services.sdk.standardization.moher.metrics.impl; +import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics; +import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics; +import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics; +import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics; +import io.micrometer.core.instrument.binder.system.ProcessorMetrics; import io.micrometer.prometheus.PrometheusMeterRegistry; import org.onap.dcaegen2.services.sdk.standardization.moher.metrics.api.Metrics; import reactor.core.publisher.Flux; @@ -45,4 +50,12 @@ public class MetricsImpl implements Metrics { return Flux.interval(interval) .map((l) -> registry.scrape()); } + + public void configureDefaultMetrics(){ + new ClassLoaderMetrics().bindTo(registry); + new JvmThreadMetrics().bindTo(registry); + new JvmMemoryMetrics().bindTo(registry); + new JvmGcMetrics().bindTo(registry); + new ProcessorMetrics().bindTo(registry); + } } diff --git a/standardization/moher-api/metrics/src/test/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/impl/MetricsIT.java b/standardization/moher-api/metrics/src/test/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/impl/MetricsIT.java index f04636e9..d024d08d 100644 --- a/standardization/moher-api/metrics/src/test/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/impl/MetricsIT.java +++ b/standardization/moher-api/metrics/src/test/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/impl/MetricsIT.java @@ -30,7 +30,7 @@ import reactor.test.StepVerifier; import java.time.Duration; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; class MetricsIT { @@ -76,7 +76,7 @@ class MetricsIT { cut.collect(INTERVAL).take(2) ) .consumeNextWith((collectedMetrics) -> { - assertTrue(collectedMetrics.contains(COUNTER_NAME)); + assertMetricsContain(collectedMetrics, COUNTER_NAME); counter.increment(); }) .thenAwait(INTERVAL) @@ -84,4 +84,23 @@ class MetricsIT { collectedMetrics.contains(COUNTER_NAME + "_total 1.0")) .verifyComplete(); } + + @Test + void metrics_shouldIncludeSomeDefaultMetrics() { + StepVerifier.create(cut.collect()) + .consumeNextWith((collectedMetrics) -> { + assertMetricsContain(collectedMetrics, "jvm_threads"); + assertMetricsContain(collectedMetrics, "jvm_memory"); + assertMetricsContain(collectedMetrics, "jvm_classes"); + assertMetricsContain(collectedMetrics, "jvm_gc"); + assertMetricsContain(collectedMetrics, "system_cpu"); + }) + .verifyComplete(); + } + + private void assertMetricsContain(final String collectedMetrics, final String metricName) { + assertThat(collectedMetrics.contains(metricName)) + .describedAs(String.format("Expected metric: %s", metricName)) + .isTrue(); + } } |