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