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
35 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
38 class MicrometerMetrics internal constructor(
39 private val registry: PrometheusMeterRegistry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT)
42 private val receivedBytes = registry.counter(name(DATA, RECEIVED, BYTES))
43 private val receivedMsgCount = registry.counter(name(MESSAGES, RECEIVED, COUNT))
44 private val receivedMsgBytes = registry.counter(name(MESSAGES, RECEIVED, BYTES))
45 private val sentCountTotal = registry.counter(name(MESSAGES, SENT, COUNT))
48 registry.gauge(name(MESSAGES, PROCESSING, COUNT), this) {
49 (receivedMsgCount.count() - sentCountTotal.count()).coerceAtLeast(0.0)
51 ClassLoaderMetrics().bindTo(registry)
52 JvmMemoryMetrics().bindTo(registry)
53 JvmGcMetrics().bindTo(registry)
54 ProcessorMetrics().bindTo(registry)
55 JvmThreadMetrics().bindTo(registry)
58 private val sentCount = { topic: String ->
59 registry.counter("hvves.messages.sent.topic.count", "topic", topic)
60 }.memoize<String, Counter>()
62 val metricsProvider = MicrometerPrometheusMetricsProvider(registry)
64 override fun notifyBytesReceived(size: Int) {
65 receivedBytes.increment(size.toDouble())
68 override fun notifyMessageReceived(size: Int) {
69 receivedMsgCount.increment()
70 receivedMsgBytes.increment(size.toDouble())
73 override fun notifyMessageSent(topic: String) {
74 sentCountTotal.increment()
75 sentCount(topic).increment()
79 val INSTANCE = MicrometerMetrics()
80 internal const val PREFIX = "hvves"
81 internal const val MESSAGES = "messages"
82 internal const val RECEIVED = "received"
83 internal const val BYTES = "bytes"
84 internal const val COUNT = "count"
85 internal const val DATA = "data"
86 internal const val SENT = "sent"
87 internal const val PROCESSING = "processing"
88 fun name(vararg name: String) = "$PREFIX.${name.joinToString(".")}"