Bump checkstyle version
[dcaegen2/collectors/hv-ves.git] / hv-collector-main / src / main / kotlin / org / onap / dcae / collectors / veshv / main / MicrometerMetrics.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.syntax.function.memoize
23 import io.micrometer.core.instrument.Clock
24 import io.micrometer.core.instrument.Counter
25 import io.micrometer.core.instrument.MeterRegistry
26 import io.micrometer.jmx.JmxConfig
27 import io.micrometer.jmx.JmxMeterRegistry
28 import org.onap.dcae.collectors.veshv.boundary.Metrics
29
30 /**
31  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
32  * @since June 2018
33  */
34 class MicrometerMetrics(
35         private val registry: MeterRegistry = JmxMeterRegistry(JmxConfig.DEFAULT, Clock.SYSTEM)
36 ) : Metrics {
37
38     private val receivedBytes = registry.counter("data.received.bytes")
39     private val receivedMsgCount = registry.counter("messages.received.count")
40     private val receivedMsgBytes = registry.counter("messages.received.bytes")
41     private val sentCountTotal = registry.counter("messages.sent.count")
42
43     init {
44         registry.gauge("messages.processing.count", this) {
45             (receivedMsgCount.count() - sentCountTotal.count()).coerceAtLeast(0.0)
46         }
47     }
48
49     private val sentCount = { topic: String ->
50         registry.counter("messages.sent.count", "topic", topic)
51     }.memoize<String, Counter>()
52
53
54     override fun notifyBytesReceived(size: Int) {
55         receivedBytes.increment(size.toDouble())
56     }
57
58     override fun notifyMessageReceived(size: Int) {
59         receivedMsgCount.increment()
60         receivedMsgBytes.increment(size.toDouble())
61     }
62
63     override fun notifyMessageSent(topic: String) {
64         sentCountTotal.increment()
65         sentCount(topic).increment()
66     }
67 }