Skip to content

Commit c4a611a

Browse files
committed
feat: add readme for metrics module
1 parent 24564ac commit c4a611a

File tree

6 files changed

+137
-13
lines changed

6 files changed

+137
-13
lines changed

ingester-example/src/main/java/io/greptime/bench/BatchingWriteBenchmark.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.greptime.common.util.MetricsUtil;
2222
import io.greptime.common.util.ServiceLoader;
2323
import io.greptime.common.util.SystemPropertyUtil;
24+
import io.greptime.metrics.ExporterOptions;
2425
import io.greptime.metrics.MetricsExporter;
2526
import io.greptime.models.Err;
2627
import io.greptime.models.Result;
@@ -61,8 +62,8 @@ public static void main(String[] args) throws Exception {
6162
LOG.info("Max points per second: {}", maxPointsPerSecond);
6263

6364
// Start a metrics exporter
64-
MetricsExporter metricsExporter = new MetricsExporter(8080, MetricsUtil.metricRegistry());
65-
metricsExporter.init(null);
65+
MetricsExporter metricsExporter = new MetricsExporter(MetricsUtil.metricRegistry());
66+
metricsExporter.init(ExporterOptions.newDefault());
6667

6768
GreptimeDB greptimeDB = DBConnector.connectTo(new String[] {endpoint}, dbName);
6869

ingester-example/src/main/java/io/greptime/bench/BulkWriteBenchmark.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.greptime.common.util.MetricsUtil;
2323
import io.greptime.common.util.ServiceLoader;
2424
import io.greptime.common.util.SystemPropertyUtil;
25+
import io.greptime.metrics.ExporterOptions;
2526
import io.greptime.metrics.MetricsExporter;
2627
import io.greptime.models.Table;
2728
import io.greptime.models.TableSchema;
@@ -55,8 +56,8 @@ public static void main(String[] args) throws Exception {
5556
LOG.info("Batch size: {}", batchSize);
5657

5758
// Start a metrics exporter
58-
MetricsExporter metricsExporter = new MetricsExporter(8080, MetricsUtil.metricRegistry());
59-
metricsExporter.init(null);
59+
MetricsExporter metricsExporter = new MetricsExporter(MetricsUtil.metricRegistry());
60+
metricsExporter.init(ExporterOptions.newDefault());
6061

6162
GreptimeDB greptimeDB = DBConnector.connectTo(new String[] {endpoint}, dbName);
6263

ingester-example/src/main/java/io/greptime/bench/StreamingWriteBenchmark.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.greptime.common.util.MetricsUtil;
2222
import io.greptime.common.util.ServiceLoader;
2323
import io.greptime.common.util.SystemPropertyUtil;
24+
import io.greptime.metrics.ExporterOptions;
2425
import io.greptime.metrics.MetricsExporter;
2526
import io.greptime.models.Table;
2627
import io.greptime.models.TableSchema;
@@ -58,8 +59,8 @@ public static void main(String[] args) throws Exception {
5859
LOG.info("Max points per second: {}", maxPointsPerSecond);
5960

6061
// Start a metrics exporter
61-
MetricsExporter metricsExporter = new MetricsExporter(8080, MetricsUtil.metricRegistry());
62-
metricsExporter.init(null);
62+
MetricsExporter metricsExporter = new MetricsExporter(MetricsUtil.metricRegistry());
63+
metricsExporter.init(ExporterOptions.newDefault());
6364

6465
GreptimeDB greptimeDB = DBConnector.connectTo(new String[] {endpoint}, dbName);
6566

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Export Metrics
2+
3+
Monitoring metrics is essential for maintaining a healthy ingester deployment. It enables you to:
4+
5+
- Assess the current state of your ingester
6+
- Proactively maintain your deployment
7+
- Quickly diagnose and resolve issues when they arise
8+
9+
For a comprehensive list of available metrics and their descriptions, please refer to our [detailed metrics documentation](../docs/metrics-display.md#list-of-metrics-constantly-updated).
10+
11+
## Start Metrics Exporter
12+
13+
To start an HTTP server that serves Prometheus metrics, initialize the metrics exporter before starting the GreptimeDB ingester:
14+
15+
```java
16+
MetricsExporter metricsExporter = new MetricsExporter(MetricsUtil.metricRegistry());
17+
metricsExporter.init(ExporterOptions.newDefault());
18+
// Start GreptimeDB ingester
19+
// ...
20+
```
21+
22+
## Get Metrics
23+
24+
You can check the output of `curl http://<host>:<port>/metrics` by getting the latest metrics of Ingester.
25+
26+
## Export metrics to Prometheus
27+
28+
Ingester supports exporting metrics to Prometheus. Before configuring export of metrics, you need to setup Prometheus by following their official [documentation](https://prometheus.io/docs/prometheus/latest/installation/).
29+
30+
To scrape metrics from Ingester, write a Prometheus configuration file and save it as `prometheus.yml`:
31+
32+
```yaml
33+
global:
34+
scrape_interval: 15s
35+
36+
scrape_configs:
37+
- job_name: 'greptimedb ingester'
38+
static_configs:
39+
# Assuming that Ingester is running locally.
40+
# The default HTTP port of 8090.
41+
- targets: ['localhost:8090']
42+
```
43+
44+
Start Prometheus using the configuration file:
45+
46+
```yaml
47+
./prometheus --config.file=prometheus.yml
48+
```
49+
50+
## Grafana Dashboard
51+
52+
You can import dashboard via JSON model: [`greptimedb-ingester-dashboard.json`](../grafana/greptimedb-ingester-dashboard.json)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2023 Greptime Team
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.greptime.metrics;
18+
19+
import io.greptime.common.Copiable;
20+
21+
/**
22+
* Exporter options.
23+
*/
24+
public class ExporterOptions implements Copiable<ExporterOptions> {
25+
private int port;
26+
private boolean deamon;
27+
28+
public static ExporterOptions newDefault() {
29+
ExporterOptions opts = new ExporterOptions();
30+
opts.port = 8090;
31+
opts.deamon = true;
32+
return opts;
33+
}
34+
35+
public int getPort() {
36+
return port;
37+
}
38+
39+
public void setPort(int port) {
40+
this.port = port;
41+
}
42+
43+
public boolean isDeamon() {
44+
return deamon;
45+
}
46+
47+
public void setDeamon(boolean deamon) {
48+
this.deamon = deamon;
49+
}
50+
51+
@Override
52+
public ExporterOptions copy() {
53+
ExporterOptions opts = new ExporterOptions();
54+
opts.port = this.port;
55+
opts.deamon = this.deamon;
56+
return opts;
57+
}
58+
59+
@Override
60+
public String toString() {
61+
return "ExporterOptions{" + "port=" + port + ", deamon=" + deamon + '}';
62+
}
63+
}

ingester-prometheus-metrics/src/main/java/io/greptime/metrics/MetricsExporter.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,30 @@
2727
import org.slf4j.Logger;
2828
import org.slf4j.LoggerFactory;
2929

30-
public class MetricsExporter implements Lifecycle<Void> {
30+
public class MetricsExporter implements Lifecycle<ExporterOptions> {
3131

3232
private static final Logger LOG = LoggerFactory.getLogger(MetricsExporter.class);
3333

3434
private final CollectorRegistry prometheusMetricRegistry;
3535

36-
private final int port;
3736
private HTTPServer server;
37+
private ExporterOptions opts;
3838

3939
private final AtomicBoolean started = new AtomicBoolean(false);
4040

41-
public MetricsExporter(int port, MetricRegistry dropwizardMetricRegistry) {
42-
this.port = port;
41+
public MetricsExporter(MetricRegistry dropwizardMetricRegistry) {
4342
this.prometheusMetricRegistry = new CollectorRegistry();
4443
this.prometheusMetricRegistry.register(new DropwizardExports(dropwizardMetricRegistry));
4544
}
4645

4746
@Override
48-
public boolean init(Void opts) {
47+
public boolean init(ExporterOptions opts) {
4948
if (this.started.compareAndSet(false, true)) {
49+
this.opts = opts;
5050
try {
51-
this.server = new HTTPServer(new InetSocketAddress(this.port), this.prometheusMetricRegistry, true);
52-
LOG.info("Metrics exporter started at `http://localhost:{}/metrics`", this.port);
51+
this.server = new HTTPServer(
52+
new InetSocketAddress(opts.getPort()), this.prometheusMetricRegistry, opts.isDeamon());
53+
LOG.info("Metrics exporter started at `http://localhost:{}/metrics`", opts.getPort());
5354
return true;
5455
} catch (IOException e) {
5556
this.started.set(false);
@@ -69,4 +70,9 @@ public void shutdownGracefully() {
6970
}
7071
}
7172
}
73+
74+
@Override
75+
public String toString() {
76+
return "MetricsExporter{" + "opts=" + opts + '}';
77+
}
7278
}

0 commit comments

Comments
 (0)