Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class CounterMetric @JvmOverloads constructor(
internal val initialValue: Long = 0L
) : Metric<Long>() {
private val counter =
Counter.build(name, help).namespace(namespace).create().apply { inc(initialValue.toDouble()) }
Counter.build(name, help).namespace(namespace)
.apply { setUnit() }.create()
.apply { inc(initialValue.toDouble()) }

override fun get() = counter.get().toLong()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class DoubleGaugeMetric @JvmOverloads constructor(
/** an optional initial value for this metric */
internal val initialValue: Double = 0.0
) : Metric<Double>() {
private val gauge = Gauge.build(name, help).namespace(namespace).create().apply { set(initialValue) }
private val gauge = Gauge.build(name, help).namespace(namespace)
.apply { setUnit() }.create()
.apply { set(initialValue) }

override fun get() = gauge.get()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class LongGaugeMetric @JvmOverloads constructor(
/** an optional initial value for this metric */
internal val initialValue: Long = 0L
) : Metric<Long>() {
private val gauge = Gauge.build(name, help).namespace(namespace).create().apply { set(initialValue.toDouble()) }
private val gauge = Gauge.build(name, help).namespace(namespace)
.apply { setUnit() }.create()
.apply { set(initialValue.toDouble()) }

override fun get() = gauge.get().toLong()

Expand Down
15 changes: 15 additions & 0 deletions jicoco-metrics/src/main/kotlin/org/jitsi/metrics/Metric.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.jitsi.metrics

import io.prometheus.client.CollectorRegistry
import io.prometheus.client.SimpleCollector

/**
* `Metric` provides methods common to all Prometheus metric type wrappers.
Expand Down Expand Up @@ -44,4 +45,18 @@ sealed class Metric<T> {
* Registers this metric with the given [CollectorRegistry] and returns it.
*/
internal abstract fun register(registry: CollectorRegistry): Metric<T>

/**
* Sets the OpenMetrics format unit of this metric from its name (suffix delimited by an underscore).
*
* See: [OpenMetrics](https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#unit)
*/
internal fun SimpleCollector.Builder<*, *>.setUnit() {
val suffix = name.substringAfterLast("_", "")
if (UNITS.contains(suffix)) {
unit(suffix)
}
}
}

private val UNITS = setOf("milliseconds", "seconds", "bits", "kilobits", "megabits", "bytes", "kilobytes", "megabytes")