RousselTMLEARNING

Observability

Logstash best practices: pipelines, performance, reliability and monitoring

Logstash best practices: multiple pipelines, dissect rather than grok, conditionals, persistent queue, dead letter queue, workers/batch settings, JVM, security and monitoring API.

1. Designing pipelines #

Separate, simplify, test.

Several pipelines #

Logstash pipeline
The persistent queue protects against data loss when the output fails or slows down.
# config/pipelines.yml : un pipeline par flux, avec ses propres réglages
- pipeline.id: nginx
  path.config: "/etc/logstash/conf.d/nginx.conf"
  pipeline.workers: 4
  pipeline.batch.size: 250
  queue.type: persisted
  queue.max_bytes: 4gb
- pipeline.id: app
  path.config: "/etc/logstash/conf.d/app.conf"
  queue.type: persisted
  dead_letter_queue.enable: true
  • One pipeline per type of stream: failures and slowdowns do not spread from one stream to another.
  • Chain pipelines with the pipeline input/output plugins to share processing.
  • Version the files and validate them in CI with --config.test_and_exit.

2. Efficient filters #

dissect, conditionals and date.

Preferring dissect to grok #

filter {
  if [fields][type] == "nginx" {
    dissect {
      mapping => { "message" => '%{client_ip} - - [%{ts}] "%{method} %{uri} HTTP/%{http_ver}" %{status} %{bytes}' }
    }
    date   { match => ["ts", "dd/MMM/yyyy:HH:mm:ss Z"]  target => "@timestamp" }
    mutate { convert => { "status" => "integer"  "bytes" => "integer" }  remove_field => ["ts", "message"] }
  } else if [fields][type] == "app" {
    json { source => "message"  skip_on_invalid_json => true }
  }
  # grok seulement pour les formats variables, avec ancrage et motifs précis
  grok { match => { "message" => "^%{TIMESTAMP_ISO8601:ts} %{LOGLEVEL:level} %{GREEDYDATA:msg}$" }  tag_on_failure => ["_grokfail"] }
}
Good practice: dissect is much faster than grok when the format is fixed. Keep grok for irregular formats and always anchor your patterns (^...$).

3. Reliability and security #

Lose nothing, expose nothing.

Outputs and DLQ #

output {
  elasticsearch {
    hosts => ["https://es01:9200", "https://es02:9200"]
    api_key => "${ES_API_KEY}"                       # secret dans le keystore Logstash
    ssl_certificate_authorities => ["/etc/logstash/ca.crt"]
    data_stream => "true"
    data_stream_type => "logs"  data_stream_dataset => "nginx"  data_stream_namespace => "prod"
  }
}
# Secrets : bin/logstash-keystore create && bin/logstash-keystore add ES_API_KEY
# Relire la dead letter queue pour corriger les rejets
input { dead_letter_queue { path => "/var/lib/logstash/dead_letter_queue"  pipeline_id => "app"  commit_offsets => true } }
Common mistake: A blocked output (Elasticsearch unavailable) without a persistent queue loses events or blocks inputs. Enable queue.type: persisted and size queue.max_bytes.

4. Performance and monitoring #

Measure before tuning.

Monitoring API and settings #

curl -s localhost:9600/_node/stats/pipelines?pretty | jq '.pipelines | to_entries[] | {id: .key, in: .value.events.in, out: .value.events.out}'
curl -s 'localhost:9600/_node/stats/jvm?pretty' | jq '.jvm.mem.heap_used_percent'
curl -s 'localhost:9600/_node/hot_threads?human=true' | head -30          # où le CPU est consommé
bin/logstash --config.test_and_exit -f /etc/logstash/conf.d/             # validation
SettingAdvice
pipeline.workersEqual to the number of cores; more if filters wait on I/O
pipeline.batch.size125 by default; 250 – 1000 for better sending performance
JVM heap-Xms = -Xmx, typically 4 – 8 GB
pipeline.orderedDisable it if order does not matter, to parallelize
Good to know: When throughput rises sharply, place Kafka between the collectors and Logstash to absorb peaks and replay data.

Reference diagram and quick check #

Visualize the principle and check it from the command line.

Reference diagram #

Logstash pipeline statistics
A lasting gap between in and out indicates a slow output or a filling queue.

Related articles

Logstash: role, pipeline concept and installation

Written version of the video lessons: what Logstash is for, how a pipeline works (inputs, filters, outputs, q…

Logstash: inputs and outputs

Written version of the video lessons: common inputs (beats, file, tcp/udp, http, kafka, jdbc) and outputs (el…

Logstash: filters, grok and dissect

Written version of the video lessons: essential filters (mutate, date, json, kv, drop), the grok plugin and i…

Keep learning

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!