Java client library for Grafana Loki

Viewed 1670

Currently we are using statsd java client to push certain application data to graphite. We build dashboards in Grafana using this data.

I am planning to switch to Grafana Loki. I was wondering if its possible to push certain data directly to Grafana Loki using a daemon like statsd . Are there are any java client libraries to do so. If so please give me maven repository link.

4 Answers

You can try Loki4j Logback appender https://github.com/loki4j/loki-logback-appender (disclosure: I'm an author of it).

Loki4j is pure Java client for Loki with flexible formatting options for labels and log messages. It supports both JSON and Protobuf flavors of Loki push API. You should be able to push any data you want directly to Loki using Loki4j.

TinyLoki - zero dependency pure Java 1.8 library for desktop and Android. Not well optimized but very simple to adopt as independent log sender with custom labels.

https://github.com/mjfryc/mjaron-tinyloki-java

Import library:

dependencies {
    implementation 'io.github.mjfryc:mjaron-tinyloki-java:0.2.2'
}

and then:

import pl.mjaron.tinyloki.*;
public class Sample {
    public static void main(String[] args) {
        LogController logController = TinyLoki.createAndStart(
            "https://localhost/loki/api/v1/push", "user", "pass");
        ILogStream stream = logController.createStream(
            TinyLoki.info().l("customStaticLabelName","customVaue"));
        stream.log("Hello world.");
        //...

        // Optionally at the application exit, send latest logs.
        logController.softStop().hardStop();
    }
}

I have crated a Java client for pushing logs to Loki. There is a log4j appender built on top of it (at my employers it is used on production to push 10s of GB of logs daily).

It is designed to be lock free and allocation free on logging path. Uses Netty so sending batches to Loki allocates, but it's very efficient.

Related