diff options
author | Filip Krzywka <filip.krzywka@nokia.com> | 2019-05-22 11:44:35 +0200 |
---|---|---|
committer | Filip Krzywka <filip.krzywka@nokia.com> | 2019-05-22 12:32:09 +0200 |
commit | 21f6895b4127a6d61e2d6718967bdd1de250eb90 (patch) | |
tree | e7e63c4a485cedd8323cdbfa1ba3b1f9c868c11b | |
parent | c12ea59982286a68aeb9ebc50ed0acd0b0a1140d (diff) |
Implement MoHeR metrics module
Change-Id: I988e742d55217e6fbd0f17fa3364d6c4121a6e1d
Issue-ID: DCAEGEN2-152
Signed-off-by: Filip Krzywka <filip.krzywka@nokia.com>
6 files changed, 283 insertions, 0 deletions
@@ -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> @@ -211,6 +212,11 @@ <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> @@ -225,6 +231,12 @@ <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> <version>${assertj-core.version}</version> 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 @@ <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 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<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 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. + * + * <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 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<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 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(); + } +} |