I have a log message that is a json file.
I would like to add a field to that original message within the json structure.
I've tried multiple ways of doing it and none of them get the job done.
I have trouble with the elastic documentation.
Lets say my original message input was {"hello": "world"}.
I want my output to be {"hello": "world", "logstash_timestamp", "2018-05-02T13:20:51.179Z"}.
I don't care about the metadata as it is intentionally lost in SNS.
Below is a single message entering my logstash configuration (the names have been changed to protect the innocent)...
Change codec => plain to codec => json_lines so Logstash deserializes the JSON in each line. You'll probably want to change the codec for the sns output in the same way.
Please always state what happens rather than "it doesn't work" or similar.
So the JSON objects you want to parse and adorn with additional fields is broken across multiple lines? If yes, the file input is line-oriented so you need to use a multiline codec to join the lines of the file into a single event, then use a json filter to deserialize that field in the events. Examples of this have been posted in the past.
@Badger I have updated the input and output in my original post to reflect the actual input and actual output, respectively. @magnusbaeck sorry for just saying "that didnt work". What I meant was that I got the identical output that I had originally gotten.
Again, don't use the line codec. If you want to add fields to the input message you need to deserialize the input JSON into Logstash fields, otherwise the input is just a string stored in the message field. The fields should then be serialized back into a suitable format, e.g. JSON, using a json or json_lines codec.
I find it very hard to believe that you're getting the exact same results when using a json_lines codec in both the input and output plugin compared to when you were using a plain and a line codec.
So, please switch both input and outputs to one of the mentioned codecs and report the full output of the stdout plugin.
@magnusbaeck the config file doesnt run when i change both the input and the output codecs to json_lines as you suggested. However, if I use codec => json on the input side and codec => json_lines on the output side it does run but output is not what i am looking for. Please see below:
The problem with this is that I want the same order the message was in before I parsed it and I do not want all the other fields that are now in there. I cant move the fields around because there there are too many variables as to what the fields will be. I simply want to add a single <key, value> pair inside the json structure that was input to logstash and then send that out.
That's because you have that codec that appends that string to the field called message. You asked for a field call Message, which is a different field. Change the stdout codec to rubydebug and you will see what I mean.
@Badger I'm sorry for all this confusion. I am looking at a few outputs at different points in the messages life. I was looking at the output from SQS which is after SNS when I gave you that "Message" field. I am getting confused and confusing everyone else. I dont need an extra field I just want the output of logstash which is "message" to have the logstash_timestamp field within it.
I tried both methods proposed in the comments. I like the approach of pulling apart the json, then blacklisting the extra fields added by logstash, and finally packing it back into json in the output.
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.