Skip to content

Commit

Permalink
[KYUUBI #6891] Fix get existing gauge issue
Browse files Browse the repository at this point in the history
### Why are the changes needed?

For the `com.codahale.metrics.MetricRegistry::gauge`.
It `getOrAdd` the gauge with name.
```
    public <T extends Gauge> T gauge(String name) {
        return (Gauge)this.getOrAdd(name, MetricRegistry.MetricBuilder.GAUGES);
    }
```

So we have to get all the gauges to check whether the gauge exists.

### How was this patch tested?

UT.
### Was this patch authored or co-authored using generative AI tooling?

No.

Closes #6891 from turboFei/gauge_exists.

Closes #6891

18be2a5 [Wang, Fei] o(1)
039e7b5 [Wang, Fei] check existing gauge
32dce6f [Wang, Fei] check gauge exists

Authored-by: Wang, Fei <[email protected]>
Signed-off-by: Wang, Fei <[email protected]>
(cherry picked from commit e12d1ff)
Signed-off-by: Wang, Fei <[email protected]>
  • Loading branch information
turboFei committed Jan 23, 2025
1 parent 803c8e1 commit 3c34ed7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class MetricsSystem extends CompositeService("MetricsSystem") {
meter.mark(value)
}

def getGauge[T](name: String): Option[Gauge[T]] = {
Option(registry.gauge(name))
def getGauge(name: String): Option[Gauge[_]] = {
Option(registry.getGauges().get(name))
}

def registerGauge[T](name: String, value: => T, default: T): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,20 @@ class MetricsSystemSuite extends KyuubiFunSuite {
checkJsonFileMetrics(reportFile, "20181117")
metricsSystem.stop()
}

test("metrics - get gauge") {
val conf = KyuubiConf().set(MetricsConf.METRICS_ENABLED, true)
val metricsSystem = new MetricsSystem()
metricsSystem.initialize(conf)
metricsSystem.start()

assert(metricsSystem.getGauge(MetricsConstants.THRIFT_SSL_CERT_EXPIRATION).isEmpty)
metricsSystem.registerGauge(
MetricsConstants.THRIFT_SSL_CERT_EXPIRATION,
1000,
0)
assert(metricsSystem.getGauge(MetricsConstants.THRIFT_SSL_CERT_EXPIRATION).get.getValue == 1000)

metricsSystem.stop()
}
}

0 comments on commit 3c34ed7

Please sign in to comment.