Subscribe to Stuck in an Infiniteloop        RSS Feed
-----

Prometheus Remote Write Part 2

Icon Leave Comment
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

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


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


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

 

May 2020

S M T W T F S
     12
3456789
10111213141516
17181920212223
2425 26 27282930
31      

Tags

    Recent Entries

    Recent Comments

    Search My Blog

    3 user(s) viewing

    3 Guests
    0 member(s)
    0 anonymous member(s)