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 receivedMessages = registry.counter(name(MESSAGES, RECEIVED))
51 private val receivedMessagesPayloadBytes = registry.counter(name(MESSAGES, RECEIVED, PAYLOAD, BYTES))
53 private val totalConnections = registry.counter(name(CONNECTIONS))
54 private val disconnections = registry.counter(name(DISCONNECTIONS))
56 private val processingTime = registry.timer(name(MESSAGES, PROCESSING, TIME))
57 private val totalLatency = registry.timer(name(MESSAGES, LATENCY))
59 private val sentMessages = registry.counter(name(MESSAGES, SENT))
60 private val sentMessagesByTopic = { topic: String ->
61 registry.counter(name(MESSAGES, SENT, TOPIC), TOPIC, topic)
62 }.memoize<String, Counter>()
64 private val droppedMessages = registry.counter(name(MESSAGES, DROPPED))
65 private val messagesDroppedByCause = { cause: String ->
66 registry.counter(name(MESSAGES, DROPPED, CAUSE), CAUSE, cause)
67 }.memoize<String, Counter>()
69 private val clientsRejected = registry.counter(name(CLIENTS, REJECTED))
70 private val clientsRejectedByCause = { cause: String ->
71 registry.counter(name(CLIENTS, REJECTED, CAUSE), CAUSE, cause)
72 }.memoize<String, Counter>()
76 registry.gauge(name(CONNECTIONS, ACTIVE), this) {
77 (totalConnections.count() - disconnections.count()).coerceAtLeast(0.0)
80 ClassLoaderMetrics().bindTo(registry)
81 JvmMemoryMetrics().bindTo(registry)
82 JvmGcMetrics().bindTo(registry)
83 ProcessorMetrics().bindTo(registry)
84 JvmThreadMetrics().bindTo(registry)
87 val metricsProvider = MicrometerPrometheusMetricsProvider(registry)
89 override fun notifyBytesReceived(size: Int) {
90 receivedBytes.increment(size.toDouble())
93 override fun notifyMessageReceived(msg: WireFrameMessage) {
94 receivedMessages.increment()
95 receivedMessagesPayloadBytes.increment(msg.payloadSize.toDouble())
98 override fun notifyMessageSent(msg: RoutedMessage) {
99 val now = Instant.now()
100 sentMessages.increment()
101 sentMessagesByTopic(msg.topic).increment()
103 processingTime.record(Duration.between(msg.message.wtpFrame.receivedAt, now))
104 totalLatency.record(Duration.between(epochMicroToInstant(msg.message.header.lastEpochMicrosec), now))
107 override fun notifyMessageDropped(cause: MessageDropCause) {
108 droppedMessages.increment()
109 messagesDroppedByCause(cause.tag).increment()
112 override fun notifyClientRejected(cause: ClientRejectionCause) {
113 clientsRejected.increment()
114 clientsRejectedByCause(cause.tag).increment()
117 override fun notifyClientConnected() {
118 totalConnections.increment()
121 override fun notifyClientDisconnected() {
122 disconnections.increment()
126 val INSTANCE = MicrometerMetrics()
127 internal const val PREFIX = "hvves"
128 internal const val MESSAGES = "messages"
129 internal const val RECEIVED = "received"
130 internal const val DISCONNECTIONS = "disconnections"
131 internal const val CONNECTIONS = "connections"
132 internal const val ACTIVE = "active"
133 internal const val BYTES = "bytes"
134 internal const val DATA = "data"
135 internal const val SENT = "sent"
136 internal const val PROCESSING = "processing"
137 internal const val CAUSE = "cause"
138 internal const val CLIENTS = "clients"
139 internal const val REJECTED = "rejected"
140 internal const val TOPIC = "topic"
141 internal const val DROPPED = "dropped"
142 internal const val TIME = "time"
143 internal const val LATENCY = "latency"
144 internal const val PAYLOAD = "payload"
145 internal fun name(vararg name: String) = "$PREFIX.${name.joinToString(".")}"