Skip to content

Commit 3119d89

Browse files
authored
feat: prometheus metrics exporter (#78)
1 parent 7814fa5 commit 3119d89

File tree

7 files changed

+147
-0
lines changed

7 files changed

+147
-0
lines changed

ingester-all/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,9 @@
4747
<groupId>${project.groupId}</groupId>
4848
<artifactId>ingester-bulk-protocol</artifactId>
4949
</dependency>
50+
<dependency>
51+
<groupId>${project.groupId}</groupId>
52+
<artifactId>ingester-prometheus-metrics</artifactId>
53+
</dependency>
5054
</dependencies>
5155
</project>

ingester-example/src/main/java/io/greptime/BulkWriteApiQuickStart.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ public static void main(String[] args) throws Exception {
118118

119119
bulkStreamWriter.completed();
120120
}
121+
122+
greptimeDB.shutdownGracefully();
121123
}
122124

123125
private static Object[] generateOneRow(int cardinality) {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import io.greptime.BulkStreamWriter;
2020
import io.greptime.BulkWrite;
2121
import io.greptime.GreptimeDB;
22+
import io.greptime.common.util.MetricsUtil;
2223
import io.greptime.common.util.ServiceLoader;
2324
import io.greptime.common.util.SystemPropertyUtil;
25+
import io.greptime.metrics.MetricsExporter;
2426
import io.greptime.models.Table;
2527
import io.greptime.models.TableSchema;
2628
import io.greptime.rpc.Compression;
@@ -52,6 +54,10 @@ public static void main(String[] args) throws Exception {
5254
LOG.info("Using zstd compression: {}", zstdCompression);
5355
LOG.info("Batch size: {}", batchSize);
5456

57+
// Start a metrics exporter
58+
MetricsExporter metricsExporter = new MetricsExporter(8080, MetricsUtil.metricRegistry());
59+
metricsExporter.init(null);
60+
5561
GreptimeDB greptimeDB = DBConnector.connectTo(new String[] {endpoint}, dbName);
5662

5763
BulkWrite.Config cfg = BulkWrite.Config.newBuilder()
@@ -111,5 +117,6 @@ public static void main(String[] args) throws Exception {
111117
}
112118

113119
greptimeDB.shutdownGracefully();
120+
metricsExporter.shutdownGracefully();
114121
}
115122
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
import io.greptime.GreptimeDB;
2020
import io.greptime.StreamWriter;
21+
import io.greptime.common.util.MetricsUtil;
2122
import io.greptime.common.util.ServiceLoader;
2223
import io.greptime.common.util.SystemPropertyUtil;
24+
import io.greptime.metrics.MetricsExporter;
2325
import io.greptime.models.Table;
2426
import io.greptime.models.TableSchema;
2527
import io.greptime.models.WriteOk;
@@ -55,6 +57,10 @@ public static void main(String[] args) throws Exception {
5557
LOG.info("Batch size: {}", batchSize);
5658
LOG.info("Max points per second: {}", maxPointsPerSecond);
5759

60+
// Start a metrics exporter
61+
MetricsExporter metricsExporter = new MetricsExporter(8080, MetricsUtil.metricRegistry());
62+
metricsExporter.init(null);
63+
5864
GreptimeDB greptimeDB = DBConnector.connectTo(new String[] {endpoint}, dbName);
5965

6066
Compression compression = zstdCompression ? Compression.Zstd : Compression.None;
@@ -100,5 +106,6 @@ public static void main(String[] args) throws Exception {
100106

101107
greptimeDB.shutdownGracefully();
102108
tableDataProvider.close();
109+
metricsExporter.shutdownGracefully();
103110
}
104111
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2023 Greptime Team
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
<parent>
22+
<groupId>io.greptime</groupId>
23+
<artifactId>greptimedb-ingester</artifactId>
24+
<version>0.14.2</version>
25+
</parent>
26+
<artifactId>ingester-prometheus-metrics</artifactId>
27+
28+
<properties>
29+
<prometheus.version>0.16.0</prometheus.version>
30+
</properties>
31+
32+
<dependencies>
33+
<dependency>
34+
<groupId>io.greptime</groupId>
35+
<artifactId>ingester-common</artifactId>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>io.prometheus</groupId>
40+
<artifactId>simpleclient_dropwizard</artifactId>
41+
<version>${prometheus.version}</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>io.prometheus</groupId>
45+
<artifactId>simpleclient_httpserver</artifactId>
46+
<version>${prometheus.version}</version>
47+
</dependency>
48+
</dependencies>
49+
</project>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 com.codahale.metrics.MetricRegistry;
20+
import io.greptime.common.Lifecycle;
21+
import io.prometheus.client.CollectorRegistry;
22+
import io.prometheus.client.dropwizard.DropwizardExports;
23+
import io.prometheus.client.exporter.HTTPServer;
24+
import java.io.IOException;
25+
import java.net.InetSocketAddress;
26+
import java.util.concurrent.atomic.AtomicBoolean;
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
29+
30+
public class MetricsExporter implements Lifecycle<Void> {
31+
32+
private static final Logger LOG = LoggerFactory.getLogger(MetricsExporter.class);
33+
34+
private final CollectorRegistry prometheusMetricRegistry;
35+
36+
private final int port;
37+
private HTTPServer server;
38+
39+
private final AtomicBoolean started = new AtomicBoolean(false);
40+
41+
public MetricsExporter(int port, MetricRegistry dropwizardMetricRegistry) {
42+
this.port = port;
43+
this.prometheusMetricRegistry = new CollectorRegistry();
44+
this.prometheusMetricRegistry.register(new DropwizardExports(dropwizardMetricRegistry));
45+
}
46+
47+
@Override
48+
public boolean init(Void opts) {
49+
if (this.started.compareAndSet(false, true)) {
50+
try {
51+
this.server = new HTTPServer(new InetSocketAddress(this.port), this.prometheusMetricRegistry);
52+
LOG.info("Metrics exporter started at `http://localhost:{}/metrics`", this.port);
53+
return true;
54+
} catch (IOException e) {
55+
this.started.set(false);
56+
LOG.error("Failed to start metrics exporter", e);
57+
throw new RuntimeException("Failed to start metrics exporter", e);
58+
}
59+
}
60+
return false;
61+
}
62+
63+
@Override
64+
public void shutdownGracefully() {
65+
if (this.started.compareAndSet(true, false)) {
66+
if (this.server != null) {
67+
this.server.close();
68+
LOG.info("Metrics exporter stopped");
69+
}
70+
}
71+
}
72+
}

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<module>ingester-example</module>
5353
<module>ingester-all</module>
5454
<module>ingester-bulk-protocol</module>
55+
<module>ingester-prometheus-metrics</module>
5556
</modules>
5657

5758
<scm>
@@ -122,6 +123,11 @@
122123
<artifactId>ingester-grpc</artifactId>
123124
<version>${project.version}</version>
124125
</dependency>
126+
<dependency>
127+
<groupId>${project.groupId}</groupId>
128+
<artifactId>ingester-prometheus-metrics</artifactId>
129+
<version>${project.version}</version>
130+
</dependency>
125131
<dependency>
126132
<groupId>${project.groupId}</groupId>
127133
<artifactId>ingester-protocol</artifactId>

0 commit comments

Comments
 (0)