Piping between two different parsers

Hello Community

First time here so bear with me if I am not asking in the right channel. I am trying to create a new parser that will attach to already existing input plugins using data_format parameter.
The new parser will receive UDP packet from socket_listener and parse the Headers. Along with some custom parsing, I need to parse JSON that will be attached as Payload to Packet.

So, I am here for the best approach regarding connecting two parsers together. Should I try to emulate the same JSON parser behavior in my parser or I can somehow pipe JSON from my parser to JSON parser and get metrics.

Br, Aqs.

So the UDP packet is sort of like an HTTP message, something like this?:

SOMEHEADER: foo
ANOTHERHEADER: bar

{"foo": 42}

If I’m understanding correctly, I think there are two ways you could do this:

  1. Have your parser handle the headers and set a field with the unparsed JSON string. Separately you can use the parser processor with the existing json parser to expand the JSON out. This is probably the easiest to code.
  2. Your parser could have a json.JSONParser. Your parser handle the headers and then internally calls the JSON parser with the body of the packet to create the telegraf.Metric. Add the header data to the telegraf.Metric and return them.

Thanks Daniel for the reply.

You understood my scenario correctly. I would like to go with second proposed way but i have a ambiguity. Json parse would return telegraf.Metric and i would like to attach my headers to that telegraf.Metric instead of creating a separate telegraf.Metric and appending my headers to it as a separate point.
Basically, i would like to add my headers as tags to telegraf.Metric return by json parser.
Do you think it would be possible?
Thanks Again.

Br, Aqs.

Yes, you can just loop over them and call metric.AddTag(k, v)

Thank Daniel for making this an effortless process for me. I have another question, sorry for too many questions.

What would be the best way to initialize json.JSONParser fields from the telegraf configuration file, while I would be using my custom parser as data_format

I would like to read json_string_fields from the configuration file.

Thank you Again.

Br, Aqs.

You can use any of the options from the parser Config type, just copy in the options you would like to use to your parser from the NewParser function.