Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Send your logs to Loki

Tags: log nfrankel loki

Posted on Aug 31 • Originally published at blog.frankel.ch One of my current talks focuses on Observability in general and Distributed Tracing in particular, with an OpenTelemetry implementation. In the demo, I show how you can see the traces of a simple distributed system consisting of: the Apache APISIX API Gateway, a Kotlin app with Spring Boot, a Python app with Flask, and a Rust app with Axum.Earlier this year, I spoke and attended the Observability room at FOSDEM. One of the talks demoed the Grafana stack: Mimir for metrics, Tempo for traces, and Loki for logs. I was pleasantly surprised how one could move from one to the other. Thus, I wanted to achieve the same in my demo but via OpenTelemetry to avoid coupling to the Grafana stack.In this blog post, I want to focus on logs and Loki.At its core, Loki is a Log storage engine:Loki is a horizontally scalable, highly available, multi-tenant log aggregation system inspired by Prometheus. It is designed to be very cost effective and easy to operate. It does not index the contents of the logs, but rather a set of labels for each log stream.LokiLoki provides a RESTful API to store and read logs. Let's push a log from a Java app. Loki expects the following payload structure:I'll use Java, but you can achieve the same result with a different stack. The most straightforward code is the following:The prototype works, as seen in Grafana:However, the code has many limitations:Finally, it's completely unrelated to how we use logs, e.g.:To use the above statement, we need to choose a logging implementation. Because I'm more familiar with it, I'll use SLF4J and Logback. Don't worry; the same approach works for Log4J2.We need to add relevant dependencies:Now, we add a specific Loki appender:Our program has become much more straightforward:Grafana displays the following:I'm running most of my demos on Docker Compose, so I'll mention the Docker logging trick. When a container writes on the standard out, Docker saves it to a local file. The docker logs command can access the file content.However, other options than saving to a local file are available, e.g., syslog, Google Cloud, Splunk, etc. To choose a different option, one sets a logging driver. One can configure the driver at the overall Docker level or per container.Loki offers its own plugin. To install it:At this point, we can use it on our container app:The result is the following. Note the default labels.From a bird's eye view, Loki is nothing extraordinary: it's a plain storage engine with a RESTful API on top.Several approaches are available to use the API. Beyond the naive one, we have seen a Java logging framework appender and Docker. Other approaches include scraping the log files, e.g., Promtail, via a Kubernetes sidecar. You could also add an Opentelemetry Collector between your app and Loki to perform transformations.Options are virtually unlimited. Be careful to choose the one that fits your context the best.To go further:Originally published at A Java Geek on August 27th, 2023Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Shai Almog - Aug 22 Frank Osasere Idugboe - Aug 24 Lionel Tchami ♾️☁️ - Aug 20 Ivan - Aug 23 Once suspended, nfrankel will not be able to comment or publish posts until their suspension is removed. Once unsuspended, nfrankel will be able to comment and publish posts again. Once unpublished, all posts by nfrankel will become hidden and only accessible to themselves. If nfrankel is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Nicolas Frankel. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag nfrankel: nfrankel consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging nfrankel will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Send your logs to Loki

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×