The correction is integrated into the TP (See the green boxes)
What you will learn in this TP
- Read log files
- Extract and add labels
- Send to Loki and check
Introduction #
What you will learn in this section
- Read log files
- Extract and add labels
- Send to Loki and check
Context. An application writes JSON logs to files, and nobody reads them before an incident. You centralize them in Loki.
Prerequisites: Docker Compose, curl · Estimated time: 30 min
Log pipeline #
Guided steps, with their solution.
1. Prepare the stack
Why? The volume keeps read positions: without it, Alloy would re-read everything at each restart.
Write a Compose file with Loki and Alloy.See the correction
services: loki: image: grafana/loki:latest command: -config.file=/etc/loki/local-config.yaml ports: - "3100:3100" alloy: image: grafana/alloy:v1.21.3 command: run --server.http.listen-addr=0.0.0.0:12345 --storage.path=/var/lib/alloy/data /etc/alloy/config.alloy ports: - "12345:12345" volumes: - ./config.alloy:/etc/alloy/config.alloy:ro - ./logs:/var/log/myapp:ro - alloy-data:/var/lib/alloy/data depends_on: [loki] volumes: alloy-data:2. Read the files
Why?
Inlocal.file_matchfinds files,loki.source.filereads them and remembers its position.config.alloy, read/var/log/myapp/*.logwith thejob=myapplabel.See the correction
local.file_match "app" { path_targets = [{ "__path__" = "/var/log/myapp/*.log", "job" = "myapp" }] } loki.source.file "app_logs" { targets = local.file_match.app.targets forward_to = [loki.process.app.receiver] }3. Process the lines
Why?
Extractstage.labelstakes an extracted value; a fixed value needsstage.static_labels.levelfrom the JSON to make it a label, addenv=productionand keepmsgas text.See the correction
loki.process "app" { forward_to = [loki.write.local_loki.receiver] stage.json { expressions = { level = "", msg = "" } } stage.labels { values = { level = "" } } stage.static_labels { values = { env = "production" } } stage.output { source = "msg" } }4. Send to Loki
Why? In the Compose network, Loki is reached by its service name.
Add the sending component.See the correction
loki.write "local_loki" { endpoint { url = "http://loki:3100/loki/api/v1/push" } }5. Check
Why? A
Create a log file, start the stack and query Loki: you should see thehealthycomponent does not prove that data arrives.envandlevellabels, and the textspaiement refuséandcommande créée.See the correction
mkdir -p logs cat >> logs/app.log <<'EOF' {"level":"error","msg":"paiement refusé"} {"level":"info","msg":"commande créée"} EOF docker compose up -d sleep 20 curl -s http://localhost:3100/loki/api/v1/labels curl -s -G http://localhost:3100/loki/api/v1/query_range \ --data-urlencode 'query={job="myapp",env="production"}' \ --data-urlencode "start=$(( $(date +%s)-600 ))000000000"
Difficulty level : ●●○○○ (2/5)
My progress
Sign in to track your progress and take notes. Sign in
Course Glossary
Grafana Alloy
An open-source, vendor-neutral telemetry collector designed to replace Grafana Agent, fully compatible with OpenTelemetry (OTel).
Recommended Articles
Bonnes pratiques de collecte de logs avec Fluent Bit et Vector
Check-list de production pour la collecte de logs : structure, filtrage à la source, masquage, ta...
Grafana Alloy : Collecter les métriques système et les logs locaux
Découvrez comment configurer Grafana Alloy pour superviser le serveur sur lequel il s'exécute, av...
Grafana Alloy vs Grafana Agent vs Promtail : Lequel choisir ?
Découvrez l'évolution des collecteurs de télémétrie de l'écosystème Grafana. Comparatif détaillé ...
Grafana Loki vs Elasticsearch : La bataille du stockage de logs
Comparatif entre Loki, le système de logs inspiré par Prometheus, et Elasticsearch, le moteur de ...
LogQL (1/2) : requêter les logs (sélecteurs, filtres et parsers)
Apprendre LogQL : sélecteurs de flux, filtres de ligne, parsers json/logfmt/pattern/regexp, filtr...
Bonnes pratiques Grafana Loki : labels, requêtes, stockage et coûts
Bonnes pratiques Loki : choix des labels et cardinalité, structured metadata, requêtes LogQL perf...
LogQL (2/2) : transformer les logs en métriques
Créer des métriques à partir des logs avec LogQL : rate, count_over_time, bytes_rate, agrégations...
Traefik : les logs (journaux applicatifs et access logs)
Configurer les deux journaux de Traefik : le journal de fonctionnement (niveaux, format) et les a...
Fluent Bit : les outputs (sorties), routage et fiabilité de l'envoi
Envoyer les données de Fluent Bit : routage par Match et Match_Regex, sorties Loki, Elasticsearch...