Search code examples
node.jslogginggrafana-loki

Log to Grafana Loki from a client-side of a serverless Node.js application


Grafana documentation primarily focuses on server-side data source setup, leaving a gap for client-side use cases. The desired logging behavior is to replace standard console.log() with a direct push to Loki, similar to this:

- console.log(foo);
+ $loki.push(foo);

Is there a built-in or alternative approach to achieve this in a serverless Node.js environment?


Solution

  • An alternative client-side solution for direct logging into Loki can be created using the REST API, which is described in the documentation. To push a log into the Loki instance directly, a request can be made to the following endpoint:

    POST https://{LOKI_INSTANCE_URL}.grafana.net/loki/api/v1/push

    • To obtain the LOKI_INSTANCE_URL, follow the link in the format of: https://grafana.com/orgs/{ORGANIZATION_NAME}/hosted-logs/{ORGANIZATION_IDENTIFIER}.

    • Basic Auth is required and it can be obtained from the Grafana instance. Additionally, the header needs to be set accordingly.