Skip to content

Commit c6e47b9

Browse files
authored
[IOTDB-2671][metrics] Improve the implementation of iotdbReporter (#5338)
1 parent 4f4b360 commit c6e47b9

File tree

24 files changed

+440
-265
lines changed

24 files changed

+440
-265
lines changed

docs/UserGuide/Maintenance-Tools/Metric-Tool.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,17 @@ metricLevel: IMPORTANT
231231
predefinedMetrics:
232232
- JVM
233233

234-
# Period time of push, only used by IoTDB Reporter
235-
pushPeriodInSecond: 5
236-
237234
# The http server's port for prometheus exporter to get metric data.
238235
prometheusExporterPort: 9091
236+
237+
# The config of iotdb reporter
238+
ioTDBReporterConfig:
239+
host: 127.0.0.1
240+
port: 6667
241+
username: root
242+
password: root
243+
database: _metric
244+
pushPeriodInSecond: 15
239245
```
240246
241247
Then you can get metrics data as follows

docs/zh/UserGuide/Maintenance-Tools/Metric-Tool.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,17 @@ metricLevel: IMPORTANT
232232
predefinedMetrics:
233233
- JVM
234234

235-
# 数据推送时间,该参数只对 IoTDB Reporter 生效
236-
pushPeriodInSecond: 5
237-
238235
# Prometheus Reporter 使用的端口
239-
prometheusExporterPort: 9091
236+
prometheusExporterPort: 9091
237+
238+
# IoTDB Reporter相关的配置
239+
ioTDBReporterConfig:
240+
host: 127.0.0.1
241+
port: 6667
242+
username: root
243+
password: root
244+
database: _metric
245+
pushPeriodInSecond: 15
240246
```
241247
242248
然后按照下面的操作获取metrics数据

metrics/dropwizard-metrics/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
<groupId>org.apache.iotdb</groupId>
3333
<artifactId>metrics-interface</artifactId>
3434
<version>0.14.0-SNAPSHOT</version>
35-
<scope>compile</scope>
3635
</dependency>
3736
<dependency>
3837
<groupId>io.dropwizard.metrics</groupId>

metrics/dropwizard-metrics/src/main/java/org/apache/iotdb/metrics/dropwizard/MetricName.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@
2828
public class MetricName {
2929
private String name;
3030
private MetricLevel metricLevel;
31-
private Map<String, String> tags;
31+
private Map<String, String> tags = new LinkedHashMap<>();
3232
private static final String TAG_SEPARATOR = ".";
3333

3434
public MetricName(String name, String... tags) {
3535
this.name = name;
36-
this.tags = new HashMap<>();
3736
if (tags.length % 2 == 0) {
3837
for (int i = 0; i < tags.length; i += 2) {
3938
this.tags.put(tags[i], tags[i + 1]);
@@ -53,6 +52,30 @@ public MetricName(String name, MetricLevel metricLevel, String... tags) {
5352
this.metricLevel = metricLevel;
5453
}
5554

55+
/** Create metric name from flatString */
56+
public MetricName(String flatString) {
57+
int firstIndex = flatString.indexOf("{");
58+
int lastIndex = flatString.indexOf("}");
59+
if (firstIndex == -1 || lastIndex == -1) {
60+
String sanitizeMetricName = flatString.replaceAll("[^a-zA-Z0-9:_\\]\\[]", "_");
61+
this.name = sanitizeMetricName;
62+
} else {
63+
String[] labelsFlat = flatString.substring(firstIndex + 1, lastIndex).split("\\.");
64+
String sanitizeMetricName =
65+
flatString.substring(0, firstIndex).replaceAll("[^a-zA-Z0-9:_\\]\\[]", "_");
66+
if (labelsFlat.length == 0) {
67+
this.name = sanitizeMetricName;
68+
} else {
69+
this.name = sanitizeMetricName;
70+
if (labelsFlat.length % 2 == 0) {
71+
for (int i = 0; i < labelsFlat.length; i += 2) {
72+
this.tags.put(labelsFlat[i], labelsFlat[i + 1]);
73+
}
74+
}
75+
}
76+
}
77+
}
78+
5679
/**
5780
* convert the metric name to flat string
5881
*
@@ -74,11 +97,7 @@ public String toFlatString() {
7497
return stringBuilder.toString();
7598
}
7699

77-
/**
78-
* convert the metric name to string array.
79-
*
80-
* @return
81-
*/
100+
/** convert the metric name to string array. */
82101
public String[] toStringArray() {
83102
List<String> allNames = new ArrayList<>();
84103
allNames.add(name);

server/src/main/java/org/apache/iotdb/db/metrics/dropwizard/reporter/DropwizardIoTDBReporter.java renamed to metrics/dropwizard-metrics/src/main/java/org/apache/iotdb/metrics/dropwizard/reporter/DropwizardIoTDBReporter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* under the License.
1818
*/
1919

20-
package org.apache.iotdb.db.metrics.dropwizard.reporter;
20+
package org.apache.iotdb.metrics.dropwizard.reporter;
2121

2222
import org.apache.iotdb.metrics.MetricManager;
2323
import org.apache.iotdb.metrics.config.MetricConfig;
@@ -49,7 +49,8 @@ public boolean start() {
4949
.prefixedWith("dropwizard:")
5050
.filter(MetricFilter.ALL)
5151
.build();
52-
ioTDBReporter.start(metricConfig.getPushPeriodInSecond(), TimeUnit.SECONDS);
52+
ioTDBReporter.start(
53+
metricConfig.getIoTDBReporterConfig().getPushPeriodInSecond(), TimeUnit.SECONDS);
5354
return true;
5455
}
5556

metrics/dropwizard-metrics/src/main/java/org/apache/iotdb/metrics/dropwizard/reporter/DropwizardMetricsExporter.java

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void scrape() throws IOException {
5757

5858
/** Export Gauge as Prometheus Gauge */
5959
public void writeGauge(String dropwizardName, Gauge<?> gauge) throws IOException {
60-
MetricName metricName = getMetricNameFromName(dropwizardName);
60+
MetricName metricName = new MetricName(dropwizardName);
6161
String sanitizeName = metricName.getName();
6262
writer.writeHelp(sanitizeName, getHelpMessage(dropwizardName, gauge));
6363
writer.writeType(sanitizeName, MetricType.GAUGE);
@@ -77,7 +77,7 @@ public void writeGauge(String dropwizardName, Gauge<?> gauge) throws IOException
7777

7878
/** Export counter as Prometheus Gauge */
7979
public void writeCounter(String dropwizardName, Counter counter) throws IOException {
80-
MetricName metricName = getMetricNameFromName(dropwizardName);
80+
MetricName metricName = new MetricName(dropwizardName);
8181
String sanitizeName = metricName.getName();
8282
writer.writeHelp(sanitizeName, getHelpMessage(dropwizardName, counter));
8383
writer.writeType(sanitizeName, MetricType.GAUGE);
@@ -87,7 +87,7 @@ public void writeCounter(String dropwizardName, Counter counter) throws IOExcept
8787
/** Export histogram snapshot as Prometheus SUMMARY */
8888
public void writeHistogram(String dropwizardName, Histogram histogram) throws IOException {
8989
writeSnapshotAndCount(
90-
getMetricNameFromName(dropwizardName),
90+
new MetricName(dropwizardName),
9191
histogram.getSnapshot(),
9292
histogram.getCount(),
9393
1.0,
@@ -125,42 +125,24 @@ private void writeSnapshotAndCount(
125125
/** Export Timer as Prometheus Summary */
126126
public void writeTimer(String dropwizardName, Timer timer) throws IOException {
127127
writeSnapshotAndCount(
128-
getMetricNameFromName(dropwizardName),
128+
new MetricName(dropwizardName),
129129
timer.getSnapshot(),
130130
timer.getCount(),
131131
1.0D / TimeUnit.SECONDS.toNanos(1L),
132132
getHelpMessage(dropwizardName, timer));
133-
writeMetered(getMetricNameFromName(dropwizardName), timer);
133+
writeMetered(new MetricName(dropwizardName), timer);
134134
}
135135

136136
/** Export Meter as Prometheus Counter */
137137
public void writeMeter(String dropwizardName, Meter meter) throws IOException {
138-
MetricName metricName = getMetricNameFromName(dropwizardName);
138+
MetricName metricName = new MetricName(dropwizardName);
139139
String sanitizeName = metricName.getName() + "_total";
140140

141141
writer.writeHelp(sanitizeName, getHelpMessage(dropwizardName, meter));
142142
writer.writeType(sanitizeName, MetricType.COUNTER);
143143
writer.writeSample(sanitizeName, metricName.getTags(), meter.getCount());
144144

145-
writeMetered(getMetricNameFromName(dropwizardName), meter);
146-
}
147-
148-
/** Get metric name and tags from name */
149-
private MetricName getMetricNameFromName(String dropwizardName) {
150-
int firstIndex = dropwizardName.indexOf("{");
151-
int lastIndex = dropwizardName.indexOf("}");
152-
if (firstIndex == -1 || lastIndex == -1) {
153-
String sanitizeMetricName = sanitizeMetricName(dropwizardName);
154-
return new MetricName(sanitizeMetricName);
155-
} else {
156-
String[] labelsFlat = dropwizardName.substring(firstIndex + 1, lastIndex).split("\\.");
157-
String sanitizeMetricName = sanitizeMetricName(dropwizardName.substring(0, firstIndex));
158-
if (labelsFlat.length == 0) {
159-
return new MetricName(sanitizeMetricName);
160-
} else {
161-
return new MetricName(sanitizeMetricName, labelsFlat);
162-
}
163-
}
145+
writeMetered(new MetricName(dropwizardName), meter);
164146
}
165147

166148
/** Export meter for multi type */
@@ -184,8 +166,4 @@ private static String getHelpMessage(String metricName, Metric metric) {
184166
"Generated from Dropwizard metric import (metric=%s, type=%s)",
185167
metricName, metric.getClass().getName());
186168
}
187-
188-
static String sanitizeMetricName(String dropwizardName) {
189-
return dropwizardName.replaceAll("[^a-zA-Z0-9:_\\]\\[]", "_");
190-
}
191169
}

0 commit comments

Comments
 (0)