From 21f6895b4127a6d61e2d6718967bdd1de250eb90 Mon Sep 17 00:00:00 2001 From: Filip Krzywka Date: Wed, 22 May 2019 11:44:35 +0200 Subject: Implement MoHeR metrics module Change-Id: I988e742d55217e6fbd0f17fa3364d6c4121a6e1d Issue-ID: DCAEGEN2-152 Signed-off-by: Filip Krzywka --- pom.xml | 12 +++ standardization/moher-api/metrics/pom.xml | 21 ++++++ .../standardization/moher/metrics/api/Metrics.java | 54 ++++++++++++++ .../moher/metrics/api/MetricsFactory.java | 61 +++++++++++++++ .../moher/metrics/impl/MetricsImpl.java | 48 ++++++++++++ .../moher/metrics/impl/MetricsIT.java | 87 ++++++++++++++++++++++ 6 files changed, 283 insertions(+) create mode 100644 standardization/moher-api/metrics/src/main/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/api/Metrics.java create mode 100644 standardization/moher-api/metrics/src/main/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/api/MetricsFactory.java create mode 100644 standardization/moher-api/metrics/src/main/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/impl/MetricsImpl.java create mode 100644 standardization/moher-api/metrics/src/test/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/impl/MetricsIT.java diff --git a/pom.xml b/pom.xml index d043e132..3bc181b9 100644 --- a/pom.xml +++ b/pom.xml @@ -39,6 +39,7 @@ 1.6 16.0.3 3.6.0.2 + 1.1.4 @@ -211,6 +212,11 @@ ${logback.version} runtime + + io.micrometer + micrometer-registry-prometheus + ${micrometer.version} + org.mockito @@ -224,6 +230,12 @@ ${junit-jupiter.version} test + + org.junit.jupiter + junit-jupiter-api + ${junit-jupiter.version} + test + org.assertj assertj-core diff --git a/standardization/moher-api/metrics/pom.xml b/standardization/moher-api/metrics/pom.xml index 3cfefed6..fd0c9678 100644 --- a/standardization/moher-api/metrics/pom.xml +++ b/standardization/moher-api/metrics/pom.xml @@ -33,4 +33,25 @@ MoHeR standardization for providing application metrics dcaegen2-sdk-moher-metrics + + + io.micrometer + micrometer-registry-prometheus + + + io.projectreactor + reactor-core + + + + io.projectreactor + reactor-test + test + + + org.junit.jupiter + junit-jupiter-engine + test + + \ No newline at end of file diff --git a/standardization/moher-api/metrics/src/main/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/api/Metrics.java b/standardization/moher-api/metrics/src/main/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/api/Metrics.java new file mode 100644 index 00000000..66c33680 --- /dev/null +++ b/standardization/moher-api/metrics/src/main/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/api/Metrics.java @@ -0,0 +1,54 @@ +/* + * ============LICENSE_START==================================== + * DCAEGEN2-SERVICES-SDK + * ========================================================= + * Copyright (C) 2019 Nokia. All rights reserved. + * ========================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END===================================== + */ + +package org.onap.dcaegen2.services.sdk.standardization.moher.metrics.api; + +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import java.time.Duration; + +/** + * Object responsible for returning application metrics. + * + * @since 1.2.0 + */ +public interface Metrics { + + /** + * Return all gathered metrics. + * + * Returns a Publisher that will emit a single string containing all + * metrics with current values and finish afterwards. + * + * @since 1.2.0 + */ + Mono collect(); + + /** + * Returns all gathered metrics. + * + * Returns a Publisher that will emit string containing all metrics with current values in intervals. + * + * @param interval interval in which Publisher should return metrics + * @since 1.2.0 + */ + Flux collect(Duration interval); +} \ No newline at end of file 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 new file mode 100644 index 00000000..359f7470 --- /dev/null +++ b/standardization/moher-api/metrics/src/main/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/api/MetricsFactory.java @@ -0,0 +1,61 @@ +/* + * ============LICENSE_START==================================== + * DCAEGEN2-SERVICES-SDK + * ========================================================= + * Copyright (C) 2019 Nokia. All rights reserved. + * ========================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END===================================== + */ + +package org.onap.dcaegen2.services.sdk.standardization.moher.metrics.api; + +import io.micrometer.prometheus.PrometheusConfig; +import io.micrometer.prometheus.PrometheusMeterRegistry; +import org.onap.dcaegen2.services.sdk.standardization.moher.metrics.impl.MetricsImpl; + +/** + * Factory for creating {@link Metrics} object. + * + *

Typical usage:

+ * + *
+ *  // create registry
+ *  PrometheusMeterRegistry registry = MetricsFactory.createDefaultRegistry();
+ *  // create metrics
+ *  Metrics metrics = MetricsFactory.createMetrics(registry);
+ * 
+ * + * @since 1.2.0 + */ +public class MetricsFactory { + + /** + * Method for creating default Prometheus registry. + * + * @since 1.2.0 + */ + public static PrometheusMeterRegistry createDefaultRegistry() { + return new PrometheusMeterRegistry(PrometheusConfig.DEFAULT); + } + + /** + * Method for creating {@link Metrics} with configured Prometheus registry. + * + * @param registry Prometheus registry to be used + * @since 1.2.0 + */ + public static Metrics createMetrics(PrometheusMeterRegistry registry) { + return new MetricsImpl(registry); + } +} 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 new file mode 100644 index 00000000..068a2db4 --- /dev/null +++ b/standardization/moher-api/metrics/src/main/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/impl/MetricsImpl.java @@ -0,0 +1,48 @@ +/* + * ============LICENSE_START==================================== + * DCAEGEN2-SERVICES-SDK + * ========================================================= + * Copyright (C) 2019 Nokia. All rights reserved. + * ========================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END===================================== + */ + +package org.onap.dcaegen2.services.sdk.standardization.moher.metrics.impl; + + +import io.micrometer.prometheus.PrometheusMeterRegistry; +import org.onap.dcaegen2.services.sdk.standardization.moher.metrics.api.Metrics; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import java.time.Duration; + +public class MetricsImpl implements Metrics { + private final PrometheusMeterRegistry registry; + + public MetricsImpl(PrometheusMeterRegistry registry) { + this.registry = registry; + } + + @Override + public Mono collect() { + return Mono.just(registry.scrape()); + } + + @Override + public Flux collect(Duration interval) { + return Flux.interval(interval) + .map((l) -> registry.scrape()); + } +} 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 new file mode 100644 index 00000000..f04636e9 --- /dev/null +++ b/standardization/moher-api/metrics/src/test/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/impl/MetricsIT.java @@ -0,0 +1,87 @@ +/* + * ============LICENSE_START==================================== + * DCAEGEN2-SERVICES-SDK + * ========================================================= + * Copyright (C) 2019 Nokia. All rights reserved. + * ========================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END===================================== + */ + +package org.onap.dcaegen2.services.sdk.standardization.moher.metrics.impl; + +import io.micrometer.core.instrument.Counter; +import io.micrometer.prometheus.PrometheusMeterRegistry; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.sdk.standardization.moher.metrics.api.Metrics; +import org.onap.dcaegen2.services.sdk.standardization.moher.metrics.api.MetricsFactory; +import reactor.test.StepVerifier; + +import java.time.Duration; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +class MetricsIT { + + private static final String COUNTER_NAME = "Duplicates_Amount"; + private static final String GAUGE_NAME = "gauge_Duplicates_Amount"; + private static final String TIMER_NAME = "Duplicates_Amount_timer"; + private static final String SUMMARY_NAME = "Duplicates_Amount_summary"; + private static final Duration INTERVAL = Duration.ofMillis(100); + + private PrometheusMeterRegistry defaultRegistry; + private Metrics cut; + + @BeforeEach + void setup() { + defaultRegistry = MetricsFactory.createDefaultRegistry(); + cut = MetricsFactory.createMetrics(defaultRegistry); + } + + @Test + void metrics_givenDefaultRegistry_shouldReturnCreatedMeters() { + + Counter counter = defaultRegistry.counter(COUNTER_NAME); + defaultRegistry.gauge(GAUGE_NAME, counter.count()); + defaultRegistry.timer(TIMER_NAME); + defaultRegistry.summary(SUMMARY_NAME); + + StepVerifier.create(cut.collect()) + .expectNextMatches((collectedMetrics) -> + collectedMetrics.contains(COUNTER_NAME) && + collectedMetrics.contains(GAUGE_NAME) && + collectedMetrics.contains(TIMER_NAME) && + collectedMetrics.contains(SUMMARY_NAME) + ) + .verifyComplete(); + } + + @Test + void metrics_givenDefaultRegistry_shouldReturnCreatedMetersInIntervals() { + + Counter counter = defaultRegistry.counter(COUNTER_NAME); + + StepVerifier.create( + cut.collect(INTERVAL).take(2) + ) + .consumeNextWith((collectedMetrics) -> { + assertTrue(collectedMetrics.contains(COUNTER_NAME)); + counter.increment(); + }) + .thenAwait(INTERVAL) + .expectNextMatches((collectedMetrics) -> + collectedMetrics.contains(COUNTER_NAME + "_total 1.0")) + .verifyComplete(); + } +} -- cgit 1.2.3-korg