From e12d1ff881ceb6b549eb5262597eaf6e5747867e Mon Sep 17 00:00:00 2001 From: "Wang, Fei" Date: Thu, 23 Jan 2025 11:55:37 +0800 Subject: [PATCH] [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 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 18be2a521 [Wang, Fei] o(1) 039e7b5eb [Wang, Fei] check existing gauge 32dce6fb1 [Wang, Fei] check gauge exists Authored-by: Wang, Fei Signed-off-by: Wang, Fei --- .../apache/kyuubi/metrics/MetricsSystem.scala | 4 ++-- .../kyuubi/metrics/MetricsSystemSuite.scala | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/kyuubi-metrics/src/main/scala/org/apache/kyuubi/metrics/MetricsSystem.scala b/kyuubi-metrics/src/main/scala/org/apache/kyuubi/metrics/MetricsSystem.scala index 3db6daba4b2..6df0713bb5d 100644 --- a/kyuubi-metrics/src/main/scala/org/apache/kyuubi/metrics/MetricsSystem.scala +++ b/kyuubi-metrics/src/main/scala/org/apache/kyuubi/metrics/MetricsSystem.scala @@ -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 = { diff --git a/kyuubi-metrics/src/test/scala/org/apache/kyuubi/metrics/MetricsSystemSuite.scala b/kyuubi-metrics/src/test/scala/org/apache/kyuubi/metrics/MetricsSystemSuite.scala index bac20181ca5..9a7a3ecfb04 100644 --- a/kyuubi-metrics/src/test/scala/org/apache/kyuubi/metrics/MetricsSystemSuite.scala +++ b/kyuubi-metrics/src/test/scala/org/apache/kyuubi/metrics/MetricsSystemSuite.scala @@ -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() + } }