Search code examples
springelasticsearchopenshiftfluentd

FluentD fails to write log in Elasticsearch


Using:

  • fluentd 1.11.2
  • fluent-plugin-elasticsearch 4.1.3
  • elasticsearch 7.5.1
  • springboot 2.3.3

Running in Openshift (Kubernetes v1.17.1+20ba474).

Both Fluentd and Elasticsearch are running in different pods.

Fluentd configuration file:

<source>
  @type forward
  port 24224
  bind 0.0.0.0
</source>
<filter *.**>
      @type parser
      key_name log
      reserve_data true
      <parse>
        @type none
      </parse>
</filter>
<match *.**>
  @type copy
<store>
    @type elasticsearch
    host elasticdb
    port 9200
    logstash_format true
    logstash_prefix applogs
    logstash_dateformat %Y%m%d
    include_tag_key true
    type_name app_log
    tag_key @log_name
    flush_interval 1s
    user elastic
    password changeme
  </store>
  <store>
    @type stdout
  </store>
</match>

From a local springboot service, I am sending to fluentd some dummy data:

// Local port 24224 is being forwarded to remote 24224 via oc port-forward command
private static FluentLogger LOG = FluentLogger.getLogger("app", "127.0.0.1", 24224);

Map<String, Object> data = new HashMap<String, Object>();
data.put("from", "userA");
data.put("to", "userB");

LOG.log("app", data);

which sends this piece of JSON data:

{"from":"userA","to":"userB"}

It is only working one out of ten times, apparently. Or seems to work two or three times and then brokes until I change the index. Not clear a pattern of behavior, actually.

When it does not work (most of the times), these are the logs in the fluentd pod:

2020-09-18 17:33:08.000000000 +0000 app.appaa: {"from":"userA","to":"userB"}
2020-09-18 17:33:37 +0000 [warn]: #0 dump an error event: error_class=ArgumentError error="log does not exist" location=nil tag="fluent.warn" time=2020-09-18 17:33:37.328180192 +0000 record={"error"=>"#<ArgumentError: log does not exist>", "location"=>nil, "tag"=>"app.appaa", "time"=>1600450388, "record"=>{"from"=>"userA", "to"=>"userB"}, "message"=>"dump an error event: error_class=ArgumentError error=\"log does not exist\" location=nil tag=\"app.appaa\" time=1600450388 record={\"from\"=>\"userAa\", \"to\"=>\"userBb\"}"}
2020-09-18 17:33:37.328180192 +0000 fluent.warn: {"error":"#<ArgumentError: log does not exist>","location":null,"tag":"app.appaa","time":1600450388,"record":{"from":"userA","to":"userB"},"message":"dump an error event: error_class=ArgumentError error=\"log does not exist\" location=nil tag=\"app.appaa\" time=1600450388 record={\"from\"=>\"userA\", \"to\"=>\"userB\"}"}
warning: 299 Elasticsearch-7.5.1-3ae9ac9a93c95bd0cdc054951cf95d88e1e18d96 "[types removal] Specifying types in bulk requests is deprecated."

Althoug the Elasticsearch pod does not show anything (I guess a matter of logging level), if I go to Elastic, I see this:

{
    "_index": "applogs-20200918",
    "_type": "_doc",
    "_id": "F0M2onQBB89nIri4Cb1Z",
    "_score": 1.0,
    "_source": {
        "error": "#<ArgumentError: log does not exist>",
        "location": null,
        "tag": "app.app",
        "time": 1600449251,
        "record": {
            "from": "userA",
            "to": "userB"
        },
        "message": "dump an error event: error_class=ArgumentError error=\"log does not exist\" location=nil tag=\"app.app\" time=1600449251 record={\"from\"=>\"userA\", \"to\"=>\"userB\"}",
        "@timestamp": "2020-09-18T17:14:39.775332214+00:00",
        "@log_name": "fluent.warn"
    }
}

So it looks like the error comes from

"Elastic: Argument Error: Log does not exist"

Did anyone face this error before?


Solution

  • The configuration of the parser in the filter i.e.

    <filter *.**>
      @type parser
      key_name log    # << Look for key `log` in event
      # ...
    </filter>
    

    is looking for the key log which doesn't exist in this event:

    {"from":"userA","to":"userB"}
    

    You need to use something like this:

    {"log":"... your log here..."}
    

    You might need to escape " in there if you use quotations.

    Relevant documentation: https://docs.fluentd.org/filter/parser#key_name