You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ro...@apache.org on 2022/03/25 07:19:14 UTC
[iotdb] branch master updated: [IOTDB-2762][metrics] Fix inconsistent name between dropwizard implementation and micrometer implementation (#5301)
This is an automated email from the ASF dual-hosted git repository.
rong pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/master by this push:
new 8f9d829 [IOTDB-2762][metrics] Fix inconsistent name between dropwizard implementation and micrometer implementation (#5301)
8f9d829 is described below
commit 8f9d829efc70fd91e1c0b1fcc380db4289b09d56
Author: ZhangHongYin <46...@users.noreply.github.com>
AuthorDate: Fri Mar 25 15:18:30 2022 +0800
[IOTDB-2762][metrics] Fix inconsistent name between dropwizard implementation and micrometer implementation (#5301)
---
.../iotdb/metrics/dropwizard/MetricName.java | 27 ++-
.../reporter/DropwizardMetricsExporter.java | 183 +++++++++------------
2 files changed, 98 insertions(+), 112 deletions(-)
diff --git a/metrics/dropwizard-metrics/src/main/java/org/apache/iotdb/metrics/dropwizard/MetricName.java b/metrics/dropwizard-metrics/src/main/java/org/apache/iotdb/metrics/dropwizard/MetricName.java
index 1275fdf..ae136db 100644
--- a/metrics/dropwizard-metrics/src/main/java/org/apache/iotdb/metrics/dropwizard/MetricName.java
+++ b/metrics/dropwizard-metrics/src/main/java/org/apache/iotdb/metrics/dropwizard/MetricName.java
@@ -22,20 +22,22 @@ package org.apache.iotdb.metrics.dropwizard;
import org.apache.iotdb.metrics.utils.MetricLevel;
import java.util.*;
+import java.util.stream.Collectors;
/** the unique identifier of a metric, include a name and some tags. */
public class MetricName {
- public static final String SEPARATOR = ":";
-
private String name;
private MetricLevel metricLevel;
private Map<String, String> tags;
+ private static final String TAG_SEPARATOR = ".";
public MetricName(String name, String... tags) {
this.name = name;
this.tags = new HashMap<>();
- for (int i = 0; i < tags.length; i += 2) {
- this.tags.put(tags[i], tags[i + 1]);
+ if (tags.length % 2 == 0) {
+ for (int i = 0; i < tags.length; i += 2) {
+ this.tags.put(tags[i], tags[i + 1]);
+ }
}
}
/**
@@ -52,14 +54,23 @@ public class MetricName {
}
/**
- * convert the metric name to flat string, like name_tag_key1:tag_value1_tag_key2:tag_value2....
+ * convert the metric name to flat string
*
* @return the flat string
*/
public String toFlatString() {
- StringBuilder stringBuilder = new StringBuilder(name).append("_");
- tags.forEach((k, v) -> stringBuilder.append(k).append(SEPARATOR).append(v).append("_"));
- stringBuilder.deleteCharAt(stringBuilder.length() - 1);
+ StringBuilder stringBuilder = new StringBuilder(name.replaceAll("\\{|\\}", ""));
+ stringBuilder.append("{");
+ stringBuilder.append(
+ tags.entrySet().stream()
+ .map(
+ t ->
+ t.getKey().replace(TAG_SEPARATOR, "")
+ + TAG_SEPARATOR
+ + t.getValue().replace(TAG_SEPARATOR, ""))
+ .collect(Collectors.joining(TAG_SEPARATOR))
+ .replaceAll("\\{|\\}", ""));
+ stringBuilder.append("}");
return stringBuilder.toString();
}
diff --git a/metrics/dropwizard-metrics/src/main/java/org/apache/iotdb/metrics/dropwizard/reporter/DropwizardMetricsExporter.java b/metrics/dropwizard-metrics/src/main/java/org/apache/iotdb/metrics/dropwizard/reporter/DropwizardMetricsExporter.java
index 33f1a58..edaf119 100644
--- a/metrics/dropwizard-metrics/src/main/java/org/apache/iotdb/metrics/dropwizard/reporter/DropwizardMetricsExporter.java
+++ b/metrics/dropwizard-metrics/src/main/java/org/apache/iotdb/metrics/dropwizard/reporter/DropwizardMetricsExporter.java
@@ -19,19 +19,16 @@
package org.apache.iotdb.metrics.dropwizard.reporter;
+import org.apache.iotdb.metrics.dropwizard.MetricName;
+
import com.codahale.metrics.*;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import java.io.IOException;
-import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
class DropwizardMetricsExporter {
- private static final Logger LOGGER = LoggerFactory.getLogger(DropwizardMetricsExporter.class);
-
private final MetricRegistry metricRegistry;
private final PrometheusTextWriter writer;
@@ -58,11 +55,12 @@ class DropwizardMetricsExporter {
}
}
- /** Export Gauge as Prometheus Guage */
- public void writeGauge(String name, Gauge<?> gauge) throws IOException {
- final String sanitizedName = sanitizeMetricName(name);
- writer.writeHelp(sanitizedName, getHelpMessage(name, gauge));
- writer.writeType(sanitizedName, MetricType.GAUGE);
+ /** Export Gauge as Prometheus Gauge */
+ public void writeGauge(String dropwizardName, Gauge<?> gauge) throws IOException {
+ MetricName metricName = getMetricNameFromName(dropwizardName);
+ String sanitizeName = metricName.getName();
+ writer.writeHelp(sanitizeName, getHelpMessage(dropwizardName, gauge));
+ writer.writeType(sanitizeName, MetricType.GAUGE);
Object obj = gauge.getValue();
double value;
@@ -74,136 +72,113 @@ class DropwizardMetricsExporter {
return;
}
- writer.writeSample(sanitizedName, emptyMap(), value);
+ writer.writeSample(sanitizeName, metricName.getTags(), value);
}
- /**
- * Export counter as Prometheus GAUGE
- *
- * @param dropwizardName need to transform
- * @param counter
- * @throws IOException
- */
+ /** Export counter as Prometheus Gauge */
public void writeCounter(String dropwizardName, Counter counter) throws IOException {
- String name = sanitizeMetricName(dropwizardName);
- writer.writeHelp(name, getHelpMessage(dropwizardName, counter));
- writer.writeType(name, MetricType.GAUGE);
- writer.writeSample(name, emptyMap(), counter.getCount());
+ MetricName metricName = getMetricNameFromName(dropwizardName);
+ String sanitizeName = metricName.getName();
+ writer.writeHelp(sanitizeName, getHelpMessage(dropwizardName, counter));
+ writer.writeType(sanitizeName, MetricType.GAUGE);
+ writer.writeSample(sanitizeName, metricName.getTags(), counter.getCount());
}
- /**
- * Export histogram snapshot as Prometheus SUMMARY
- *
- * @param dropwizardName
- * @param histogram
- * @throws IOException
- */
+ /** Export histogram snapshot as Prometheus SUMMARY */
public void writeHistogram(String dropwizardName, Histogram histogram) throws IOException {
writeSnapshotAndCount(
- dropwizardName,
+ getMetricNameFromName(dropwizardName),
histogram.getSnapshot(),
histogram.getCount(),
1.0,
- MetricType.SUMMARY,
getHelpMessage(dropwizardName, histogram));
}
- /**
- * Export histogram snapshot
- *
- * @param dropwizardName
- * @param snapshot
- * @param count
- * @param factor
- * @param type
- * @param helpMessage
- * @throws IOException
- */
+ /** Export histogram snapshot */
private void writeSnapshotAndCount(
- String dropwizardName,
- Snapshot snapshot,
- long count,
- double factor,
- MetricType type,
- String helpMessage)
+ MetricName metricName, Snapshot snapshot, long count, double factor, String helpMessage)
throws IOException {
- String name = sanitizeMetricName(dropwizardName);
- writer.writeHelp(name, helpMessage);
- writer.writeType(name, type);
- writer.writeSample(name, mapOf("quantile", "0.5"), snapshot.getMedian() * factor);
- writer.writeSample(name, mapOf("quantile", "0.75"), snapshot.get75thPercentile() * factor);
- writer.writeSample(name, mapOf("quantile", "0.95"), snapshot.get95thPercentile() * factor);
- writer.writeSample(name, mapOf("quantile", "0.98"), snapshot.get98thPercentile() * factor);
- writer.writeSample(name, mapOf("quantile", "0.99"), snapshot.get99thPercentile() * factor);
- writer.writeSample(name, mapOf("quantile", "0.999"), snapshot.get999thPercentile() * factor);
- writer.writeSample(name + "_min", emptyMap(), snapshot.getMin());
- writer.writeSample(name + "_max", emptyMap(), snapshot.getMax());
- writer.writeSample(name + "_median", emptyMap(), snapshot.getMedian());
- writer.writeSample(name + "_mean", emptyMap(), snapshot.getMean());
- writer.writeSample(name + "_stddev", emptyMap(), snapshot.getStdDev());
- writer.writeSample(name + "_count", emptyMap(), count);
+ String sanitizeName = metricName.getName();
+ writer.writeHelp(sanitizeName, helpMessage);
+ writer.writeType(sanitizeName, MetricType.SUMMARY);
+ Map<String, String> tags = metricName.getTags();
+ writer.writeSample(
+ sanitizeName, addTags(tags, "quantile", "0.5"), snapshot.getMedian() * factor);
+ writer.writeSample(
+ sanitizeName, addTags(tags, "quantile", "0.75"), snapshot.get75thPercentile() * factor);
+ writer.writeSample(
+ sanitizeName, addTags(tags, "quantile", "0.95"), snapshot.get95thPercentile() * factor);
+ writer.writeSample(
+ sanitizeName, addTags(tags, "quantile", "0.98"), snapshot.get98thPercentile() * factor);
+ writer.writeSample(
+ sanitizeName, addTags(tags, "quantile", "0.99"), snapshot.get99thPercentile() * factor);
+ writer.writeSample(
+ sanitizeName, addTags(tags, "quantile", "0.999"), snapshot.get999thPercentile() * factor);
+ writer.writeSample(sanitizeName + "_min", tags, snapshot.getMin());
+ writer.writeSample(sanitizeName + "_max", tags, snapshot.getMax());
+ writer.writeSample(sanitizeName + "_median", tags, snapshot.getMedian());
+ writer.writeSample(sanitizeName + "_mean", tags, snapshot.getMean());
+ writer.writeSample(sanitizeName + "_stddev", tags, snapshot.getStdDev());
+ writer.writeSample(sanitizeName + "_count", tags, count);
}
- /**
- * Export Timer as Prometheus SUMMARY
- *
- * @param dropwizardName
- * @param timer
- * @throws IOException
- */
+ /** Export Timer as Prometheus Summary */
public void writeTimer(String dropwizardName, Timer timer) throws IOException {
writeSnapshotAndCount(
- dropwizardName,
+ getMetricNameFromName(dropwizardName),
timer.getSnapshot(),
timer.getCount(),
1.0D / TimeUnit.SECONDS.toNanos(1L),
- MetricType.SUMMARY,
getHelpMessage(dropwizardName, timer));
- writeMetered(dropwizardName, timer);
+ writeMetered(getMetricNameFromName(dropwizardName), timer);
}
- /**
- * Export Meter as Prometheus Counter
- *
- * @param dropwizardName
- * @param meter
- * @throws IOException
- */
+ /** Export Meter as Prometheus Counter */
public void writeMeter(String dropwizardName, Meter meter) throws IOException {
- String name = sanitizeMetricName(dropwizardName) + "_total";
+ MetricName metricName = getMetricNameFromName(dropwizardName);
+ String sanitizeName = metricName.getName() + "_total";
- writer.writeHelp(name, getHelpMessage(dropwizardName, meter));
- writer.writeType(name, MetricType.COUNTER);
- writer.writeSample(name, emptyMap(), meter.getCount());
+ writer.writeHelp(sanitizeName, getHelpMessage(dropwizardName, meter));
+ writer.writeType(sanitizeName, MetricType.COUNTER);
+ writer.writeSample(sanitizeName, metricName.getTags(), meter.getCount());
+
+ writeMetered(getMetricNameFromName(dropwizardName), meter);
+ }
- writeMetered(dropwizardName, meter);
+ /** Get metric name and tags from name */
+ private MetricName getMetricNameFromName(String dropwizardName) {
+ int firstIndex = dropwizardName.indexOf("{");
+ int lastIndex = dropwizardName.indexOf("}");
+ if (firstIndex == -1 || lastIndex == -1) {
+ String sanitizeMetricName = sanitizeMetricName(dropwizardName);
+ return new MetricName(sanitizeMetricName);
+ } else {
+ String[] labelsFlat = dropwizardName.substring(firstIndex + 1, lastIndex).split("\\.");
+ String sanitizeMetricName = sanitizeMetricName(dropwizardName.substring(0, firstIndex));
+ if (labelsFlat.length == 0) {
+ return new MetricName(sanitizeMetricName);
+ } else {
+ return new MetricName(sanitizeMetricName, labelsFlat);
+ }
+ }
}
- /**
- * Export meter for multi type
- *
- * @param dropwizardName
- * @param metered
- * @throws IOException
- */
- private void writeMetered(String dropwizardName, Metered metered) throws IOException {
- String name = sanitizeMetricName(dropwizardName);
- writer.writeSample(name, mapOf("rate", "m1"), metered.getOneMinuteRate());
- writer.writeSample(name, mapOf("rate", "m5"), metered.getFiveMinuteRate());
- writer.writeSample(name, mapOf("rate", "m15"), metered.getFifteenMinuteRate());
- writer.writeSample(name, mapOf("rate", "mean"), metered.getMeanRate());
+ /** Export meter for multi type */
+ private void writeMetered(MetricName metricName, Metered metered) throws IOException {
+ String sanitizeName = metricName.getName();
+ Map<String, String> tags = metricName.getTags();
+ writer.writeSample(sanitizeName, addTags(tags, "rate", "m1"), metered.getOneMinuteRate());
+ writer.writeSample(sanitizeName, addTags(tags, "rate", "m5"), metered.getFiveMinuteRate());
+ writer.writeSample(sanitizeName, addTags(tags, "rate", "m15"), metered.getFifteenMinuteRate());
+ writer.writeSample(sanitizeName, addTags(tags, "rate", "mean"), metered.getMeanRate());
}
- private Map<String, String> mapOf(String key, String value) {
- HashMap<String, String> result = new HashMap<>();
+ private Map<String, String> addTags(Map<String, String> tags, String key, String value) {
+ HashMap<String, String> result = new HashMap<>(tags);
result.put(key, value);
return result;
}
- private Map<String, String> emptyMap() {
- return Collections.emptyMap();
- }
-
private static String getHelpMessage(String metricName, Metric metric) {
return String.format(
"Generated from Dropwizard metric import (metric=%s, type=%s)",