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