Implement MoHeR metrics module 33/88233/3
authorFilip Krzywka <filip.krzywka@nokia.com>
Wed, 22 May 2019 09:44:35 +0000 (11:44 +0200)
committerFilip Krzywka <filip.krzywka@nokia.com>
Wed, 22 May 2019 10:32:09 +0000 (12:32 +0200)
Change-Id: I988e742d55217e6fbd0f17fa3364d6c4121a6e1d
Issue-ID: DCAEGEN2-152
Signed-off-by: Filip Krzywka <filip.krzywka@nokia.com>
pom.xml
standardization/moher-api/metrics/pom.xml
standardization/moher-api/metrics/src/main/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/api/Metrics.java [new file with mode: 0644]
standardization/moher-api/metrics/src/main/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/api/MetricsFactory.java [new file with mode: 0644]
standardization/moher-api/metrics/src/main/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/impl/MetricsImpl.java [new file with mode: 0644]
standardization/moher-api/metrics/src/test/java/org/onap/dcaegen2/services/sdk/standardization/moher/metrics/impl/MetricsIT.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index d043e13..3bc181b 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -39,6 +39,7 @@
     <commons-text.version>1.6</commons-text.version>
     <jetbrains-annotations.version>16.0.3</jetbrains-annotations.version>
     <protoc-jar-maven-plugin.version>3.6.0.2</protoc-jar-maven-plugin.version>
+    <micrometer.version>1.1.4</micrometer.version>
   </properties>
 
   <modules>
         <version>${logback.version}</version>
         <scope>runtime</scope>
       </dependency>
+      <dependency>
+        <groupId>io.micrometer</groupId>
+        <artifactId>micrometer-registry-prometheus</artifactId>
+        <version>${micrometer.version}</version>
+      </dependency>
 
       <dependency>
         <groupId>org.mockito</groupId>
         <version>${junit-jupiter.version}</version>
         <scope>test</scope>
       </dependency>
+      <dependency>
+        <groupId>org.junit.jupiter</groupId>
+        <artifactId>junit-jupiter-api</artifactId>
+        <version>${junit-jupiter.version}</version>
+        <scope>test</scope>
+      </dependency>
       <dependency>
         <groupId>org.assertj</groupId>
         <artifactId>assertj-core</artifactId>
index 3cfefed..fd0c967 100644 (file)
     <description>MoHeR standardization for providing application metrics</description>
     <artifactId>dcaegen2-sdk-moher-metrics</artifactId>
 
+    <dependencies>
+        <dependency>
+            <groupId>io.micrometer</groupId>
+            <artifactId>micrometer-registry-prometheus</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>io.projectreactor</groupId>
+            <artifactId>reactor-core</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>io.projectreactor</groupId>
+            <artifactId>reactor-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.junit.jupiter</groupId>
+            <artifactId>junit-jupiter-engine</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
 </project>
\ 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 (file)
index 0000000..66c3368
--- /dev/null
@@ -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<String> 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<String> 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 (file)
index 0000000..359f747
--- /dev/null
@@ -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.
+ *
+ * <p>Typical usage:</p>
+ *
+ * <pre>
+ *  // create registry
+ *  PrometheusMeterRegistry registry = MetricsFactory.createDefaultRegistry();
+ *  // create metrics
+ *  Metrics metrics = MetricsFactory.createMetrics(registry);
+ * </pre>
+ *
+ * @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 (file)
index 0000000..068a2db
--- /dev/null
@@ -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<String> collect() {
+        return Mono.just(registry.scrape());
+    }
+
+    @Override
+    public Flux<String> 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 (file)
index 0000000..f04636e
--- /dev/null
@@ -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();
+    }
+}