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.domain.WireFrameMessage
33 import org.onap.dcae.collectors.veshv.model.ClientRejectionCause
34 import org.onap.dcae.collectors.veshv.model.MessageDropCause
35 import org.onap.dcae.collectors.veshv.model.RoutedMessage
36 import org.onap.dcae.collectors.veshv.utils.TimeUtils.epochMicroToInstant
37 import java.time.Duration
38 import java.time.Instant
42 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
45 class MicrometerMetrics internal constructor(
46 private val registry: PrometheusMeterRegistry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT)
49 private val receivedBytes = registry.counter(name(DATA, RECEIVED, BYTES))
50 private val receivedMsgCount = registry.counter(name(MESSAGES, RECEIVED, COUNT))
51 private val receivedMsgBytes = registry.counter(name(MESSAGES, RECEIVED, BYTES))
53 private val processingTime = registry.timer(name(MESSAGES, PROCESSING, TIME))
54 private val totalLatency = registry.timer(name(MESSAGES, LATENCY, TIME))
56 private val sentCountTotal = registry.counter(name(MESSAGES, SENT, COUNT))
57 private val sentToTopicCount = { topic: String ->
58 registry.counter(name(MESSAGES, SENT, TOPIC, COUNT), TOPIC, topic)
59 }.memoize<String, Counter>()
61 private val droppedCount = registry.counter(name(MESSAGES, DROPPED, COUNT))
62 private val droppedCauseCount = { cause: String ->
63 registry.counter(name(MESSAGES, DROPPED, CAUSE, COUNT), CAUSE, cause)
64 }.memoize<String, Counter>()
66 private val clientsRejectedCount = registry.counter(name(CLIENTS, REJECTED, COUNT))
67 private val clientsRejectedCauseCount = { cause: String ->
68 registry.counter(name(CLIENTS, REJECTED, CAUSE, COUNT), CAUSE, cause)
69 }.memoize<String, Counter>()
72 registry.gauge(name(MESSAGES, PROCESSING, COUNT), this) {
73 (receivedMsgCount.count() - sentCountTotal.count()).coerceAtLeast(0.0)
75 ClassLoaderMetrics().bindTo(registry)
76 JvmMemoryMetrics().bindTo(registry)
77 JvmGcMetrics().bindTo(registry)
78 ProcessorMetrics().bindTo(registry)
79 JvmThreadMetrics().bindTo(registry)
83 val metricsProvider = MicrometerPrometheusMetricsProvider(registry)
85 override fun notifyBytesReceived(size: Int) {
86 receivedBytes.increment(size.toDouble())
89 override fun notifyMessageReceived(msg: WireFrameMessage) {
90 receivedMsgCount.increment()
91 receivedMsgBytes.increment(msg.payloadSize.toDouble())
94 override fun notifyMessageSent(msg: RoutedMessage) {
95 val now = Instant.now()
96 sentCountTotal.increment()
97 sentToTopicCount(msg.topic).increment()
99 processingTime.record(Duration.between(msg.message.wtpFrame.receivedAt, now))
100 totalLatency.record(Duration.between(epochMicroToInstant(msg.message.header.lastEpochMicrosec), now))
103 override fun notifyMessageDropped(cause: MessageDropCause) {
104 droppedCount.increment()
105 droppedCauseCount(cause.tag).increment()
108 override fun notifyClientRejected(cause: ClientRejectionCause) {
109 clientsRejectedCount.increment()
110 clientsRejectedCauseCount(cause.tag).increment()
114 val INSTANCE = MicrometerMetrics()
115 internal const val PREFIX = "hvves"
116 internal const val MESSAGES = "messages"
117 internal const val RECEIVED = "received"
118 internal const val BYTES = "bytes"
119 internal const val COUNT = "count"
120 internal const val DATA = "data"
121 internal const val SENT = "sent"
122 internal const val PROCESSING = "processing"
123 internal const val CAUSE = "cause"
124 internal const val CLIENTS = "clients"
125 internal const val REJECTED = "rejected"
126 internal const val TOPIC = "topic"
127 internal const val DROPPED = "dropped"
128 internal const val TIME = "time"
129 internal const val LATENCY = "latency"
130 internal fun name(vararg name: String) = "$PREFIX.${name.joinToString(".")}"