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.apache.kafka.common.TopicPartition
25 import org.assertj.core.api.Assertions.assertThat
26 import org.assertj.core.data.Percentage
27 import org.jetbrains.spek.api.Spek
28 import org.jetbrains.spek.api.dsl.describe
29 import org.jetbrains.spek.api.dsl.it
30 import org.jetbrains.spek.api.dsl.on
31 import org.onap.dcae.collectors.veshv.tests.utils.verifyGauge
32 import org.onap.dcae.collectors.veshv.tests.utils.verifyTimer
33 import java.time.Instant
34 import java.util.concurrent.TimeUnit
36 object MicrometerMetricsTest : Spek({
37 val PREFIX = "hv-kafka-consumer"
38 val doublePrecision = Percentage.withPercentage(0.5)
39 lateinit var registry: PrometheusMeterRegistry
40 lateinit var cut: MicrometerMetrics
43 registry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT)
44 cut = MicrometerMetrics(registry)
48 val arbitraryMessageTravelTime = 100L
49 val messageSentTimeMicros = Instant.now().minusMillis(arbitraryMessageTravelTime).toEpochMilli() * 1000
50 val timerName = "$PREFIX.travel.time"
52 on("notifyMessageTravelTime") {
53 it("should update timer $timerName") {
55 val timeBeforeNotifyMicros = Instant.now().toEpochMilli() * 1000
56 cut.notifyMessageTravelTime(messageSentTimeMicros)
57 val timeAfterNotifyMicros = Instant.now().toEpochMilli() * 1000
59 registry.verifyTimer(timerName) { timer ->
60 val travelTimeBeforeNotify = (timeBeforeNotifyMicros - messageSentTimeMicros).toDouble()
61 val travelTimeAfterNotify = (timeAfterNotifyMicros - messageSentTimeMicros).toDouble()
62 assertThat(timer.totalTime(TimeUnit.MICROSECONDS))
63 .isLessThanOrEqualTo(travelTimeAfterNotify)
64 .isGreaterThanOrEqualTo(travelTimeBeforeNotify)
72 val gaugeName = "$PREFIX.consumer.offset.topic"
74 on("notifyOffsetChanged") {
76 val topicPartition = TopicPartition("sample_topic", 1)
78 it("should update $gaugeName") {
79 cut.notifyOffsetChanged(offset, topicPartition)
81 registry.verifyGauge(gaugeName) {
82 assertThat(it.value()).isCloseTo(offset.toDouble(), doublePrecision)