cf903591aff03e7501db70ad5ca1500c3dccebc7
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-main / src / main / kotlin / org / onap / dcae / collectors / veshv / main / metrics / 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.metrics
21
22 import arrow.syntax.function.memoize
23 import io.micrometer.core.instrument.Counter
24 import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics
25 import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics
26 import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics
27 import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics
28 import io.micrometer.core.instrument.binder.system.ProcessorMetrics
29 import io.micrometer.prometheus.PrometheusConfig
30 import io.micrometer.prometheus.PrometheusMeterRegistry
31 import org.onap.dcae.collectors.veshv.boundary.Metrics
32
33
34 /**
35  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
36  * @since June 2018
37  */
38 class MicrometerMetrics internal constructor(
39         private val registry: PrometheusMeterRegistry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT)
40 ) : Metrics {
41
42     private val receivedBytes = registry.counter(name(DATA, RECEIVED, BYTES))
43     private val receivedMsgCount = registry.counter(name(MESSAGES, RECEIVED, COUNT))
44     private val receivedMsgBytes = registry.counter(name(MESSAGES, RECEIVED, BYTES))
45     private val sentCountTotal = registry.counter(name(MESSAGES, SENT, COUNT))
46
47     init {
48         registry.gauge(name(MESSAGES, PROCESSING, COUNT), this) {
49             (receivedMsgCount.count() - sentCountTotal.count()).coerceAtLeast(0.0)
50         }
51         ClassLoaderMetrics().bindTo(registry)
52         JvmMemoryMetrics().bindTo(registry)
53         JvmGcMetrics().bindTo(registry)
54         ProcessorMetrics().bindTo(registry)
55         JvmThreadMetrics().bindTo(registry)
56     }
57
58     private val sentCount = { topic: String ->
59         registry.counter("hvves.messages.sent.topic.count", "topic", topic)
60     }.memoize<String, Counter>()
61
62     val metricsProvider = MicrometerPrometheusMetricsProvider(registry)
63
64     override fun notifyBytesReceived(size: Int) {
65         receivedBytes.increment(size.toDouble())
66     }
67
68     override fun notifyMessageReceived(size: Int) {
69         receivedMsgCount.increment()
70         receivedMsgBytes.increment(size.toDouble())
71     }
72
73     override fun notifyMessageSent(topic: String) {
74         sentCountTotal.increment()
75         sentCount(topic).increment()
76     }
77
78     companion object {
79         val INSTANCE = MicrometerMetrics()
80         internal const val PREFIX = "hvves"
81         internal const val MESSAGES = "messages"
82         internal const val RECEIVED = "received"
83         internal const val BYTES = "bytes"
84         internal const val COUNT = "count"
85         internal const val DATA = "data"
86         internal const val SENT = "sent"
87         internal const val PROCESSING = "processing"
88         fun name(vararg name: String) = "$PREFIX.${name.joinToString(".")}"
89     }
90 }