288145aab5d724d620daa9e8d7044ce14eac7f4b
[dcaegen2/collectors/hv-ves.git] /
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.domain.WireFrameMessage
33 import org.onap.dcae.collectors.veshv.model.ClientRejectionCause
34 import org.onap.dcae.collectors.veshv.model.MessageDropCause
35 import org.onap.dcae.collectors.veshv.model.RoutedMessage
36 import org.onap.dcae.collectors.veshv.utils.TimeUtils.epochMicroToInstant
37 import java.time.Duration
38 import java.time.Instant
39
40
41 /**
42  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
43  * @since June 2018
44  */
45 class MicrometerMetrics internal constructor(
46         private val registry: PrometheusMeterRegistry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT)
47 ) : Metrics {
48
49     private val receivedBytes = registry.counter(name(DATA, RECEIVED, BYTES))
50     private val receivedMsgCount = registry.counter(name(MESSAGES, RECEIVED, COUNT))
51     private val receivedMsgBytes = registry.counter(name(MESSAGES, RECEIVED, BYTES))
52
53     private val connectionsTotalCount = registry.counter(name(CONNECTIONS, TOTAL, COUNT))
54     private val disconnectionsCount = registry.counter(name(DISCONNECTIONS, COUNT))
55
56     private val processingTime = registry.timer(name(MESSAGES, PROCESSING, TIME))
57     private val totalLatency = registry.timer(name(MESSAGES, LATENCY, TIME))
58
59     private val sentCount = registry.counter(name(MESSAGES, SENT, COUNT))
60     private val sentToTopicCount = { topic: String ->
61         registry.counter(name(MESSAGES, SENT, TOPIC, COUNT), TOPIC, topic)
62     }.memoize<String, Counter>()
63
64     private val droppedCount = registry.counter(name(MESSAGES, DROPPED, COUNT))
65     private val droppedCauseCount = { cause: String ->
66         registry.counter(name(MESSAGES, DROPPED, CAUSE, COUNT), CAUSE, cause)
67     }.memoize<String, Counter>()
68
69     private val clientsRejectedCount = registry.counter(name(CLIENTS, REJECTED, COUNT))
70     private val clientsRejectedCauseCount = { cause: String ->
71         registry.counter(name(CLIENTS, REJECTED, CAUSE, COUNT), CAUSE, cause)
72     }.memoize<String, Counter>()
73
74     init {
75         registry.gauge(name(MESSAGES, PROCESSING, COUNT), this) {
76             (receivedMsgCount.count() - sentCount.count()).coerceAtLeast(0.0)
77         }
78
79         registry.gauge(name(CONNECTIONS, ACTIVE, COUNT), this) {
80             (connectionsTotalCount.count() - disconnectionsCount.count()).coerceAtLeast(0.0)
81         }
82
83         ClassLoaderMetrics().bindTo(registry)
84         JvmMemoryMetrics().bindTo(registry)
85         JvmGcMetrics().bindTo(registry)
86         ProcessorMetrics().bindTo(registry)
87         JvmThreadMetrics().bindTo(registry)
88     }
89
90     val metricsProvider = MicrometerPrometheusMetricsProvider(registry)
91
92     override fun notifyBytesReceived(size: Int) {
93         receivedBytes.increment(size.toDouble())
94     }
95
96     override fun notifyMessageReceived(msg: WireFrameMessage) {
97         receivedMsgCount.increment()
98         receivedMsgBytes.increment(msg.payloadSize.toDouble())
99     }
100
101     override fun notifyMessageSent(msg: RoutedMessage) {
102         val now = Instant.now()
103         sentCount.increment()
104         sentToTopicCount(msg.topic).increment()
105
106         processingTime.record(Duration.between(msg.message.wtpFrame.receivedAt, now))
107         totalLatency.record(Duration.between(epochMicroToInstant(msg.message.header.lastEpochMicrosec), now))
108     }
109
110     override fun notifyMessageDropped(cause: MessageDropCause) {
111         droppedCount.increment()
112         droppedCauseCount(cause.tag).increment()
113     }
114
115     override fun notifyClientRejected(cause: ClientRejectionCause) {
116         clientsRejectedCount.increment()
117         clientsRejectedCauseCount(cause.tag).increment()
118     }
119
120     override fun notifyClientConnected() {
121         connectionsTotalCount.increment()
122     }
123
124     override fun notifyClientDisconnected() {
125         disconnectionsCount.increment()
126     }
127
128     companion object {
129         val INSTANCE = MicrometerMetrics()
130         internal const val PREFIX = "hvves"
131         internal const val MESSAGES = "messages"
132         internal const val RECEIVED = "received"
133         internal const val DISCONNECTIONS = "disconnections"
134         internal const val CONNECTIONS = "connections"
135         internal const val ACTIVE = "active"
136         internal const val BYTES = "bytes"
137         internal const val COUNT = "count"
138         internal const val DATA = "data"
139         internal const val SENT = "sent"
140         internal const val PROCESSING = "processing"
141         internal const val CAUSE = "cause"
142         internal const val CLIENTS = "clients"
143         internal const val REJECTED = "rejected"
144         internal const val TOPIC = "topic"
145         internal const val DROPPED = "dropped"
146         internal const val TOTAL = "total"
147         internal const val TIME = "time"
148         internal const val LATENCY = "latency"
149         internal fun name(vararg name: String) = "$PREFIX.${name.joinToString(".")}"
150     }
151 }