fa52ac2c2542db66d72bbe4107bd1e667e5d3d95
[dcaegen2/collectors/hv-ves.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2018-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
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.Timer
25 import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics
26 import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics
27 import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics
28 import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics
29 import io.micrometer.core.instrument.binder.system.ProcessorMetrics
30 import io.micrometer.prometheus.PrometheusConfig
31 import io.micrometer.prometheus.PrometheusMeterRegistry
32 import org.onap.dcae.collectors.veshv.boundary.Metrics
33 import org.onap.dcae.collectors.veshv.domain.WireFrameMessage
34 import org.onap.dcae.collectors.veshv.model.ClientRejectionCause
35 import org.onap.dcae.collectors.veshv.model.MessageDropCause
36 import org.onap.dcae.collectors.veshv.domain.RoutedMessage
37 import org.onap.dcae.collectors.veshv.utils.TimeUtils.epochMicroToInstant
38 import java.time.Duration
39 import java.time.Instant
40
41
42 /**
43  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
44  * @since June 2018
45  */
46 class MicrometerMetrics internal constructor(
47         private val registry: PrometheusMeterRegistry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT)
48 ) : Metrics {
49
50     private val receivedBytes = registry.counter(name(DATA, RECEIVED, BYTES))
51     private val receivedMessages = registry.counter(name(MESSAGES, RECEIVED))
52     private val receivedMessagesPayloadBytes = registry.counter(name(MESSAGES, RECEIVED, PAYLOAD, BYTES))
53
54     private val totalConnections = registry.counter(name(CONNECTIONS))
55     private val disconnections = registry.counter(name(DISCONNECTIONS))
56
57     private val processingTime = Timer.builder(name(MESSAGES, PROCESSING, TIME))
58             .maximumExpectedValue(MAX_BUCKET_DURATION)
59             .publishPercentileHistogram(true)
60             .register(registry)
61     private val totalLatency = Timer.builder(name(MESSAGES, LATENCY))
62             .maximumExpectedValue(MAX_BUCKET_DURATION)
63             .publishPercentileHistogram(true)
64             .register(registry)
65
66     private val sentMessages = registry.counter(name(MESSAGES, SENT))
67     private val sentMessagesByTopic = { topic: String ->
68         registry.counter(name(MESSAGES, SENT, TOPIC), TOPIC, topic)
69     }.memoize<String, Counter>()
70
71     private val droppedMessages = registry.counter(name(MESSAGES, DROPPED))
72     private val messagesDroppedByCause = { cause: String ->
73         registry.counter(name(MESSAGES, DROPPED, CAUSE), CAUSE, cause)
74     }.memoize<String, Counter>()
75
76     private val clientsRejected = registry.counter(name(CLIENTS, REJECTED))
77     private val clientsRejectedByCause = { cause: String ->
78         registry.counter(name(CLIENTS, REJECTED, CAUSE), CAUSE, cause)
79     }.memoize<String, Counter>()
80
81     init {
82
83         registry.gauge(name(CONNECTIONS, ACTIVE), this) {
84             (totalConnections.count() - disconnections.count()).coerceAtLeast(0.0)
85         }
86
87         ClassLoaderMetrics().bindTo(registry)
88         JvmMemoryMetrics().bindTo(registry)
89         JvmGcMetrics().bindTo(registry)
90         ProcessorMetrics().bindTo(registry)
91         JvmThreadMetrics().bindTo(registry)
92     }
93
94     val metricsProvider = MicrometerPrometheusMetricsProvider(registry)
95
96     override fun notifyBytesReceived(size: Int) {
97         receivedBytes.increment(size.toDouble())
98     }
99
100     override fun notifyMessageReceived(msg: WireFrameMessage) {
101         receivedMessages.increment()
102         receivedMessagesPayloadBytes.increment(msg.payloadSize.toDouble())
103     }
104
105     override fun notifyMessageSent(msg: RoutedMessage) {
106         val now = Instant.now()
107         sentMessages.increment()
108         sentMessagesByTopic(msg.targetTopic).increment()
109
110         processingTime.record(Duration.between(msg.message.wtpFrame.receivedAt, now))
111         totalLatency.record(Duration.between(epochMicroToInstant(msg.message.header.lastEpochMicrosec), now))
112     }
113
114     override fun notifyMessageDropped(cause: MessageDropCause) {
115         droppedMessages.increment()
116         messagesDroppedByCause(cause.tag).increment()
117     }
118
119     override fun notifyClientRejected(cause: ClientRejectionCause) {
120         clientsRejected.increment()
121         clientsRejectedByCause(cause.tag).increment()
122     }
123
124     override fun notifyClientConnected() {
125         totalConnections.increment()
126     }
127
128     override fun notifyClientDisconnected() {
129         disconnections.increment()
130     }
131
132     companion object {
133         val INSTANCE = MicrometerMetrics()
134         internal const val PREFIX = "hvves"
135         internal const val MESSAGES = "messages"
136         internal const val RECEIVED = "received"
137         internal const val DISCONNECTIONS = "disconnections"
138         internal const val CONNECTIONS = "connections"
139         internal const val ACTIVE = "active"
140         internal const val BYTES = "bytes"
141         internal const val DATA = "data"
142         internal const val SENT = "sent"
143         internal const val PROCESSING = "processing"
144         internal const val CAUSE = "cause"
145         internal const val CLIENTS = "clients"
146         internal const val REJECTED = "rejected"
147         internal const val TOPIC = "topic"
148         internal const val DROPPED = "dropped"
149         internal const val TIME = "time"
150         internal const val LATENCY = "latency"
151         internal const val PAYLOAD = "payload"
152         internal val MAX_BUCKET_DURATION = Duration.ofSeconds(300L)
153         internal fun name(vararg name: String) = "$PREFIX.${name.joinToString(".")}"
154     }
155 }