Skip to content

Commit 1a85e95

Browse files
committed
feat: prometheus metrics exporter
1 parent 7814fa5 commit 1a85e95

File tree

7 files changed

+132
-0
lines changed

7 files changed

+132
-0
lines changed

ingester-all/pom.xml

+4
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

+4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package io.greptime;
1818

1919
import io.greptime.BulkWrite.Config;
20+
import io.greptime.common.util.MetricsUtil;
2021
import io.greptime.common.util.StringBuilderHelper;
22+
import io.greptime.metrics.MetricsExporter;
2123
import io.greptime.models.DataType;
2224
import io.greptime.models.Table;
2325
import io.greptime.models.TableSchema;
@@ -118,6 +120,8 @@ public static void main(String[] args) throws Exception {
118120

119121
bulkStreamWriter.completed();
120122
}
123+
124+
greptimeDB.shutdownGracefully();
121125
}
122126

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

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

+7
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

+7
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
}

ingester-prometheus-metrics/pom.xml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>io.greptime</groupId>
6+
<artifactId>greptimedb-ingester</artifactId>
7+
<version>0.14.2</version>
8+
</parent>
9+
<artifactId>ingester-prometheus-metrics</artifactId>
10+
11+
<properties>
12+
<prometheus.version>0.16.0</prometheus.version>
13+
</properties>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>io.greptime</groupId>
18+
<artifactId>ingester-common</artifactId>
19+
</dependency>
20+
21+
<dependency>
22+
<groupId>io.prometheus</groupId>
23+
<artifactId>simpleclient_dropwizard</artifactId>
24+
<version>${prometheus.version}</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>io.prometheus</groupId>
28+
<artifactId>simpleclient_httpserver</artifactId>
29+
<version>${prometheus.version}</version>
30+
</dependency>
31+
</dependencies>
32+
</project>
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

+6
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)