This is an automated email from the ASF dual-hosted git repository.

feiwang pushed a commit to branch branch-1.10
in repository https://gitbox.apache.org/repos/asf/kyuubi.git


The following commit(s) were added to refs/heads/branch-1.10 by this push:
     new 3c34ed70f1 [KYUUBI #6891] Fix get existing gauge issue
3c34ed70f1 is described below

commit 3c34ed70f1b4d621258e5678c0dc026c332bab47
Author: Wang, Fei <[email protected]>
AuthorDate: Thu Jan 23 11:55:37 2025 +0800

    [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
    
    18be2a521 [Wang, Fei] o(1)
    039e7b5eb [Wang, Fei] check existing gauge
    32dce6fb1 [Wang, Fei] check gauge exists
    
    Authored-by: Wang, Fei <[email protected]>
    Signed-off-by: Wang, Fei <[email protected]>
    (cherry picked from commit e12d1ff881ceb6b549eb5262597eaf6e5747867e)
    Signed-off-by: Wang, Fei <[email protected]>
---
 .../scala/org/apache/kyuubi/metrics/MetricsSystem.scala  |  4 ++--
 .../org/apache/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 3db6daba4b..6df0713bb5 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 bac20181ca..9a7a3ecfb0 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()
+  }
 }

Reply via email to