fluent/fluent-plugin-mongo

Get Mongo::Error::BulkWriteError when collect rails logs.

zw963 opened this issue · 0 comments

zw963 commented

Hi, i try to rails with fluent-plugin-mongo, but get some issue

Following is my config:

Gemfile

# Gemfile
gem 'fluentd'
gem 'act-fluent-logger-rails'
gem 'lograge'
gem 'fluent-plugin-mongo'

Gemfile version

 ╰─ $ cat Gemfile.lock |grep fluent
    act-fluent-logger-rails (0.5.0)
    fluent-logger (0.8.1)
    fluent-plugin-mongo (1.3.0)
      fluentd (>= 0.14.22, < 2)
    fluentd (1.6.0)

acts-fluent-logger-rails config

# config/fluent-logger.yml
production:
  fluent_host:   '152.32.134.198'
  fluent_port:   24224
  tag:           mongo.app1
  messages_type: 'string'

fluentd server config

# fluentd server config
<source>
  @type forward
  # port 24224 # this is default
  @label @raw
  tag mongo.airhost
</source>

<label @raw>
  # <filter>
  #   @type parser
  #   key_name messages
  #   <parse>
  #     @type json
  #   </parse>
  # </filter>

  # <filter>
    #   @type detect_exceptions3
    #   # remove_tag_prefix foo
    #   # message log
    #   languages ruby
    #   # multiline_flush_interval 0.1
    # </filter>

  <match>
    @type relabel
    @label @rails
  </match>
</label>

<label @rails>
  # <match **>
  #   @type file
  #     path /var/log/fluentd/fluent
  #   # time_slice_format %Y%m%d
  #   # time_slice_wait 10s
  #   # time_format %Y-%m-%dT%H:%M:%S.%L
  #   # time_key sent_at
  # </match>

  <match mongo.**>
    @type mongo
    database airhost
    collection fluentd
    capped
    capped_size 100M
    host my_host
    port 27017
    user fluentd
    remove_tag_prefix mongo.
    replace_dot_in_key_with __dot__
    replace_dollar_in_key_with __dollar__
    password my_password
    <buffer>
      flush_interval 1s
    </buffer>
    <inject>
      time_key time
    </inject>
  </match>
</label>

Rails production config

# config/environments/production.rb

config.logger = ActFluentLoggerRails::Logger.new

  config.lograge.keep_original_rails_log = true
  config.lograge.logger = ActiveSupport::Logger.new "#{Rails.root}/log/lograge_#{Rails.env}.log"

  config.lograge.enabled = true
config.lograge.formatter = ->(data) do
    {
      'action' => data.dig(:action)
    }.to_json
  end
  config.lograge.custom_options = lambda do |event|
    {
      exception: event.payload[:exception],
      exception_object: event.payload[:exception_object],
      time: Time.now
    }

The error.

After some debug, i see get records like following:

2.6.1 :009 > records
 => [{"messages"=>"Started GET \"/posts\" for 114.112.255.34 at 2019-07-10 12:08:30 +0800\nProcessing by PostsController#index as HTML\n  Rendering posts/index.html.erb within layouts/application\n  Rendered posts/index.html.erb within layouts/application (3.7ms)\nCompleted 200 OK in 5ms (Views: 3.8ms | ActiveRecord: 0.7ms)", "severity"=>"INFO", "time"=>2019-07-10 12:08:30 +0800}, {"messages"=>"Started GET \"/posts\" for 114.112.255.34 at 2019-07-10 12:08:31 +0800\nProcessing by PostsController#index as HTML\n  Rendering posts/index.html.erb within layouts/application\n  Rendered posts/index.html.erb within layouts/application (2.9ms)\nCompleted 200 OK in 4ms (Views: 3.0ms | ActiveRecord: 0.7ms)", "severity"=>"INFO", "time"=>2019-07-10 12:08:31 +0800}, {"messages"=>"Started GET \"/posts\" for 114.112.255.34 at 2019-07-10 12:08:32 +0800\nProcessing by PostsController#index as HTML\n  Rendering posts/index.html.erb within layouts/application\n  Rendered posts/index.html.erb within layouts/application (3.6ms)\nCompleted 200 OK in 5ms (Views: 3.3ms | ActiveRecord: 1.1ms)", "severity"=>"INFO", "time"=>2019-07-10 12:08:32 +0800}] 

when run get_collection(database, collection, @collection_options).insert_many(records),
get following backtrace:

Traceback (most recent call last):
       14: from /home/deployer/apps/test_fluentd/test_fluentd_app2/shared/bundle/ruby/2.6.0/gems/fluentd-1.6.0/lib/fluent/plugin_helper/thread.rb:78:in `block in thread_create'
       13: from /home/deployer/apps/test_fluentd/test_fluentd_app2/shared/bundle/ruby/2.6.0/gems/fluentd-1.6.0/lib/fluent/plugin/output.rb:456:in `block (2 levels) in start'
       12: from /home/deployer/apps/test_fluentd/test_fluentd_app2/shared/bundle/ruby/2.6.0/gems/fluentd-1.6.0/lib/fluent/plugin/output.rb:1433:in `flush_thread_run'
       11: from /home/deployer/apps/test_fluentd/test_fluentd_app2/shared/bundle/ruby/2.6.0/gems/fluentd-1.6.0/lib/fluent/plugin/output.rb:1127:in `try_flush'
       10: from /home/deployer/apps/test_fluentd/test_fluentd_app2/shared/bundle/ruby/2.6.0/gems/fluent-plugin-mongo-1.3.0/lib/fluent/plugin/out_mongo.rb:182:in `write'
        9: from /home/deployer/apps/test_fluentd/test_fluentd_app2/shared/bundle/ruby/2.6.0/gems/fluent-plugin-mongo-1.3.0/lib/fluent/plugin/out_mongo.rb:269:in `operate'
        8: from <internal:prelude>:145:in `irb'
        7: from (irb):12:in `operate'
        6: from (irb):12:in `rescue in operate'
        5: from /home/deployer/apps/test_fluentd/test_fluentd_app2/shared/bundle/ruby/2.6.0/gems/mongo-2.6.4/lib/mongo/collection.rb:509:in `insert_many'
        4: from /home/deployer/apps/test_fluentd/test_fluentd_app2/shared/bundle/ruby/2.6.0/gems/mongo-2.6.4/lib/mongo/collection.rb:532:in `bulk_write'
        3: from /home/deployer/apps/test_fluentd/test_fluentd_app2/shared/bundle/ruby/2.6.0/gems/mongo-2.6.4/lib/mongo/bulk_write.rb:84:in `execute'
        2: from /home/deployer/apps/test_fluentd/test_fluentd_app2/shared/bundle/ruby/2.6.0/gems/mongo-2.6.4/lib/mongo/bulk_write/result_combiner.rb:73:in `result'
        1: from /home/deployer/apps/test_fluentd/test_fluentd_app2/shared/bundle/ruby/2.6.0/gems/mongo-2.6.4/lib/mongo/bulk_write/result.rb:184:in `validate!'
Mongo::Error::BulkWriteError (Mongo::Error::BulkWriteError)

Thank you!