Copilot commented on code in PR #3734:
URL: https://github.com/apache/celeborn/pull/3734#discussion_r3548765640
##########
service/src/main/scala/org/apache/celeborn/common/metrics/sink/PrometheusServlet.scala:
##########
@@ -37,4 +37,23 @@ class PrometheusServlet(
servletPath,
new ServletParams(_ => getMetricsSnapshot, "text/plain"))
}
+
+ override def getMetricsSnapshot: String = {
+ val raw = super.getMetricsSnapshot
+ val seen = scala.collection.mutable.HashSet[String]()
+ val header = "^#\\s+(TYPE|HELP)\\s+(\\S+)\\b.*$".r
+ val out = new StringBuilder(raw.length)
+
+ raw.linesIterator.foreach {
+ case line @ header(kind, metric) =>
+ val key = s"$kind\t$metric"
+ if (!seen.contains(key)) {
+ out.append(line).append('\n')
+ seen += key
+ }
+ case line =>
+ out.append(line).append('\n')
+ }
Review Comment:
`linesIterator` is not available in Scala 2.11 (Celeborn still builds under
the `spark-2.4` profile with Scala 2.11.12), so this will fail compilation
outside the default Scala version. Use a Scala-version-agnostic line iteration
(e.g., `BufferedReader.readLine`) instead.
##########
service/src/test/scala/org/apache/celeborn/common/metrics/sink/PrometheusServletSuite.scala:
##########
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.celeborn.common.metrics.sink
+
+import java.util.Properties
+
+import com.codahale.metrics.MetricRegistry
+
+import org.apache.celeborn.CelebornFunSuite
+import org.apache.celeborn.common.CelebornConf
+import org.apache.celeborn.common.metrics.source.{AbstractSource, Role}
+
+class PrometheusServletSuite extends CelebornFunSuite {
+
+ private def newServlet(source: AbstractSource): PrometheusServlet = {
+ new PrometheusServlet(
+ new Properties(),
+ new MetricRegistry(),
+ Seq(source),
+ "/metrics/prometheus")
+ }
+
+ test("getMetricsSnapshot deduplicates # HELP and # TYPE lines per metric
family") {
+ val conf = new CelebornConf()
+ val source = new AbstractSource(conf, Role.WORKER) {
+ override def sourceName: String = "mockSource"
+ }
+
+ val user1 = Map("user" -> "user1")
+ val user2 = Map("user" -> "user2")
+ source.addCounter("Counter", user1)
+ source.addCounter("Counter", user2)
+ source.incCounter("Counter", 1, user1)
+ source.incCounter("Counter", 2, user2)
+
+ source.addGauge("Gauge1") { () => 1000 }
+
+ val snapshot = newServlet(source).getMetricsSnapshot
+ val lines = snapshot.linesIterator.toList
Review Comment:
`String#linesIterator` is not available in Scala 2.11, which Celeborn still
targets under the `spark-2.4` build profile. Use a Scala-2.11-compatible way to
split the snapshot into lines (e.g.,
`scala.io.Source.fromString(...).getLines()`).
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]