Merge pull request #128 from pmlopes/feature/vertx-support · feheraron/client_java@98ce1d3 · GitHub
Skip to content

Commit 98ce1d3

Browse files
authored
Merge pull request prometheus#128 from pmlopes/feature/vertx-support
Implemented Vert.x simple client support
2 parents 5dc058f + 3589e6a commit 98ce1d3

5 files changed

Lines changed: 195 additions & 0 deletions

File tree

.travis.yml

Lines changed: 3 additions & 0 deletions

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<module>simpleclient_servlet</module>
5555
<module>simpleclient_spring_boot</module>
5656
<module>simpleclient_jetty</module>
57+
<module>simpleclient_vertx</module>
5758
<module>benchmark</module>
5859
</modules>
5960

simpleclient_vertx/pom.xml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<build>
5+
<plugins>
6+
<plugin>
7+
<groupId>org.apache.maven.plugins</groupId>
8+
<artifactId>maven-compiler-plugin</artifactId>
9+
<configuration>
10+
<source>1.8</source>
11+
<target>1.8</target>
12+
</configuration>
13+
</plugin>
14+
</plugins>
15+
</build>
16+
17+
<parent>
18+
<groupId>io.prometheus</groupId>
19+
<artifactId>parent</artifactId>
20+
<version>0.0.15-SNAPSHOT</version>
21+
</parent>
22+
23+
<groupId>io.prometheus</groupId>
24+
<artifactId>simpleclient_vertx</artifactId>
25+
<packaging>bundle</packaging>
26+
27+
<name>Prometheus Java Simpleclient Vert.x</name>
28+
<description>
29+
Vert.x Web Handler exporter for the simpleclient.
30+
</description>
31+
32+
<licenses>
33+
<license>
34+
<name>The Apache Software License, Version 2.0</name>
35+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
36+
<distribution>repo</distribution>
37+
</license>
38+
</licenses>
39+
40+
<developers>
41+
<developer>
42+
<id>pmlopes</id>
43+
<name>Paulo Lopes</name>
44+
<email>paulo@mlopes.net</email>
45+
</developer>
46+
</developers>
47+
48+
<properties>
49+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
50+
</properties>
51+
52+
<dependencies>
53+
<dependency>
54+
<groupId>io.prometheus</groupId>
55+
<artifactId>simpleclient</artifactId>
56+
<version>0.0.15-SNAPSHOT</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>io.prometheus</groupId>
60+
<artifactId>simpleclient_common</artifactId>
61+
<version>0.0.15-SNAPSHOT</version>
62+
</dependency>
63+
<dependency>
64+
<groupId>io.vertx</groupId>
65+
<artifactId>vertx-web</artifactId>
66+
<version>3.3.2</version>
67+
<scope>provided</scope>
68+
</dependency>
69+
<!-- Test Dependencies Follow -->
70+
<dependency>
71+
<groupId>junit</groupId>
72+
<artifactId>junit</artifactId>
73+
<version>4.11</version>
74+
<scope>test</scope>
75+
</dependency>
76+
</dependencies>
77+
</project>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package io.prometheus.client.vertx;
2+
3+
import io.prometheus.client.CollectorRegistry;
4+
import io.prometheus.client.exporter.common.TextFormat;
5+
import io.vertx.core.Handler;
6+
import io.vertx.core.buffer.Buffer;
7+
import io.vertx.ext.web.RoutingContext;
8+
9+
import java.io.IOException;
10+
import java.io.Writer;
11+
12+
/**
13+
* Metrics Handler for Vert.x Web.
14+
*
15+
* This handler will allow the usage of Prometheus Client Java API with
16+
* Vert.x applications and expose a API compatible handler for the collector.
17+
*
18+
* Usage:
19+
*
20+
* router.route("/metrics").handler(new MetricsHandler());
21+
*/
22+
public class MetricsHandler implements Handler<RoutingContext> {
23+
24+
/**
25+
* Wrap a Vert.x Buffer as a Writer so it can be used with
26+
* TextFormat writer
27+
*/
28+
private static class BufferWriter extends Writer {
29+
30+
private final Buffer buffer = Buffer.buffer();
31+
32+
@Override
33+
public void write(char[] cbuf, int off, int len) throws IOException {
34+
buffer.appendString(new String(cbuf, off, len));
35+
}
36+
37+
@Override
38+
public void flush() throws IOException {
39+
// NO-OP
40+
}
41+
42+
@Override
43+
public void close() throws IOException {
44+
// NO-OP
45+
}
46+
47+
Buffer getBuffer() {
48+
return buffer;
49+
}
50+
}
51+
52+
private CollectorRegistry registry;
53+
54+
/**
55+
* Construct a MetricsHandler for the default registry.
56+
*/
57+
public MetricsHandler() {
58+
this(CollectorRegistry.defaultRegistry);
59+
}
60+
61+
/**
62+
* Construct a MetricsHandler for the given registry.
63+
*/
64+
public MetricsHandler(CollectorRegistry registry) {
65+
this.registry = registry;
66+
}
67+
68+
@Override
69+
public void handle(RoutingContext ctx) {
70+
try {
71+
final BufferWriter writer = new BufferWriter();
72+
TextFormat.write004(writer, registry.metricFamilySamples());
73+
ctx.response()
74+
.setStatusCode(200)
75+
.putHeader("Content-Type", TextFormat.CONTENT_TYPE_004)
76+
.end(writer.getBuffer());
77+
} catch (IOException e) {
78+
ctx.fail(e);
79+
}
80+
}
81+
}
Lines changed: 33 additions & 0 deletions

0 commit comments

Comments
 (0)