influxdb-logger 1.0.3 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8b05d8b6706111f2fca5db10879f26ce106aa81b
4
- data.tar.gz: f486822bc9d4c99f7837c8dfc6d0ee760c240e48
3
+ metadata.gz: e56aa8636f81b07f6f8ba75b534ac4165b190ef1
4
+ data.tar.gz: bc744b3cb2fd91cc6ebaa3735ab5d02bc1252ffc
5
5
  SHA512:
6
- metadata.gz: e438029412859abae4811a40d1982ff86f24f8faa762f37f4f653666109dbeea1130635146a5a6df6254870267345ffe862d04255f7616464efd36ae0463abf4
7
- data.tar.gz: a46e990537873512fa206aa0de0de1eff7121fc0db29b2f86c189784e9fe0fc3038fb9b7e5f1a2508aa15c8c6152d24e5d69e938440b61434cd8b341e2b85630
6
+ metadata.gz: 9f1fc696deec683e26159b0993771d61762cbf372640ec6c082852905dff18f956e147fb84bb29aa5d5f52cba66861be129a15cb04b3201108cc45b18caceb4e
7
+ data.tar.gz: 16f960d5c0315cf0bc24762974611811fad31faebe69b3e8efdfa98f216a52ee6ccd14984093b6bdf2d824c050a9cf1ef7eff7fa4f04c3faaf6df6dc99b586a5
data/README.md CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  Add this line to your application's Gemfile:
12
12
 
13
- gem 'influxdb-logger', '1.0.1'
13
+ gem 'influxdb-logger', '2.0.0'
14
14
 
15
15
  And then execute:
16
16
 
@@ -20,42 +20,56 @@ Or install it yourself as:
20
20
 
21
21
  $ gem install influxdb-logger
22
22
 
23
- ## Usage
23
+ ## Basic Usage
24
24
 
25
25
  In `config/environments/production.rb`(`test.rb`, `development.rb`)
26
26
 
27
27
  ```ruby
28
- config.logger = InfluxdbLogger::Logger.new(log_tags: {}, ...)
28
+ config.logger = InfluxdbLogger::Logger.new(influxdb_tags: ... tags: ... settings: ... batch_size: ..., interval: ..., async: ...)
29
29
 
30
30
  ```
31
31
 
