fluent-bit icon indicating copy to clipboard operation
fluent-bit copied to clipboard

How to parse data as a collection of key/value pairs

Open lktslionel opened this issue 9 years ago • 10 comments

Hi Guys, I'm a little new to fluent-bit and I want to use the filter "parser" to parse a key on a log line.

Given :

A log line

[0] os.component/abba/3594881dacb8: [1511196039.000000000, {"container_name"=>"/abba", "source"=>"stdout", "log"=>"time="2017-11-20 17:40:39" || component="EX--ABBA" || logger="/os/app/src/index.js" || type="TECHNICAL" || msg="/health endpoint ca", "container_id"=>"3594881dacb8d9d3317a6c9fc6aff2ec627ee43037fec5e609e22c6cb9cf93b7"}]

And

A parser filter with the key_name I want to parse "log"

[FILTER]
    Name parser
    Key_Name log
    Parser os_parser

And

A Parser file with content

[PARSER]
    Name os_parser
    Format regex
    Regex <I_DONT_KNOW_THE_RIGHT_REGEX>

When

I run fluent-bit with the configuration given above

Then

I should get the following output

[3] os.component: [1511195416.002497117, {"time"=>"2017-11-20 17:22:32", "component" => "EX--ABBA", "logger" => "/os/app/src/index.js", "type"=>"TECHNICAL", "msg"=>"/health endpoint ca" }]

Do you have any idea of which regex (<I_DONT_KNOW_THE_RIGHT_REGEX>) I could use to achieve that result ?

Thanks in advance

lktslionel avatar Nov 20 '17 16:11 lktslionel

I could contribute by creating a parser function in Go that aims to parser key/val pairs with a given separator; but I don't know how to create one not in C but in Go instead. Thanks

lktslionel avatar Nov 21 '17 09:11 lktslionel

regexp

fluentbit supports named capture. It can define the key name.

e.g (?<key_name>[^ ]*)

could you check this document? http://fluentbit.io/documentation/current/parser/regular_expression.html

http://rubular.com/ is helpful to test regexp.

nokute78 avatar Nov 21 '17 23:11 nokute78

I agree but I need to capture key_name/key_value without having to specify the key_name by its position in the log line. The behavior I need is something similar to logstash kv plugin

Thanks

lktslionel avatar Nov 26 '17 21:11 lktslionel

Hello, @edsiper Does your commit solves my issue ? Thanks

lktslionel avatar Feb 06 '19 16:02 lktslionel

Hi, We also need to have a parser that allows us to extract key=value pairs without a need to defining the name and order of each field... Is there anything like that? Tons of our applications would break with this restriction!

dvlato avatar Apr 26 '19 14:04 dvlato

I came across this issue while checking the same for myself and it seems to me that the logfmt parser is what you're looking for. Please notice I still haven't tested it, but the docs seem to indicate that it's the way to go

hernandesbsousa avatar Aug 30 '19 17:08 hernandesbsousa

Thanks @hernandesbsousa ! I will try this one a let you guys, know :-)

lktslionel avatar Nov 14 '19 15:11 lktslionel

thanks everyone.

ref: https://docs.fluentbit.io/manual/parser/logfmt

edsiper avatar Nov 28 '19 17:11 edsiper

Is there any way to use logfmt with a delimiter other than space? I can't seem to find any config options in any documentation

ilanRosenbaum avatar Sep 01 '22 17:09 ilanRosenbaum

Commenting on this old issue, cause google brought me here I couldn't get logfmt to work with a comma delimiter either, but a little lua helped me out https://github.com/tibbycat/parser-prometheus

tibbycat avatar Aug 26 '23 23:08 tibbycat