In the previous post, I laid out how to use Prometheus' remote_write capability with non Go languages and used python as an example. Today we'll look at an example in java.
The steps are the same:
1. Gather the .proto files from Prometheus.
2. Generate language specific source code with protoc
3. Write a server to receive requests from Prometheus. Jetty is nice for a quick prototype:
You'll need the following additional dependencies for your project:
Handler for our prometheus metric protobuf data:
Main Server:
--
Happy coding!
The steps are the same:
1. Gather the .proto files from Prometheus.
2. Generate language specific source code with protoc
Quote
./protoc/bin/protoc --proto_path=./imports --java_out=./java_output/ imports/types.proto
./protoc/bin/protoc --proto_path=./imports --java_out=./java_output/ imports/remote.proto
./protoc/bin/protoc --proto_path=./imports --java_out=./java_output/ imports/gogoproto/gogo.proto
./protoc/bin/protoc --proto_path=./imports --java_out=./java_output/ imports/remote.proto
./protoc/bin/protoc --proto_path=./imports --java_out=./java_output/ imports/gogoproto/gogo.proto
Quote
ls -R java_output/
java_output/:
com prometheus
java_output/com:
google
java_output/com/google:
protobuf
java_output/com/google/protobuf:
GoGoProtos.java
java_output/prometheus:
Remote.java Types.java
java_output/:
com prometheus
java_output/com:
java_output/com/google:
protobuf
java_output/com/google/protobuf:
GoGoProtos.java
java_output/prometheus:
Remote.java Types.java
3. Write a server to receive requests from Prometheus. Jetty is nice for a quick prototype:
You'll need the following additional dependencies for your project:
<!-- protobuf --> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>3.11.1</version> </dependency> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java-util</artifactId> <version>3.11.1</version> </dependency> <!-- snappy compression --> <dependency> <groupId>org.xerial.snappy</groupId> <artifactId>snappy-java</artifactId> <version>1.1.7.3</version> </dependency>
Handler for our prometheus metric protobuf data:
public class PrometheusHandler extends AbstractHandler { private static final Logger logger = LoggerFactory.getLogger(PrometheusHandler.class); private static JsonFormat.Printer JSON_PRINTER = JsonFormat.printer(); public PrometheusHandler() { super(); } @Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { try (InputStream is = request.getInputStream()) { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); int nRead; byte[] data = new byte[1024]; while ((nRead = is.read(data, 0, data.length)) != -1) { buffer.write(data, 0, nRead); } buffer.flush(); Remote.WriteRequest writeRequest = Remote.WriteRequest.parseFrom(Snappy.uncompress(buffer.toByteArray())); String json = JSON_PRINTER.print(writeRequest); logger.info(json); } catch (IOException e) { throw e; } } }
Main Server:
public class MetricsReporter { private static final Logger logger = LoggerFactory.getLogger(MetricsReporter.class); private static Server createServer(final int port){ Server server = new Server(port); server.setHandler(new PrometheusHandler()); return server; } public static void main(String[] args) throws Exception { logger.info("Starting metrics reporting server: "); Server server = createServer(8000); server.start(); server.join(); } }
--
Happy coding!
0 Comments On This Entry
← February 2021 →
S | M | T | W | T | F | S |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 |
Tags
My Blog Links
Recent Entries
-
-
-
Prometheus Remote Write Part 2
on Jan 16 2020 08:47 AM
-
-
Recent Comments
Search My Blog
1 user(s) viewing
1 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)