Logger for Influxdb in Rails
- Rails 4 and 5
Add this line to your application's Gemfile:
gem 'influxdb-logger', '2.0.0'
And then execute:
$ bundle
Or install it yourself as:
$ gem install influxdb-logger
In config/environments/production.rb
(test.rb
, development.rb
)
config.logger = InfluxdbLogger::Logger.new(influxdb_tags: ... tags: ... settings: ... batch_size: ..., interval: ..., async: ...)
By default, influxdb-logger will log
duration, db, format, location, message, message_type, method, params, path, severity, status, view
as fields into specified
series.
Which means, your influxdb-logger
is good to go with configuration only about how to talk to influxdb:
config.logger = InfluxdbLogger::Logger.new(settings: {
database: ENV['INFLUXDB_DB_NAME'],
series: ENV['INFLUXDB_SERIES'],
username: ENV['INFLUXDB_USER'],
password: ENV['INFLUXDB_USER_PASSWORD']
})
-
influxdb_tags
[Array]: This argument specifies tag-set of series. If we need to constantly checkout influxdb logs about specificcontroller
oraction
, the best way is to tag both fields to speed up any query on them utilizinginfluxdb_tags
:config.logger = InfluxdbLogger::Logger.new(infludb_tags: [:controller, :action], settings: ...)
-
tags
[Hash]: If extra fields are required to be sent to influxdb,tags
could be utilized, e.g., ip info of agents:config.logger = InfluxdbLogger::Logger.new(tags: { remote_ip: -> request { request.remote_ip } }, settings: ...)
Passed
tags
can be aHash
consisting values of any basic ruby type or alambda
. -
settings
: Which defines how ourinfluxdb-logger
connects to influxdb database. Detailed doc about it is here: influxdb-ruby.InfluxdbLogger::Logger.new(settings: { host: 'influxdb', retry: 3, time_precision: 'ms', database: ENV['INFLUXDB_DB_NAME'], series: ENV['INFLUXDB_SERIES'], username: ENV['INFLUXDB_USER'], password: ENV['INFLUXDB_USER_PASSWORD'] })
-
batch_size
,interval
: Since logging is a high frequncy job for any application with large user base in production environment. These two parameters give a chance for the logger to batch logging actions.For example, you can tell the logger to log when size of logging actions hits
1000
or that the last logging action is1000
ms later than the first one in the queue by:InfluxdbLogger::Logger.new(batch_size: 1000, interval: 1000, settings: ...)
-
async
: Determines whether the logger write asynchronously to influxdb, default tofalse
. Read code here to know how it works.InfluxdbLogger::Logger.new(async: false, settings: ...)
MIT
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request