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