2 * ============LICENSE_START=======================================================
3 * dcaegen2-collectors-veshv
4 * ================================================================================
5 * Copyright (C) 2019 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.kafkaconsumer.metrics
22 import io.micrometer.prometheus.PrometheusConfig
23 import io.micrometer.prometheus.PrometheusMeterRegistry
24 import org.assertj.core.api.Assertions.assertThat
25 import org.assertj.core.data.Percentage
26 import org.jetbrains.spek.api.Spek
27 import org.jetbrains.spek.api.dsl.describe
28 import org.jetbrains.spek.api.dsl.it
29 import org.jetbrains.spek.api.dsl.on
30 import org.onap.dcae.collectors.veshv.tests.utils.verifyGauge
31 import org.onap.dcae.collectors.veshv.tests.utils.verifyTimer
32 import java.time.Instant
33 import java.util.concurrent.TimeUnit
35 object MicrometerMetricsTest : Spek({
36 val PREFIX = "hv-kafka-consumer"
37 val doublePrecision = Percentage.withPercentage(0.5)
38 lateinit var registry: PrometheusMeterRegistry
39 lateinit var cut: MicrometerMetrics
42 registry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT)
43 cut = MicrometerMetrics(registry)
47 val arbitraryMessageTravelTime = 100L
48 val messageSentTimeMicros = Instant.now().minusMillis(arbitraryMessageTravelTime).toEpochMilli() * 1000
49 val timerName = "$PREFIX.travel.time"
51 on("notifyMessageTravelTime") {
52 it("should update timer $timerName") {
54 val timeBeforeNotifyMicros = Instant.now().toEpochMilli() * 1000
55 cut.notifyMessageTravelTime(messageSentTimeMicros)
56 val timeAfterNotifyMicros = Instant.now().toEpochMilli() * 1000
58 registry.verifyTimer(timerName) { timer ->
59 val travelTimeBeforeNotify = (timeBeforeNotifyMicros - messageSentTimeMicros).toDouble()
60 val travelTimeAfterNotify = (timeAfterNotifyMicros - messageSentTimeMicros).toDouble()
61 assertThat(timer.totalTime(TimeUnit.MICROSECONDS))
62 .isLessThanOrEqualTo(travelTimeAfterNotify)
63 .isGreaterThanOrEqualTo(travelTimeBeforeNotify)
71 val gaugeName = "$PREFIX.consumer.offset"
73 on("notifyOffsetChanged") {
76 it("should update $gaugeName") {
77 cut.notifyOffsetChanged(offset)
79 registry.verifyGauge(gaugeName) {
80 assertThat(it.value()).isCloseTo(offset.toDouble(), doublePrecision)