Skip to content

Commit e12d1ff

Browse files
committed
[KYUUBI #6891] Fix get existing gauge issue
### 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]>
1 parent b0f3c00 commit e12d1ff

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

kyuubi-metrics/src/main/scala/org/apache/kyuubi/metrics/MetricsSystem.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class MetricsSystem extends CompositeService("MetricsSystem") {
5858
meter.mark(value)
5959
}
6060

61-
def getGauge[T](name: String): Option[Gauge[T]] = {
62-
Option(registry.gauge(name))
61+
def getGauge(name: String): Option[Gauge[_]] = {
62+
Option(registry.getGauges().get(name))
6363
}
6464

6565
def registerGauge[T](name: String, value: => T, default: T): Unit = {

kyuubi-metrics/src/test/scala/org/apache/kyuubi/metrics/MetricsSystemSuite.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,20 @@ class MetricsSystemSuite extends KyuubiFunSuite {
9494
checkJsonFileMetrics(reportFile, "20181117")
9595
metricsSystem.stop()
9696
}
97+
98+
test("metrics - get gauge") {
99+
val conf = KyuubiConf().set(MetricsConf.METRICS_ENABLED, true)
100+
val metricsSystem = new MetricsSystem()
101+
metricsSystem.initialize(conf)
102+
metricsSystem.start()
103+
104+
assert(metricsSystem.getGauge(MetricsConstants.THRIFT_SSL_CERT_EXPIRATION).isEmpty)
105+
metricsSystem.registerGauge(
106+
MetricsConstants.THRIFT_SSL_CERT_EXPIRATION,
107+
1000,
108+
0)
109+
assert(metricsSystem.getGauge(MetricsConstants.THRIFT_SSL_CERT_EXPIRATION).get.getValue == 1000)
110+
111+
metricsSystem.stop()
112+
}
97113
}

0 commit comments

Comments
 (0)