Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Loki to monitor SpringBoot applications

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to use Loki to monitor SpringBoot applications", the content is easy to understand, clear, hope to help you solve your doubts, let the editor lead you to study and learn "how to use Loki to monitor SpringBoot applications" this article.

Log Collection practice of Loki

Previously, we used Docker Compose to launch Grafana, Loki and Promtail at the same time to monitor an application. And the architecture of the whole log system.

Loki log system

But in practical application, it is impossible to establish a Grafana-Loki-Promtail system for every application.

Promtail Log Agent

At present, Promtail can track logs from two sources: local log files and systemd logs. What we demonstrated in the last article is the loading of local log files, which is the only way I can use at present. The other way is through the service discovery capability of K8S.

Loki flow chart

If you deploy multiple Promtail daemons on multiple application servers, you can monitor the static log files of multiple applications and push the logs to Loki through Loki API for aggregate management.

Deployment topology of Spring Boot logs

Promtail dynamic configuration

We only need to deploy the relevant Promtail daemon for the Loki application. Here I still use Docker to deploy Promtail, but I can no longer use the default configuration, so the config.yml should be:

Server: http_listen_port: 9080 grpc_listen_port: 0 positions: filename: / var/log/positions.yaml client: url: http://${LOKI_HOST}:${LOKI_PORT}/loki/api/v1/push scrape_configs:-job_name: system pipeline_stages: static_configs:-labels: app: ${APP_NAME} job: varlogs host : ${LOG_HOST} _ _ path__: / var/log/*log

In order to build a generic configuration, I made some parameters dynamic. This is a feature provided by the Loki2.1+ version, and you can use ${} to refer to environment variables, and you can even specify a default value of ${VAR:default_value}. But you have to know that in order to turn on this feature, you need to add the option-config.expand-env to the Promtail startup command.

Promtail Docker image modification

According to this, I modified the Docker image of Promtail. The specific Dockerfile is as follows:

FROM grafana/promtail:2.2.1 LABEL AUTHOR = felord.cn VOLUME ["/ var/log/"] EXPOSE 9080 ENV LOKI_HOST= "localhost" ENV LOKI_PORT=3100 ENV APP_NAME= "APP" ENV LOG_HOST= "localhost" COPY config.yml / etc/promtail/ CMD ["- config.file=/etc/promtail/config.yml", "- config.expand-env"]

You can use docker build-t loki-promtail:1.0. Command to build this custom Promtail image. Basic startup commands:

Docker run-d-- name promtail-service-- network loki- v c:/docker/log:/var/log/-e LOKI_HOST=loki-e APP_NAME=SpringBoot loki-promtail:1.0

The mounted directory c:/docker/log is still the log directory of the application, and LOKI_HOST should ensure that it can communicate with the Loki server, whether you are directly connected or over the Docker network (here the Docker bridge is used). You can use Docker Compose to bundle the application with Promtail, and all Promtail will send the corresponding logs to Loki for centralized management. In addition, through the custom Label, we can search the log by applying the name.

The above is all the contents of the article "how to use Loki to monitor SpringBoot applications". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report