2 * ============LICENSE_START=======================================================
3 * dcaegen2-collectors-veshv
4 * ================================================================================
5 * Copyright (C) 2018 NOKIA
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.dcae.collectors.veshv.main
23 import io.micrometer.core.instrument.Counter
24 import io.micrometer.core.instrument.Gauge
25 import io.micrometer.core.instrument.search.RequiredSearch
26 import io.micrometer.core.instrument.simple.SimpleMeterRegistry
27 import io.micrometer.prometheus.PrometheusConfig
28 import io.micrometer.prometheus.PrometheusMeterRegistry
29 import org.assertj.core.api.Assertions.assertThat
30 import org.assertj.core.data.Percentage
31 import org.jetbrains.spek.api.Spek
32 import org.jetbrains.spek.api.dsl.describe
33 import org.jetbrains.spek.api.dsl.it
34 import org.jetbrains.spek.api.dsl.on
35 import org.onap.dcae.collectors.veshv.healthcheck.ports.PrometheusMetricsProvider
36 import org.onap.dcae.collectors.veshv.main.metrics.MicrometerMetrics
37 import org.onap.dcae.collectors.veshv.main.metrics.MicrometerMetrics.Companion.PREFIX
40 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
43 object MicrometerMetricsTest : Spek({
44 val doublePrecision = Percentage.withPercentage(0.5)
45 lateinit var registry: PrometheusMeterRegistry
46 lateinit var cut: MicrometerMetrics
49 registry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT)
50 cut = MicrometerMetrics(registry)
53 fun registrySearch() = RequiredSearch.`in`(registry)
55 fun <M, T> verifyMeter(search: RequiredSearch, map: (RequiredSearch) -> M, verifier: (M) -> T) =
59 { ex -> assertThat(ex).doesNotThrowAnyException() },
63 fun <T> verifyGauge(name: String, verifier: (Gauge) -> T) =
64 verifyMeter(registrySearch().name(name), RequiredSearch::gauge, verifier)
66 fun <T> verifyCounter(search: RequiredSearch, verifier: (Counter) -> T) =
67 verifyMeter(search, RequiredSearch::counter, verifier)
69 fun <T> verifyCounter(name: String, verifier: (Counter) -> T) =
70 verifyCounter(registrySearch().name(name), verifier)
72 fun verifyAllCountersAreUnchangedBut(vararg changedCounters: String) {
74 .filter { it is Counter }
75 .map { it as Counter }
76 .filterNot { it.id.name in changedCounters }
78 assertThat(it.count()).describedAs(it.id.toString()).isCloseTo(0.0, doublePrecision)
82 describe("notifyBytesReceived") {
84 on("$PREFIX.data.received.bytes counter") {
85 val counterName = "$PREFIX.data.received.bytes"
87 it("should increment counter") {
89 cut.notifyBytesReceived(bytes)
91 verifyCounter(counterName) { counter ->
92 assertThat(counter.count()).isCloseTo(bytes.toDouble(), doublePrecision)
96 it("should leave all other counters unchanged") {
97 cut.notifyBytesReceived(128)
98 verifyAllCountersAreUnchangedBut(counterName)
103 describe("notifyMessageReceived") {
104 on("$PREFIX.messages.received.count counter") {
105 val counterName = "$PREFIX.messages.received.count"
107 it("should increment counter") {
108 cut.notifyMessageReceived(777)
110 verifyCounter(counterName) { counter ->
111 assertThat(counter.count()).isCloseTo(1.0, doublePrecision)
116 on("$PREFIX.messages.received.bytes counter") {
117 val counterName = "$PREFIX.messages.received.bytes"
119 it("should increment counter") {
121 cut.notifyMessageReceived(bytes)
123 verifyCounter(counterName) { counter ->
124 assertThat(counter.count()).isCloseTo(bytes.toDouble(), doublePrecision)
129 it("should leave all other counters unchanged") {
130 cut.notifyMessageReceived(128)
131 verifyAllCountersAreUnchangedBut("$PREFIX.messages.received.count", "$PREFIX.messages.received.bytes")
135 describe("notifyMessageSent") {
136 val topicName1 = "PERF3GPP"
137 val topicName2 = "CALLTRACE"
139 on("$PREFIX.messages.sent.count counter") {
140 val counterName = "$PREFIX.messages.sent.count"
142 it("should increment counter") {
143 cut.notifyMessageSent(topicName1)
145 verifyCounter(counterName) { counter ->
146 assertThat(counter.count()).isCloseTo(1.0, doublePrecision)
148 verifyAllCountersAreUnchangedBut(counterName, "$PREFIX.messages.sent.topic.count")
152 on("$PREFIX.messages.sent.topic.count counter") {
153 val counterName = "$PREFIX.messages.sent.topic.count"
154 it("should handle counters for different topics") {
155 cut.notifyMessageSent(topicName1)
156 cut.notifyMessageSent(topicName2)
157 cut.notifyMessageSent(topicName2)
159 verifyCounter(registrySearch().name(counterName).tag("topic", topicName1)) { counter ->
160 assertThat(counter.count()).isCloseTo(1.0, doublePrecision)
163 verifyCounter(registrySearch().name(counterName).tag("topic", topicName2)) { counter ->
164 assertThat(counter.count()).isCloseTo(2.0, doublePrecision)
170 describe("processing gauge") {
171 it("should show difference between sent and received messages") {
173 on("positive difference") {
174 cut.notifyMessageReceived(128)
175 cut.notifyMessageReceived(256)
176 cut.notifyMessageReceived(256)
177 cut.notifyMessageSent("perf3gpp")
178 verifyGauge("messages.processing.count") { gauge ->
179 assertThat(gauge.value()).isCloseTo(2.0, doublePrecision)
183 on("zero difference") {
184 cut.notifyMessageReceived(128)
185 cut.notifyMessageSent("perf3gpp")
186 verifyGauge("messages.processing.count") { gauge ->
187 assertThat(gauge.value()).isCloseTo(0.0, doublePrecision)
191 on("negative difference") {
192 cut.notifyMessageReceived(128)
193 cut.notifyMessageSent("fault")
194 cut.notifyMessageSent("perf3gpp")
195 verifyGauge("messages.processing.count") { gauge ->
196 assertThat(gauge.value()).isCloseTo(0.0, doublePrecision)