DISSECT and key=value logs
12 min · 2 ejercicio(s)
Grok is powerful, but every pattern is a regular expression. When a format is fixed (same delimiters in the same order on every line), DISSECT does the job faster and with a simpler syntax:
Mar 12 09:58:20 fw-edge-01 kernel: [FW-ALLOW] IN=lan0 OUT=wan0 SRC=10.10.6.61 DST=45.133.1.20 LEN=60 PROTO=TCP SPT=49712 DPT=443FROM lab-raw
| WHERE event.dataset == "iptables.log"
| DISSECT message "%{} [FW-%{action}] IN=%{in} OUT=%{out} SRC=%{src} DST=%{dst} LEN=%{len} PROTO=%{proto} SPT=%{spt} DPT=%{dpt}"
| STATS lines = COUNT(*) BY action| Syntax | Meaning |
|---|---|
%{name} |
Everything up to the next literal text |
%{} or %{?name} |
Skip this part |
%{+name} |
Append to an earlier key (%{+name} joins two parts into one field) |
%{name->} |
Ignore repeated delimiters (padding) after this key |
DISSECT has no types and no regular expressions: fields are strings, convert them with TO_INTEGER(dpt) when needed. If a line doesn't fit, all keys are null, just like Grok.
Grok or DISSECT?
| Use DISSECT when … | Use Grok when … |
|---|---|
| the delimiters are always the same | parts are optional or vary between lines |
| you parse millions of lines per second | you need to validate a value (IP, INT) |
the format is CSV-like, key=value or pipe-separated |
you need types or alternatives |
A common trick is to combine both: DISSECT the fixed header, then GROK only the variable message part.
key=value logs
The firewall line is really a list of key=value pairs. Ingest pipelines have a dedicated kv processor for that, which splits on a field separator (space) and a value separator (=). Note the empty OUT= on blocked inbound packets: DISSECT and kv handle that, a Grok pattern with %{WORD:out} would fail.
🔎 Profundiza: funciones, errores habituales y práctica
Why DISSECT is faster
DISSECT scans the string once from left to right and looks for the next delimiter. A Grok pattern is compiled into a regular expression that may backtrack. For a pipeline that processes tens of thousands of events per second, the difference is noticeable. Elastic's own integrations use dissect wherever the format allows.
Padding
Syslog pads one-digit days with an extra space (Mar 9). A DISSECT pattern %{month} %{day} %{time} breaks on such lines because the first delimiter matches the first space only. %{month->} %{day} %{time} tells DISSECT to skip repeated spaces after month. The example above avoids the problem by skipping the whole header with %{}.