<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ </dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<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>
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;
/**
* 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
*/
/**
* 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;
}
}
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;
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);
+ }
}
import java.time.Duration;
-import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.assertj.core.api.Assertions.assertThat;
class MetricsIT {
cut.collect(INTERVAL).take(2)
)
.consumeNextWith((collectedMetrics) -> {
- assertTrue(collectedMetrics.contains(COUNTER_NAME));
+ assertMetricsContain(collectedMetrics, COUNTER_NAME);
counter.increment();
})
.thenAwait(INTERVAL)
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();
+ }
}