showuon commented on code in PR #14832: URL: https://github.com/apache/kafka/pull/14832#discussion_r1414765630
########## core/src/main/scala/kafka/server/KafkaRequestHandler.scala: ########## @@ -283,8 +283,38 @@ class BrokerTopicMetrics(name: Option[String], configOpt: java.util.Optional[Kaf meter() } + case class GaugeWrapper(metricType: String, brokerTopicAggregatedMetric: BrokerTopicAggregatedMetric) { + @volatile private var lazyGauge: Gauge[Long] = _ + private val gaugeLock = new Object + + def gauge(): Gauge[Long] = { + var gauge = lazyGauge + if (gauge == null) { + gaugeLock synchronized { + gauge = lazyGauge + if (gauge == null) { + gauge = metricsGroup.newGauge(metricType, () => brokerTopicAggregatedMetric.value()) + lazyGauge = gauge + } + } + } + gauge + } + + def close(): Unit = gaugeLock synchronized { + if (lazyGauge != null) { + metricsGroup.removeMetric(metricType) + brokerTopicAggregatedMetric.close() + lazyGauge = null + } + } + + gauge() // greedily initialize the general topic metrics Review Comment: nit: I don't think we need comment here. ########## core/src/test/scala/integration/kafka/api/MetricsTest.scala: ########## @@ -320,16 +320,28 @@ class MetricsTest extends IntegrationTestHarness with SaslSetup { assertTrue(metrics.isEmpty, s"$errorMessage: ${metrics.keys}") } + private def fromNameToBrokerTopicStatsMBean(name: String): String = { + s"kafka.server:type=BrokerTopicMetrics,name=$name" + } + private def verifyRemoteStorageMetrics(shouldContainMetrics: Boolean): Unit = { val metrics = RemoteStorageMetrics.allMetrics().asScala.filter(name => KafkaYammerMetrics.defaultRegistry.allMetrics.asScala.find(metric => { metric._1.getMBeanName().equals(name.getMBeanName) }).isDefined ).toList + val aggregatedBrokerTopicStats = Set(BrokerTopicStats.RemoteCopyLagBytes) Review Comment: Why using set here? I guess we plan to add more metrics in this set in the future, is that right? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org