Fixed the behaviour of the noop metrics gauge (#18252)

Before this change, the noop metrics gauge just ignored any value update. Now, it's backed by an atomic ref.
This commit is contained in:
Ratko Veprek 2024-01-23 19:27:42 +01:00 committed by GitHub
parent c62c9d4eea
commit 932a3ac226
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,6 +5,7 @@ package com.daml.metrics.api.noop
import java.time.Duration
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicReference
import com.daml.metrics.api.MetricHandle.Timer.TimerHandle
import com.daml.metrics.api.MetricHandle.{Counter, Gauge, Histogram, Meter, Timer}
@ -31,11 +32,15 @@ case object NoOpTimerHandle extends TimerHandle {
case class NoOpGauge[T](name: String, value: T) extends Gauge[T] {
override def updateValue(newValue: T): Unit = ()
private val ref = new AtomicReference[T](value)
override def getValue: T = value
override def updateValue(newValue: T): Unit = ref.set(newValue)
override def updateValue(f: T => T): Unit = ()
override def getValue: T = ref.get()
override def updateValue(f: T => T): Unit = {
val _ = ref.updateAndGet(f(_))
}
override def close(): Unit = ()
}