utafrali commented on code in PR #3852:
URL: https://github.com/apache/celeborn/pull/3852#discussion_r4033459834


##########
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

Review Comment:
   The regex is compiled fresh on every call to `getMetricsSnapshot`. Under 
high scrape frequency this adds unnecessary overhead. Move it to the companion 
object:
   
   ```scala
   object PrometheusServlet {
     private val headerLine = "^#\\s+(TYPE|HELP)\\s+(\\S+)\\b.*$".r
   }
   ```
   
   Then reference it as `PrometheusServlet.headerLine` (or just `headerLine`) 
inside the class.



##########
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 = scala.io.Source.fromString(snapshot).getLines().toList
+
+    val counterName = "metrics_Counter_Count"
+    val gaugeName = "metrics_Gauge1_Value"
+
+    assert(lines.count(_ == s"# HELP $counterName") == 1)
+    assert(lines.count(_ == s"# TYPE $counterName counter") == 1)
+    assert(lines.exists(_ == s"# TYPE $counterName counter"))

Review Comment:
   This assertion is redundant. Line 60 already verifies `count == 1`, which 
implies `exists` is true. Remove line 61:
   
   ```scala
   // remove: assert(lines.exists(_ == s"# TYPE $counterName counter"))
   ```



##########
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 = scala.io.Source.fromString(snapshot).getLines().toList
+
+    val counterName = "metrics_Counter_Count"
+    val gaugeName = "metrics_Gauge1_Value"
+
+    assert(lines.count(_ == s"# HELP $counterName") == 1)
+    assert(lines.count(_ == s"# TYPE $counterName counter") == 1)
+    assert(lines.exists(_ == s"# TYPE $counterName counter"))
+    assert(lines.count(_.startsWith(s"$counterName{")) == 2)
+    assert(snapshot.contains("""user="user1""""))
+    assert(snapshot.contains("""user="user2""""))
+    assert(lines.count(_ == s"# HELP $gaugeName") == 1)
+    assert(lines.count(_ == s"# TYPE $gaugeName gauge") == 1)
+    assert(lines.exists(_.startsWith(s"$gaugeName")) && 
snapshot.contains("1000"))

Review Comment:
   The gauge sample assertion is weaker than the counter assertions. The test 
verifies that a line starting with `gaugeName` exists and that `"1000"` appears 
somewhere in the snapshot, but it does not verify there is exactly one sample 
line for the gauge. For consistency with the counter checks above, prefer:
   
   ```scala
   assert(lines.count(_.startsWith(s"$gaugeName")) == 1)
   assert(lines.exists(_.contains("1000")))
   ```



##########
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)
+
+    scala.io.Source.fromString(raw).getLines().foreach {

Review Comment:
   `scala.io.Source.fromString(raw).getLines()` is a roundabout way to iterate 
over the lines of an in-memory string. It wraps a `StringReader` inside a 
`BufferedSource` and is not closed here (no `finally` / `using`). Although a 
`StringReader` won't leak file handles, the pattern is still surprising. Use 
the simpler, no-allocation alternative:
   
   ```scala
   raw.linesIterator.foreach { ... }
   ```
   
   (`linesIterator` is available since Scala 2.13; if the project still targets 
2.12, `raw.split("\n", -1).iterator` is equivalent.)



-- 
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]

Reply via email to