RousselTMLEARNING

Article · Fluent Bit and Vector: lightweight log collection

VRL and unit tests

This article is available in French.

1. Les bases de VRL #

Un langage sûr et rapide.

Syntaxe #

# Le point (.) est l'événement courant
.service = "boutique"
.status = to_int!(.status)                      # « ! » : erreur fatale si la conversion échoue

# Gestion explicite des erreurs
parsed, err = parse_json(.message)
if err == null {
  . = merge(., parsed)
  del(.message)
} else {
  .parse_error = err
}

# Conditions
if .status >= 500 { .severity = "error" } else if .status >= 400 { .severity = "warning" } else { .severity = "info" }
À savoir : VRL est vérifié à la compilation : une fonction qui peut échouer doit être traitée (! ou affectation de l'erreur). Une configuration qui démarre ne plantera pas sur un événement inattendu.

2. Recettes #

Les transformations courantes.

Analyser un log nginx #

parsed = parse_regex!(.message, r'^(?P<ip>\S+) \S+ \S+ \[(?P<ts>[^\]]+)\] "(?P<method>\S+) (?P<path>\S+) \S+" (?P<status>\d+) (?P<bytes>\d+)')
. = merge(., parsed)
.status = to_int!(.status)
.bytes = to_int!(.bytes)
.timestamp = parse_timestamp!(.ts, "%d/%b/%Y:%H:%M:%S %z")
del(.ts)

Masquer des données personnelles #

.message = redact(.message, filters: [r'\b[\w.+-]+@[\w-]+\.[\w.]+\b', "us_social_security_number"])
.client_ip = ip_cidr_contains!("10.0.0.0/8", .ip) ? "interne" : "externe"
del(.authorization)
Bonne Pratique : Masquez au plus près de la source : un e-mail qui atteint Elasticsearch ou Loki se retrouve dans les sauvegardes et les index pour toute la durée de rétention.

Router et produire des métriques #

transforms:
  aiguillage:
    type: route
    inputs: [parse]
    route:
      securite: .source == "auth"
      lent: .duration_ms > 1000
  requetes_metriques:
    type: log_to_metric
    inputs: [parse]
    metrics:
      - type: counter
        field: status
        name: http_requests_total
        tags: { status: "{{ status }}", method: "{{ method }}" }
sinks:
  prom:
    type: prometheus_exporter
    inputs: [requetes_metriques]
    address: 0.0.0.0:9598

3. Tests unitaires #

Une configuration testée comme du code.

vector test #

tests:
  - name: analyse nginx
    inputs:
      - insert_at: parse
        type: log
        log_fields:
          message: '10.0.0.1 - - [19/Oct/2026:10:00:00 +0200] "GET /panier HTTP/1.1" 502 120'
    outputs:
      - extract_from: parse
        conditions:
          - type: vrl
            source: |
              assert_eq!(.status, 502)
              assert_eq!(.path, "/panier")
vector test vector.yaml
Bonne Pratique : Ajoutez vector validate et vector test à la CI du dépôt de configuration : une regex cassée ne part plus en production.

📄 Transformer avec VRL et tester ses pipelines Vector →

My progress

  • ⏱ Estimated time : ≈ 9 min

Sign in to track your progress and take notes. Sign in

✉️ Ask a question

Recommended Articles

Understanding the concept of score

The concept of score allows Elasticsearch to rank your documents by relevance during a search.

Understanding the Elastic Common Schema (ECS)

As always in our consulting engagements, we recommend that companies harmonise and standardise: k...

License consumption types

Understand the evolution of billing in Dynatrace: the difference between the old licensing model ...

Grafana Alloy: The importance of Self-Monitoring

Discover why and how to configure Grafana Alloy so that it monitors itself, collecting its own lo...

Grafana Alloy: Understanding and exploiting the User Interface (UI)

Discover how to enable, secure, and use Grafana Alloy's built-in web interface to visualize your ...

Grafana Alloy: Introduction and Architecture

Discover the fundamental concepts of Grafana Alloy, the transition from the static Agent to Alloy...

Grafana Alloy: Syntax and Configuration (Alloy Language: River)

As part of a Grafana training or observability training, master the declarative syntax of Grafana...

Grafana Alloy: Metrics Collection (Prometheus & Ecosystem)

Learn how to configure Grafana Alloy to collect, transform, and forward metrics using the Prometh...

Grafana Alloy: Log Management with Loki

Discover how to configure Grafana Alloy to read log files, journald, or network streams, process ...