dongjoon-hyun commented on code in PR #298: URL: https://github.com/apache/spark-kubernetes-operator/pull/298#discussion_r2306049207
########## spark-operator/src/main/java/org/apache/spark/k8s/operator/metrics/PrometheusPullModelHandler.java: ########## @@ -82,4 +107,257 @@ protected List<String> filterNonEmptyRecords(String metricsSnapshot) { } return filteredRecords; } + + protected String formatMetricsSnapshot() { + Map<String, Gauge> gauges = registry.getGauges(); + Map<String, Counter> counters = registry.getCounters(); + Map<String, Histogram> histograms = registry.getHistograms(); + Map<String, Meter> meters = registry.getMeters(); + Map<String, Timer> timers = registry.getTimers(); + + StringBuilder stringBuilder = new StringBuilder(); + + for (Map.Entry<String, Gauge> entry : gauges.entrySet()) { + appendIfNotEmpty(stringBuilder, formatGauge(entry.getKey(), entry.getValue())); + } + + // Counters + for (Map.Entry<String, Counter> entry : counters.entrySet()) { + String name = sanitize(entry.getKey()) + "_total"; + Counter counter = entry.getValue(); + appendIfNotEmpty(stringBuilder, formatCounter(name, counter)); + } + + // Histograms + for (Map.Entry<String, Histogram> entry : histograms.entrySet()) { + appendIfNotEmpty(stringBuilder, formatHistogram(entry.getKey(), entry.getValue())); + } + + // Meters + for (Map.Entry<String, Meter> entry : meters.entrySet()) { + appendIfNotEmpty(stringBuilder, formatMeter(entry.getKey(), entry.getValue())); + } + + // Timers (Meter + Histogram in nanoseconds) + for (Map.Entry<String, Timer> entry : timers.entrySet()) { + appendIfNotEmpty(stringBuilder, formatTimer(entry.getKey(), entry.getValue())); + } + return stringBuilder.toString(); + } + + protected void appendIfNotEmpty(StringBuilder stringBuilder, String value) { + if (StringUtils.isNotEmpty(value)) { + stringBuilder.append(value); + } + } + + protected String formatGauge(String name, Gauge gauge) { + if (gauge != null + && gauge.getValue() != null + && !EMPTY_RECORD_VALUE.equals(gauge.getValue()) + && gauge.getValue() instanceof Number) { + String formattedName = sanitize(name); + return "# HELP " + + formattedName + + " Gauge metric\n" + + "# TYPE " + + formattedName + + " gauge\n" + + formattedName + + ' ' + + gauge.getValue() + + "\n\n"; + } + return null; + } + + protected String formatCounter(String name, Counter counter) { + if (counter != null) { + String formattedName = sanitize(name); + return "# HELP " + + formattedName + + " Counter metric\n" + + "# TYPE " + + formattedName + + " counter\n" + + formattedName + + " " + + counter.getCount() + + "\n\n"; + } + return null; + } + + protected String formatHistogram(String name, Histogram histogram) { + if (histogram != null && histogram.getSnapshot() != null) { + StringBuilder stringBuilder = new StringBuilder(300); + String baseName = sanitize(name); + Snapshot snap = histogram.getSnapshot(); + long count = histogram.getCount(); + stringBuilder + .append("# HELP ") + .append(baseName) + .append(" Histogram metric\n# TYPE ") + .append(baseName) + .append(" histogram\n"); Review Comment: Please use string concatenation like line 177 to 186. FYI, Java 9+ improved string contentenation via JEP 280: Indify String Concatenation (https://openjdk.org/jeps/280). For the technical details, please see SPARK-52880 , @jiangzho . - https://github.com/apache/spark/pull/51572 -- 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: reviews-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org