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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dcae.collectors.veshv.main.metrics
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.model.MessageDropCause
36 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
39 class MicrometerMetrics internal constructor(
40 private val registry: PrometheusMeterRegistry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT)
43 private val receivedBytes = registry.counter(name(DATA, RECEIVED, BYTES))
44 private val receivedMsgCount = registry.counter(name(MESSAGES, RECEIVED, COUNT))
45 private val receivedMsgBytes = registry.counter(name(MESSAGES, RECEIVED, BYTES))
46 private val sentCountTotal = registry.counter(name(MESSAGES, SENT, COUNT, TOTAL))
47 private val droppedCountTotal = registry.counter(name(MESSAGES, DROPPED, COUNT, TOTAL))
49 private val sentCount = { topic: String ->
50 registry.counter(name(MESSAGES, SENT, COUNT, TOPIC), TOPIC, topic)
51 }.memoize<String, Counter>()
53 private val droppedCount = { cause: String ->
54 registry.counter(name(MESSAGES, DROPPED, COUNT, CAUSE), CAUSE, cause)
55 }.memoize<String, Counter>()
58 registry.gauge(name(MESSAGES, PROCESSING, COUNT), this) {
59 (receivedMsgCount.count() - sentCountTotal.count()).coerceAtLeast(0.0)
61 ClassLoaderMetrics().bindTo(registry)
62 JvmMemoryMetrics().bindTo(registry)
63 JvmGcMetrics().bindTo(registry)
64 ProcessorMetrics().bindTo(registry)
65 JvmThreadMetrics().bindTo(registry)
68 val metricsProvider = MicrometerPrometheusMetricsProvider(registry)
70 override fun notifyBytesReceived(size: Int) {
71 receivedBytes.increment(size.toDouble())
74 override fun notifyMessageReceived(size: Int) {
75 receivedMsgCount.increment()
76 receivedMsgBytes.increment(size.toDouble())
79 override fun notifyMessageSent(topic: String) {
80 sentCountTotal.increment()
81 sentCount(topic).increment()
84 override fun notifyMessageDropped(cause: MessageDropCause) {
85 droppedCountTotal.increment()
86 droppedCount(cause.tag).increment()
90 val INSTANCE = MicrometerMetrics()
91 internal const val PREFIX = "hvves"
92 internal const val MESSAGES = "messages"
93 internal const val RECEIVED = "received"
94 internal const val BYTES = "bytes"
95 internal const val COUNT = "count"
96 internal const val DATA = "data"
97 internal const val SENT = "sent"
98 internal const val PROCESSING = "processing"
99 internal const val TOPIC = "topic"
100 internal const val DROPPED = "dropped"
101 internal const val CAUSE = "cause"
102 internal const val TOTAL = "total"
104 fun name(vararg name: String) = "$PREFIX.${name.joinToString(".")}"