1. Designing pipelines #
Separate, simplify, test.
Several pipelines #
# 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
pipelineinput/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:dissectis much faster thangrokwhen the format is fixed. Keepgrokfor 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. Enablequeue.type: persistedand sizequeue.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| Setting | Advice |
|---|---|
pipeline.workers | Equal to the number of cores; more if filters wait on I/O |
pipeline.batch.size | 125 by default; 250 – 1000 for better sending performance |
| JVM heap | -Xms = -Xmx, typically 4 – 8 GB |
pipeline.ordered | Disable 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.
