logspout-kafka icon indicating copy to clipboard operation
logspout-kafka copied to clipboard

Why doesn't this write json to kafka?

Open rocketraman opened this issue 8 years ago • 9 comments

I'm not sure why this doesn't write json to kafka? We basically take our nice structured log output, and destructure it.

rocketraman avatar Jan 29 '17 00:01 rocketraman

Agree completely. Looking into solutions for this now.

gavinmcnair avatar Nov 05 '17 22:11 gavinmcnair

Any updates on this? Thanks!

rocketraman avatar Jun 20 '18 04:06 rocketraman

My solution was ugly

Put it back into JSON.

https://github.com/gavinmcnair/logspout-kafka

gavinmcnair avatar Jun 21 '18 17:06 gavinmcnair

Could just format the template to do that yourself

KAFKA_TEMPLATE="{\"time\":\"{{.Time}}\",\"container_name\":\"{{.Container.Name}}\",\"source\":\"{{.Source}}\",\"data\":\"{{.Data}}\"}"

Or more simply

KAFKA_TEMPLATE="{\"msg\":\"{{.Data}}\"}"

OneCricketeer avatar Oct 01 '18 05:10 OneCricketeer

Could just format the template to do that yourself

This is quite likely to result in invalid JSON output, given that it does no escaping whatsoever of the data fields.

rocketraman avatar Oct 01 '18 13:10 rocketraman

Probably a way to escape it. Not too familiar with Go templates.

https://golang.org/pkg/text/template/

OneCricketeer avatar Oct 01 '18 19:10 OneCricketeer

Well, yes, I mean, that is the obvious workaround. The point of the issue, however, is why we need to do:

JSON -> destructured -> JSON

when we could just output the original JSON in the first place.

rocketraman avatar Oct 03 '18 04:10 rocketraman

We seem to be struggling with the way Logspout is designed: applying templates to Docker log objects.

Interestingly, the stock raw adapter does have a toJSON helper to aid with extracting values from the object from within the template. https://github.com/gliderlabs/logspout#raw-format

dylanmei avatar Oct 04 '18 14:10 dylanmei

If the message is already JSON, then I think you would just do KAFKA_TEMPLATE="{{.Data}}". Otherwise, if you need to get the other fields of the container, source, and time, then you need a workaround or use the raw formatting.

OneCricketeer avatar Oct 04 '18 20:10 OneCricketeer