32
- Supported parameters for `InfluxdbLogger::Logger.new`:
32
+ By default, influxdb-logger will log
33
+ `duration, db, format, location, message, message_type, method, params, path, severity, status, view` as [fields](https://docs.influxdata.com/influxdb/v1.7/concepts/key_concepts/#field-key) into specified
34
+ [series](https://docs.influxdata.com/influxdb/v1.7/concepts/key_concepts/#series).
33
35
 
34
- * `log_tags`: tags which you want the created logger to pass into influxdb, for example,
35
- you could log **ip**, **user agent**, **device_type**(through requested parameters) and **version** with the following setup:
36
+ Which means, your `influxdb-logger` is good to go with configuration only about how to talk to influxdb:
37
+ ```ruby
38
+ config.logger = InfluxdbLogger::Logger.new(settings: {
39
+ database: ENV['INFLUXDB_DB_NAME'],
40
+ series: ENV['INFLUXDB_SERIES'],
41
+ username: ENV['INFLUXDB_USER'],
42
+ password: ENV['INFLUXDB_USER_PASSWORD']
43
+ })
44
+ ```
36
45
 
46
+ ## Advanced Usage
47
+
48
+ * `influxdb_tags`[Array]: This argument specifies [tag-set](https://docs.influxdata.com/influxdb/v1.7/concepts/key_concepts/#tag-set) of series.
49
+ If we need to constantly checkout influxdb logs about specific `controller` or `action`, the best way is to tag both fields to speed up any query on them utilizing `influxdb_tags`:
37
50
  ```ruby
38
- config.logger = InfluxdbLogger::Logger.new(log_tags: {
39
- ip: :ip,
40
- ua: :user_agent,
41
- device_type: -> request { request.params[:DEVICE_TYPE] },
42
- version: -> request { request.params[:VERSION] },
43
- session_id: -> request { request.params[:session_id] }
44
- }, settings: ...})
51
+ config.logger = InfluxdbLogger::Logger.new(infludb_tags: [:controller, :action], settings: ...)
52
+ ```
45
53
 
54
+ * `tags`[Hash]: If extra fields are required to be sent to influxdb, `tags` could be utilized, e.g., ip info of agents:
55
+ ```ruby
56
+ config.logger = InfluxdbLogger::Logger.new(tags: {
57
+ remote_ip: -> request { request.remote_ip }
58
+ }, settings: ...)
46
59
  ```
60
+ Passed `tags` can be a `Hash` consisting values of any basic ruby type or a `lambda`.
47
61
 
48
- * `settings`: which defines how the logger would connect to influxdb database. More detail about it could found in [influxdb-ruby](https://github.com/influxdata/influxdb-ruby).
49
- ```ruby
50
- InfluxdbLogger::Logger.new(settings: {
51
- host: 'influxdb',
52
- database: 'rallets',
53
- series: series,
54
- retry: 3,
55
- username: 'user',
56
- password: 'password',
57
- time_precision: 'ms'
58
- })
62
+ * `settings`: Which defines how our `influxdb-logger` connects to influxdb database. Detailed doc about it is here: [influxdb-ruby](https://github.com/influxdata/influxdb-ruby).
63
+ ```ruby
64
+ InfluxdbLogger::Logger.new(settings: {
65
+ host: 'influxdb',
66
+ retry: 3,
67
+ time_precision: 'ms',
68
+ database: ENV['INFLUXDB_DB_NAME'],
69
+ series: ENV['INFLUXDB_SERIES'],
70
+ username: ENV['INFLUXDB_USER'],
71
+ password: ENV['INFLUXDB_USER_PASSWORD']
72
+ })
59
73
  ```
60
74
 
61
75
  * `batch_size`, `interval`: Since logging is a high frequncy job for any application with large user base in production environment. These two parameters
@@ -64,28 +78,17 @@ Supported parameters for `InfluxdbLogger::Logger.new`:
64
78
  For example, you can tell the logger to log when size of logging actions hits `1000` or that the last logging action is `1000`ms later than the first one in the queue by:
65
79
 
66
80
  ```ruby
67
- InfluxdbLogger::Logger.new(batch_size: 1000, interval: 1000, log_tags: {
68
- ip: :ip,
69
- ua: :user_agent,
70
- device_type: -> request { request.params[:DEVICE_TYPE] },
71
- version: -> request { request.params[:VERSION] },
72
- session_id: -> request { request.params[:session_id] }
73
- }, settings: {
74
- host: 'influxdb',
75
- database: 'rallets',
76
- series: series,
77
- retry: 3,
78
- username: 'user',
79
- password: 'password',
80
- time_precision: 'ms'
81
- })
81
+ InfluxdbLogger::Logger.new(batch_size: 1000, interval: 1000, settings: ...)
82
82
  ```
83
83
 
84
84
  * `async`: Determines whether the logger write asynchronously to influxdb, default to `false`. Read code [here](https://github.com/influxdata/influxdb-ruby/blob/master/lib/influxdb/writer/async.rb#L48) to know how it works.
85
85
  ```ruby
86
- InfluxdbLogger::Logger.new(async: false, ...)
86
+ InfluxdbLogger::Logger.new(async: false, settings: ...)
87
87
  ```
88
88
 
89
+ ## License
90
+
91
+ MIT
89
92
 
90
93
  ## Contributing
91
94
 
@@ -44,11 +44,11 @@ module InfluxdbLogger
44
44
  # Severity label for logging. (max 5 char)
45
45
  SEV_LABEL = %w(DEBUG INFO WARN ERROR FATAL ANY)
46
46
 
47
- def self.new(async: true, tags: {}, fields: {}, settings: {}, batch_size: 1000, interval: 1000)
48
- all_tags = tags.values + fields.values
49
- Rails.application.config.log_tags = all_tags
47
+ def self.new(influxdb_tags: [], tags: {}, settings: {}, batch_size: 1000, interval: 1000, async: true)
48
+ log_tags = tags.values
49
+ Rails.application.config.log_tags = log_tags
50
50
  if Rails.application.config.respond_to?(:action_cable)
51
- Rails.application.config.action_cable.log_tags = all_tags.map do |x|
51
+ Rails.application.config.action_cable.log_tags = log_tags.map do |x|
52
52
  case
53
53
  when x.respond_to?(:call)
54
54
  x
@@ -69,8 +69,8 @@ module InfluxdbLogger
69
69
  settings[:async] = async
70
70
 
71
71
  level = SEV_LABEL.index(Rails.application.config.log_level.to_s.upcase)
72
- logger = InfluxdbLogger::InnerLogger.new(settings, level, tags, fields)
73
- logger = ActiveSupport::TaggedLogging.new(logger)
72
+ inner_logger = InfluxdbLogger::InnerLogger.new(settings, level, tags, influxdb_tags)
73
+ logger = ActiveSupport::TaggedLogging.new(inner_logger)
74
74
  logger.extend self
75
75
  end
76
76
 
@@ -100,7 +100,7 @@ module InfluxdbLogger
100
100
  end
101
101
 
102
102
  class InnerLogger < ActiveSupport::Logger
103
- def initialize(options, level, tags, fields)
103
+ def initialize(options, level, initialized_tags, influxdb_tags)
104
104
  self.level = level
105
105
  @messages_type = (options[:messages_type] || :array).to_sym
106
106
  @tag = options[:tag]
@@ -115,15 +115,15 @@ module InfluxdbLogger
115
115
  @time_precision = options[:time_precision] || 'ns'
116
116
 
117
117
  @influxdb_logger = InfluxDB::Client.new(
118
- **options.slice(:host, :database, :retry, :username, :password, :async),
118
+ **options.slice(:host, :port, :database, :retry, :username, :password, :async),
119
119
  time_precision: @time_precision,
120
120
  discard_write_errors: true
121
121
  )
122
122
 
123
+ @influxdb_tags = influxdb_tags
123
124
  @severity = 0
124
125
  @messages = []
125
- @all_tags = tags.merge(fields)
126
- @fields = fields
126
+ @initialized_tags = initialized_tags
127
127
  after_initialize if respond_to? :after_initialize
128
128
  end
129
129
 
@@ -181,19 +181,16 @@ module InfluxdbLogger
181
181
  }
182
182
  end
183
183
 
184
- tags = @global_tags.clone
185
-
186
184
  if @tags
187
- @all_tags.keys.zip(@tags).each do |k, v|
188
- tags[k] = v
185
+ @initialized_tags.keys.zip(@tags).each do |k, v|
186
+ values[k] = v
189
187
  end
190
188
  end
191
-
192
189
  message = {
193
190
  series: @series,
194
191
  timestamp: Time.now.to_precision(@time_precision),
195
- tags: tags.except(@fields.keys),
196
- values: values.merge(tags.slice(@fields.keys)).merge({
192
+ tags: values.slice(*@influxdb_tags).merge(@global_tags),
193
+ values: values.except(*@influxdb_tags).merge({
197
194
  severity: format_severity(@severity)
198
195
  }).transform_values {|value|
199
196
  case value
@@ -1,3 +1,3 @@
1
1
  module InfluxdbLogger
2
- VERSION = "1.0.3"
2
+ VERSION = "2.0.0"
3
3
  end
data/spec/logger_spec.rb CHANGED
@@ -51,6 +51,10 @@ describe InfluxdbLogger::Logger do
51
51
 
52
52
  let(:series) { 'Request' }
53
53
 
54
+ let(:influxdb_tags) {
55
+ [:uuid, :foo]
56
+ }
57
+
54
58
  let(:tags) {
55
59
  {
56
60
  uuid: :uuid,
@@ -71,7 +75,7 @@ describe InfluxdbLogger::Logger do
71
75
  }
72
76
 
73
77
  let(:logger) {
74
- InfluxdbLogger::Logger.new(tags: tags, settings: settings)
78
+ InfluxdbLogger::Logger.new(influxdb_tags: influxdb_tags, tags: tags, settings: settings)
75
79
  }
76
80
 
77
81
  let(:request) {
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: influxdb-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rallets
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-22 00:00:00.000000000 Z
11
+ date: 2019-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -125,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
125
  version: '0'
126
126
  requirements: []
127
127
  rubyforge_project:
128
- rubygems_version: 2.6.14
128
+ rubygems_version: 2.5.2.3
129
129
  signing_key:
130
130
  specification_version: 4
131
131
  summary: Influxdb logger