Add metrics for dropped messages
[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 import org.onap.dcae.collectors.veshv.model.MessageDropCause
33
34
35 /**
36  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
37  * @since June 2018
38  */
39 class MicrometerMetrics internal constructor(
40         private val registry: PrometheusMeterRegistry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT)
41 ) : Metrics {
42
43     private val receivedBytes = registry.counter(name(DATA, RECEIVED, BYTES))
44     private val receivedMsgCount = registry.counter(name(MESSAGES, RECEIVED, COUNT))
45     private val receivedMsgBytes = registry.counter(name(MESSAGES, RECEIVED, BYTES))
46     private val sentCountTotal = registry.counter(name(MESSAGES, SENT, COUNT, TOTAL))
47     private val droppedCountTotal = registry.counter(name(MESSAGES, DROPPED, COUNT, TOTAL))
48
49     private val sentCount = { topic: String ->
50         registry.counter(name(MESSAGES, SENT, COUNT, TOPIC), TOPIC, topic)
51     }.memoize<String, Counter>()
52
53     private val droppedCount = { cause: String ->
54         registry.counter(name(MESSAGES, DROPPED, COUNT, CAUSE), CAUSE, cause)
55     }.memoize<String, Counter>()
56
57     init {
58         registry.gauge(name(MESSAGES, PROCESSING, COUNT), this) {
59             (receivedMsgCount.count() - sentCountTotal.count()).coerceAtLeast(0.0)
60         }
61         ClassLoaderMetrics().bindTo(registry)
62         JvmMemoryMetrics().bindTo(registry)
63         JvmGcMetrics().bindTo(registry)
64         ProcessorMetrics().bindTo(registry)
65         JvmThreadMetrics().bindTo(registry)
66     }
67
68     val metricsProvider = MicrometerPrometheusMetricsProvider(registry)
69
70     override fun notifyBytesReceived(size: Int) {
71         receivedBytes.increment(size.toDouble())
72     }
73
74     override fun notifyMessageReceived(size: Int) {
75         receivedMsgCount.increment()
76         receivedMsgBytes.increment(size.toDouble())
77     }
78
79     override fun notifyMessageSent(topic: String) {
80         sentCountTotal.increment()
81         sentCount(topic).increment()
82     }
83
84     override fun notifyMessageDropped(cause: MessageDropCause) {
85         droppedCountTotal.increment()
86         droppedCount(cause.tag).increment()
87     }
88
89     companion object {
90         val INSTANCE = MicrometerMetrics()
91         internal const val PREFIX = "hvves"
92         internal const val MESSAGES = "messages"
93         internal const val RECEIVED = "received"
94         internal const val BYTES = "bytes"
95         internal const val COUNT = "count"
96         internal const val DATA = "data"
97         internal const val SENT = "sent"
98         internal const val PROCESSING = "processing"
99         internal const val TOPIC = "topic"
100         internal const val DROPPED = "dropped"
101         internal const val CAUSE = "cause"
102         internal const val TOTAL = "total"
103
104         fun name(vararg name: String) = "$PREFIX.${name.joinToString(".")}"
105     }
106 }