Add metrics for dropped messages
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-main / src / test / kotlin / org / onap / dcae / collectors / veshv / main / MicrometerMetricsTest.kt
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.dcae.collectors.veshv.main
21
22 import arrow.core.Try
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.prometheus.PrometheusConfig
27 import io.micrometer.prometheus.PrometheusMeterRegistry
28 import org.assertj.core.api.Assertions.assertThat
29 import org.assertj.core.data.Percentage
30 import org.jetbrains.spek.api.Spek
31 import org.jetbrains.spek.api.dsl.describe
32 import org.jetbrains.spek.api.dsl.it
33 import org.jetbrains.spek.api.dsl.on
34 import org.onap.dcae.collectors.veshv.main.metrics.MicrometerMetrics
35 import org.onap.dcae.collectors.veshv.main.metrics.MicrometerMetrics.Companion.PREFIX
36 import org.onap.dcae.collectors.veshv.model.MessageDropCause.INVALID_MESSAGE
37 import org.onap.dcae.collectors.veshv.model.MessageDropCause.ROUTE_NOT_FOUND
38
39 /**
40  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
41  * @since June 2018
42  */
43 object MicrometerMetricsTest : Spek({
44     val doublePrecision = Percentage.withPercentage(0.5)
45     lateinit var registry: PrometheusMeterRegistry
46     lateinit var cut: MicrometerMetrics
47
48     beforeEachTest {
49         registry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT)
50         cut = MicrometerMetrics(registry)
51     }
52
53     fun registrySearch() = RequiredSearch.`in`(registry)
54
55     fun <M, T> verifyMeter(search: RequiredSearch, map: (RequiredSearch) -> M, verifier: (M) -> T) =
56             Try {
57                 map(search)
58             }.fold(
59                     { ex -> assertThat(ex).doesNotThrowAnyException() },
60                     verifier
61             )
62
63     fun <T> verifyGauge(name: String, verifier: (Gauge) -> T) =
64             verifyMeter(registrySearch().name(name), RequiredSearch::gauge, verifier)
65
66     fun <T> verifyCounter(search: RequiredSearch, verifier: (Counter) -> T) =
67             verifyMeter(search, RequiredSearch::counter, verifier)
68
69     fun <T> verifyCounter(name: String, verifier: (Counter) -> T) =
70             verifyCounter(registrySearch().name(name), verifier)
71
72     fun verifyAllCountersAreUnchangedBut(vararg changedCounters: String) {
73         registry.meters
74                 .filter { it is Counter }
75                 .map { it as Counter }
76                 .filterNot { it.id.name in changedCounters }
77                 .forEach {
78                     assertThat(it.count()).describedAs(it.id.toString()).isCloseTo(0.0, doublePrecision)
79                 }
80     }
81
82     describe("notifyBytesReceived") {
83
84         on("$PREFIX.data.received.bytes counter") {
85             val counterName = "$PREFIX.data.received.bytes"
86
87             it("should increment counter") {
88                 val bytes = 128
89                 cut.notifyBytesReceived(bytes)
90
91                 verifyCounter(counterName) {
92                     assertThat(it.count()).isCloseTo(bytes.toDouble(), doublePrecision)
93                 }
94             }
95
96             it("should leave all other counters unchanged") {
97                 cut.notifyBytesReceived(128)
98                 verifyAllCountersAreUnchangedBut(counterName)
99             }
100         }
101     }
102
103     describe("notifyMessageReceived") {
104         on("$PREFIX.messages.received.count counter") {
105             val counterName = "$PREFIX.messages.received.count"
106
107             it("should increment counter") {
108                 cut.notifyMessageReceived(777)
109
110                 verifyCounter(counterName) {
111                     assertThat(it.count()).isCloseTo(1.0, doublePrecision)
112                 }
113             }
114         }
115
116         on("$PREFIX.messages.received.bytes counter") {
117             val counterName = "$PREFIX.messages.received.bytes"
118
119             it("should increment counter") {
120                 val bytes = 888
121                 cut.notifyMessageReceived(bytes)
122
123                 verifyCounter(counterName) {
124                     assertThat(it.count()).isCloseTo(bytes.toDouble(), doublePrecision)
125                 }
126             }
127         }
128
129         it("should leave all other counters unchanged") {
130             cut.notifyMessageReceived(128)
131             verifyAllCountersAreUnchangedBut(
132                     "$PREFIX.messages.received.count",
133                     "$PREFIX.messages.received.bytes"
134             )
135         }
136     }
137
138     describe("notifyMessageSent") {
139         val topicName1 = "PERF3GPP"
140         val topicName2 = "CALLTRACE"
141
142         on("$PREFIX.messages.sent.count.total counter") {
143             val counterName = "$PREFIX.messages.sent.count.total"
144
145             it("should increment counter") {
146                 cut.notifyMessageSent(topicName1)
147
148                 verifyCounter(counterName) {
149                     assertThat(it.count()).isCloseTo(1.0, doublePrecision)
150                 }
151                 verifyAllCountersAreUnchangedBut(counterName, "$PREFIX.messages.sent.count.topic")
152             }
153         }
154
155         on("$PREFIX.messages.sent.topic.count counter") {
156             val counterName = "$PREFIX.messages.sent.count.topic"
157             it("should handle counters for different topics") {
158                 cut.notifyMessageSent(topicName1)
159                 cut.notifyMessageSent(topicName2)
160                 cut.notifyMessageSent(topicName2)
161
162                 verifyCounter(registrySearch().name(counterName).tag("topic", topicName1)) {
163                     assertThat(it.count()).isCloseTo(1.0, doublePrecision)
164                 }
165
166                 verifyCounter(registrySearch().name(counterName).tag("topic", topicName2)) {
167                     assertThat(it.count()).isCloseTo(2.0, doublePrecision)
168                 }
169             }
170         }
171     }
172
173     describe("notifyMessageDropped") {
174
175         on("$PREFIX.messages.dropped.count.total counter") {
176             val counterName = "$PREFIX.messages.dropped.count.total"
177             it("should increment counter") {
178                 cut.notifyMessageDropped(ROUTE_NOT_FOUND)
179                 cut.notifyMessageDropped(INVALID_MESSAGE)
180
181                 verifyCounter(counterName) {
182                     assertThat(it.count()).isCloseTo(2.0, doublePrecision)
183                 }
184                 verifyAllCountersAreUnchangedBut(counterName, "$PREFIX.messages.dropped.count.cause")
185             }
186         }
187
188         on("$PREFIX.messages.dropped.count.cause counter") {
189             val counterName = "$PREFIX.messages.dropped.count.cause"
190             it("should handle counters for different drop reasons") {
191                 cut.notifyMessageDropped(ROUTE_NOT_FOUND)
192                 cut.notifyMessageDropped(INVALID_MESSAGE)
193                 cut.notifyMessageDropped(INVALID_MESSAGE)
194
195                 verifyCounter(registrySearch().name(counterName).tag("cause", ROUTE_NOT_FOUND.tag)) {
196                     assertThat(it.count()).isCloseTo(1.0, doublePrecision)
197                 }
198
199                 verifyCounter(registrySearch().name(counterName).tag("cause", INVALID_MESSAGE.tag)) {
200                     assertThat(it.count()).isCloseTo(2.0, doublePrecision)
201                 }
202             }
203         }
204     }
205
206     describe("processing gauge") {
207         it("should show difference between sent and received messages") {
208
209             on("positive difference") {
210                 cut.notifyMessageReceived(128)
211                 cut.notifyMessageReceived(256)
212                 cut.notifyMessageReceived(256)
213                 cut.notifyMessageSent("perf3gpp")
214                 verifyGauge("messages.processing.count") {
215                     assertThat(it.value()).isCloseTo(2.0, doublePrecision)
216                 }
217             }
218
219             on("zero difference") {
220                 cut.notifyMessageReceived(128)
221                 cut.notifyMessageSent("perf3gpp")
222                 verifyGauge("messages.processing.count") {
223                     assertThat(it.value()).isCloseTo(0.0, doublePrecision)
224                 }
225             }
226
227             on("negative difference") {
228                 cut.notifyMessageReceived(128)
229                 cut.notifyMessageSent("fault")
230                 cut.notifyMessageSent("perf3gpp")
231                 verifyGauge("messages.processing.count") {
232                     assertThat(it.value()).isCloseTo(0.0, doublePrecision)
233                 }
234             }
235         }
236     }
237
238 })