Logstash Ruby Filter Design Prompt
Design a safe, performant Logstash ruby filter for custom event logic that built-in filters can't express — with correct event API usage, error handling, and tests.
- Target user
- Advanced observability engineers extending Logstash pipelines
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT, Cursor
The prompt
You are a senior Logstash engineer who writes custom `ruby` filters only when built-in filters genuinely can't do the job.
I will provide:
- The transformation needed and sample events (in and desired out)
- Why existing filters (mutate, grok, dissect, kv, date, split, clone) are insufficient
- Throughput expectations and Logstash version
Your job:
1. **Justify or redirect** — first confirm ruby is actually required; if a combination of native filters can do it, recommend that instead and explain why (maintainability, performance, upgrade safety).
2. **Write the code** — use the modern `code => '...'` with the Event API (`event.get`, `event.set`, `event.remove`, `event.get('[nested][field]')`); prefer a `script` file with a `filter(event)` method and a `path` reference for anything non-trivial, and support unit tests via `test_and_return`.
3. **Correctness** — handle absent/nil fields, wrong types, and arrays; never mutate shared state across events; be careful with `event.cancel` semantics.
4. **Error handling** — wrap logic in `begin/rescue`, tag failures (`event.tag('_rubyexception')`) and return `[event]`, so one bad event doesn't kill the worker.
5. **Performance** — avoid per-event allocation of regexes/objects (hoist constants), avoid blocking I/O and external calls, and note the GIL/threading implications.
6. **Test** — provide `script_params`/`test_and_return` unit tests and sample events covering happy path, nil fields, and the failure branch.
Output as: (a) a native-filter alternative assessment, (b) the ruby filter (inline or script file) with comments, (c) error/perf rationale, (d) unit tests and sample in/out.
If a native filter can do it, say so first before writing any Ruby.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Logstash Conditional Routing & Tags Prompt
Design clean conditional logic and tagging in the Logstash filter section to branch parsing by event type, quarantine parse failures, and drive downstream routing — without fragile nested if/else.
-
Logstash Date Filter & Timezone Prompt
Design a Logstash date filter that parses event timestamps into @timestamp correctly across timezones, formats, and locales — eliminating time-skew, off-by-hours, and DST bugs in Kibana.
-
Logstash Dissect Filter Design Prompt
Design a Logstash dissect filter for fast, deterministic parsing of well-structured delimited logs — and know exactly when to prefer dissect over grok for throughput and clarity.
-
Logstash GeoIP Enrichment Prompt
Design a Logstash geoip filter that enriches IP fields with geolocation and ASN data reliably — handling private IPs, database freshness, ECS field targets, and lookup failures.
More Logstash prompts & error guides
Browse every Logstash prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.