Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add resetAll() to MetricsContainer for testing. #176

Merged
merged 2 commits into from
Aug 10, 2022
Merged
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 @@ -30,13 +30,15 @@ class BooleanMetric @JvmOverloads constructor(
/** the namespace (prefix) of this metric */
namespace: String,
/** an optional initial value for this metric */
initialValue: Boolean = false
internal val initialValue: Boolean = false
) : Metric<Boolean>() {
private val gauge =
Gauge.build(name, help).namespace(namespace).create().apply { set(if (initialValue) 1.0 else 0.0) }

override fun get() = gauge.get() != 0.0

override fun reset() = set(initialValue)

override fun register(registry: CollectorRegistry) = this.also { registry.register(gauge) }

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,26 @@ class CounterMetric @JvmOverloads constructor(
/** the namespace (prefix) of this metric */
namespace: String,
/** an optional initial value for this metric */
initialValue: Long = 0L
internal val initialValue: Long = 0L
) : Metric<Long>() {
private val counter =
Counter.build(name, help).namespace(namespace).create().apply { inc(initialValue.toDouble()) }

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

override fun reset() {
synchronized(counter) {
counter.apply { clear() }.inc(initialValue.toDouble())
}
}

override fun register(registry: CollectorRegistry) = this.also { registry.register(counter) }

/**
* Atomically adds the given value to this counter.
*/
fun add(delta: Long) = synchronized(counter) { counter.inc(delta.toDouble()) }

/**
* Atomically adds the given value to this counter, returning the updated value.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,21 @@ class DoubleGaugeMetric @JvmOverloads constructor(
/** the namespace (prefix) of this metric */
namespace: String,
/** an optional initial value for this metric */
initialValue: Double = 0.0
internal val initialValue: Double = 0.0
) : Metric<Double>() {
private val gauge = Gauge.build(name, help).namespace(namespace).create().apply { set(initialValue) }

override fun get() = gauge.get()

override fun reset() = set(initialValue)

override fun register(registry: CollectorRegistry) = this.also { registry.register(gauge) }

/**
* Sets the value of this gauge to the given value.
*/
fun set(newValue: Double) = gauge.set(newValue)

/**
* Atomically sets the gauge to the given value, returning the updated value.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ class InfoMetric(
/** the namespace (prefix) of this metric */
namespace: String,
/** the value of this info metric */
private val value: String
internal val value: String
) : Metric<String>() {
private val info = Info.build(name, help).namespace(namespace).create().apply { info(name, value) }

override fun get() = value

override fun reset() = info.info(name, value)

override fun register(registry: CollectorRegistry) = this.also { registry.register(info) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ class LongGaugeMetric @JvmOverloads constructor(
/** the namespace (prefix) of this metric */
namespace: String,
/** an optional initial value for this metric */
initialValue: Long = 0L
internal val initialValue: Long = 0L
) : Metric<Long>() {
private val gauge = Gauge.build(name, help).namespace(namespace).create().apply { set(initialValue.toDouble()) }

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

override fun reset() = set(initialValue)

override fun register(registry: CollectorRegistry) = this.also { registry.register(gauge) }

/**
Expand Down
5 changes: 5 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 @@ -35,6 +35,11 @@ sealed class Metric<T> {
*/
abstract fun get(): T

/**
* Resets the value of this metric to its initial value.
*/
internal abstract fun reset()

/**
* Registers this metric with the given [CollectorRegistry] and returns it.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,11 @@ open class MetricsContainer @JvmOverloads constructor(
}
return InfoMetric(name, help, namespace, value).apply { metrics[name] = register(registry) }
}

/**
* Resets all metrics in this container to their default values.
*/
fun resetAll() {
metrics.values.forEach { it.reset() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ class MetricsContainerTest : ShouldSpec() {
a shouldContainExactly b
}
}
context("and altering their values") {
booleanMetric.set(!booleanMetric.get())
counter.add(5)
longGauge.set(5)
context("then resetting all metrics in the MetricsContainer") {
mc.resetAll()
should("set all metric values to their initial values") {
booleanMetric.get() shouldBe booleanMetric.initialValue
counter.get() shouldBe counter.initialValue
longGauge.get() shouldBe longGauge.initialValue
}
}
}
}
}
}