influxdb-logger 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a108e31a9049f7e781a2e049c130c0f7453d38c0
4
- data.tar.gz: d15563795730d1197bcd81e6fa430913a7f923e6
3
+ metadata.gz: 4f2d46b592a8e518d7b066a8dc8097f311fffe14
4
+ data.tar.gz: 6adc6aa9db9821ca75b05548745b2776fc17b1db
5
5
  SHA512:
6
- metadata.gz: 072df960c685702015d306b87881b2527d4a1268250cccb7274824aa4c5b928676b6fd4a07a596c1f0f5486862ab6c40868f4e67e5e59393db38c8f6a621dc1e
7
- data.tar.gz: e31a48b3229760476d8d6119ec97f4ca39f88b445d4fa5b82c0f4da20691f2b78bda168718fbba3706cd9853f543b4751a0d49b9f86cb81834b1767d2eff5bed
6
+ metadata.gz: ab2d959f0ba12e9c0bc66942ec554a50b2e747fdfc5a4b9c4c08cb8573bf32506585b35e765d57fc7c4202915921be6f4f5215460141734e488ea913d249c2c8
7
+ data.tar.gz: 5242ee4f75b035a70588384c709f064a6b4af0f990e41030eadbdfb7fabda4e7c35c98362fb521fccae88cb57995d99aaf6937331ae3d613f9ec919760e791cf
data/CHANGELOG.md CHANGED
@@ -1,8 +1,3 @@
1
- ## 0.5.0 / April 29 2017
2
-
3
- * Rails 5.2
4
- * support nanosecond_precision(Support nanosecond precision when sending logs to Fluentd #43)*
5
-
6
1
  ## 0.4.0 / April 29 2017
7
2
 
8
3
  * Rails 5.1
@@ -41,11 +36,11 @@
41
36
 
42
37
  ## 0.1.5 / July 19 2014
43
38
 
44
- * Fix keynames of EVN['FLUENTD_URL']
39
+ * Fix keynames of EVN['INFLUXDB_URL']
45
40
 
46
41
  ## 0.1.4 / July 18 2014
47
42
 
48
- * Enable to use EVN['FLUENTD_URL']
43
+ * Enable to use EVN['INFLUXDB_URL']
49
44
 
50
45
  ## 0.1.3 / April 11 2014
51
46
 
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # Act::Fluent::Logger::Rails
1
+ # influxdb-logger (Rails)
2
2
 
3
- Fluent logger.
3
+ Log to Influxdb in Rails
4
4
 
5
5
  ## Supported versions
6
6
 
@@ -10,7 +10,7 @@ Fluent logger.
10
10
 
11
11
  Add this line to your application's Gemfile:
12
12
 
13
- gem 'influxdb-logger
13
+ gem 'influxdb-logger', '5.1.4'
14
14
 
15
15
  And then execute:
16
16
 
@@ -22,89 +22,64 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- in config/environments/production.rb
26
-
27
- config.log_level = :info
28
- config.logger = InfluxdbLogger::Logger.
29
- new(log_tags: {
30
- ip: :ip,
31
- ua: :user_agent,
32
- uid: ->(request) { request.session[:uid] }
33
- })
34
-
35
- Don't use config.log_tags.
36
-
37
- ### To define where to send messages to, either:
38
-
39
- #### create config/fluent-logger.yml
40
-
41
- development:
42
- fluent_host: '127.0.0.1'
43
- fluent_port: 24224
44
- tag: 'foo'
45
- messages_type: 'string'
46
- severity_key: 'level' # default severity
47
-
48
- test:
49
- fluent_host: '127.0.0.1'
50
- fluent_port: 24224
51
- tag: 'foo'
52
- messages_type: 'string'
53
- severity_key: 'level' # default severity
54
-
55
- production:
56
- fluent_host: '127.0.0.1'
57
- fluent_port: 24224
58
- tag: 'foo'
59
- messages_type: 'string'
60
- severity_key: 'level' # default severity
61
-
62
- #### set an environment variable FLUENTD_URL
63
-
64
- http://fluentd.example.com:42442/foo?messages_type=string&severity_key=level
65
-
66
- #### pass a settings object to InfluxdbLogger::Logger.new
67
-
68
- config.logger = InfluxdbLogger::Logger.
69
- new(settings: {
70
- host: '127.0.0.1',
71
- port: 24224,
72
- tag: 'foo',
73
- messages_type: 'string',
74
- severity_key: 'level'
75
- })
76
-
77
- ### Setting
78
-
79
- * fluent_host: The host name of Fluentd.
80
- * fluent_port: The port number of Fluentd.
81
- * tag: The tag of the Fluentd event.
82
- * messages_type: The type of log messages. 'string' or 'array'.
83
- If it is 'string', the log messages is a String.
84
- ```
85
- 2013-01-18T15:04:50+09:00 foo {"messages":"Started GET \"/\" for 127.0.0.1 at 2013-01-18 15:04:49 +0900\nProcessing by TopController#index as HTML\nCompleted 200 OK in 635ms (Views: 479.3ms | ActiveRecord: 39.6ms)"],"severity":"INFO"}
86
- ```
87
- If it is 'array', the log messages is an Array.
88
- ```
89
- 2013-01-18T15:04:50+09:00 foo {"messages":["Started GET \"/\" for 127.0.0.1 at 2013-01-18 15:04:49 +0900","Processing by TopController#index as HTML","Completed 200 OK in 635ms (Views: 479.3ms | ActiveRecord: 39.6ms)"],"severity":"INFO"}
25
+ In `config/environments/production.rb`(`test.rb`, `development.rb`)
26
+
27
+ ```ruby
28
+ config.logger = InfluxdbLogger::Logger.new(log_tags: {}, ...)
29
+
90
30
  ```
91
- * severity_key: The key of severity(DEBUG, INFO, WARN, ERROR).
92
31
 
93
- You can add any tags at run time.
32
+ Supported parameters for `InfluxdbLogger::Logger.new`:
94
33
 
95
- logger[:foo] = "foo value"
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:
96
36
 
97
- ### Usage as a standalone logger
37
+ ```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: ...})
98
45
 
99
- Typical usage is as a replacement for the default Rails logger, in which case
100
- messages are collected and flushed automatically as part of the request
101
- lifecycle. If you wish to use it instead as a separate logger and log to it
102
- manually then it is necessary to initialize with the `flush_immediately` flag.
46
+ ```
103
47
 
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).
104
49
  ```ruby
105
- InfluxdbLogger::Logger.new(flush_immediately: true)
106
- ```
107
-
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
+ })
59
+ ```
60
+
61
+ * `batch_size`, `interval`: Since logging is a high frequncy job for any application with large user base in production environment. These two parameters
62
+ give a chance for the logger to batch logging actions.
63
+
64
+ 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
+
66
+ ```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
+ })
82
+ ```
108
83
 
109
84
  ## Contributing
110
85
 
@@ -11,6 +11,11 @@ class Time
11
11
  end
12
12
 
13
13
  module InfluxdbLogger
14
+ def log_to_file(message) # for test
15
+ open("#{Rails.root}/log/my.log", 'w') { |f|
16
+ f.puts message.inspect
17
+ }
18
+ end
14
19
 
15
20
  module Logger
16
21
 
@@ -31,41 +36,35 @@ module InfluxdbLogger
31
36
  end
32
37
  end
33
38
  end
34
- if (0 == settings.length)
35
- fluent_config = if ENV["FLUENTD_URL"]
36
- self.parse_url(ENV["FLUENTD_URL"])
37
- end
38
- settings = {
39
- tag: fluent_config['tag'],
40
- host: fluent_config['fluent_host'],
41
- port: fluent_config['fluent_port'],
42
- nanosecond_precision: fluent_config['nanosecond_precision'],
43
- messages_type: fluent_config['messages_type'],
44
- severity_key: fluent_config['severity_key'],
45
- }
39
+
40
+ if ENV["INFLUXDB_URL"]
41
+ settings = self.parse_url(ENV["INFLUXDB_URL"]).merge(settings)
46
42
  end
47
43
 
48
44
  settings[:batch_size] ||= batch_size
49
45
  settings[:interval] ||= interval
50
46
 
51
47
  level = SEV_LABEL.index(Rails.application.config.log_level.to_s.upcase)
52
- logger = InfluxdbLogger::FluentLogger.new(settings, level, log_tags)
48
+ logger = InfluxdbLogger::InnerLogger.new(settings, level, log_tags)
53
49
  logger = ActiveSupport::TaggedLogging.new(logger)
54
50
  logger.extend self
55
51
  end
56
52
 
57
- def self.parse_url(fluentd_url)
58
- uri = URI.parse fluentd_url
53
+ def self.parse_url(influxdb_url)
54
+ uri = URI.parse influxdb_url
59
55
  params = CGI.parse uri.query
60
-
61
56
  {
62
- fluent_host: uri.host,
63
- fluent_port: uri.port,
64
- tag: uri.path[1..-1],
65
- nanosecond_precision: params['nanosecond_precision'].try(:first),
57
+ database: uri.path[1..-1],
58
+ host: uri.host,
59
+ port: uri.port,
66
60
  messages_type: params['messages_type'].try(:first),
67
61
  severity_key: params['severity_key'].try(:first),
68
- }.stringify_keys
62
+ username: params['username'].try(:first),
63
+ password: params['password'].try(:first),
64
+ series: params['series'].try(:first),
65
+ time_precision: params['time_precision'].try(:first),
66
+ retry: params['retry'].try(:first).to_i
67
+ }
69
68
  end
70
69
 
71
70
  def tagged(*tags)
@@ -76,7 +75,7 @@ module InfluxdbLogger
76
75
  end
77
76
  end
78
77
 
79
- class FluentLogger < ActiveSupport::Logger
78
+ class InnerLogger < ActiveSupport::Logger
80
79
  def initialize(options, level, log_tags)
81
80
  self.level = level
82
81
  @messages_type = (options[:messages_type] || :array).to_sym
@@ -192,18 +191,14 @@ module InfluxdbLogger
192
191
 
193
192
  def flush
194
193
  return if @messages.empty?
195
- # test switch
196
- # open("#{Rails.root}/log/my.log", 'w') { |f|
197
- # f.puts @messages
198
- # }
199
194
  @influxdb_logger.write_points(@messages)
200
195
  @severity = 0
201
196
  @messages.clear
202
197
  @last_flush_time = Time.now.to_ms
198
+ @tags = nil
203
199
  end
204
200
 
205
201
  def close
206
- # @fluent_logger.close
207
202
  end
208
203
 
209
204
  def level
@@ -219,4 +214,3 @@ module InfluxdbLogger
219
214
  end
220
215
  end
221
216
  end
222
-
@@ -1,3 +1,3 @@
1
1
  module InfluxdbLogger
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
data/spec/logger_spec.rb CHANGED
@@ -7,6 +7,12 @@ class Time
7
7
  end
8
8
  end
9
9
 
10
+ module InfluxdbLogger
11
+ module Logger
12
+ attr_accessor :settings
13
+ end
14
+ end
15
+
10
16
  describe InfluxdbLogger::Logger do
11
17
  before do
12
18
  stub_const('Rails', Class.new) unless defined?(Rails)
@@ -16,6 +22,10 @@ describe InfluxdbLogger::Logger do
16
22
 
17
23
  class MyLogger
18
24
  attr_accessor :log
25
+ def initialize()
26
+ @log = []
27
+ end
28
+
19
29
  def post(tag, map)
20
30
  end
21
31
 
@@ -27,7 +37,6 @@ describe InfluxdbLogger::Logger do
27
37
  end
28
38
 
29
39
  def write_point(point)
30
- @log ||= []
31
40
  @log << point
32
41
  end
33
42
 
@@ -179,24 +188,43 @@ describe InfluxdbLogger::Logger do
179
188
  end
180
189
  end
181
190
 
182
- describe "use ENV['FLUENTD_URL']" do
183
- let(:fluentd_url) { "http://fluentd.example.com:42442/hoge?messages_type=string&severity_key=level" }
184
-
185
- describe ".parse_url" do
186
- subject { described_class.parse_url(fluentd_url) }
187
- it { expect(subject['tag']).to eq 'hoge' }
188
- it { expect(subject['fluent_host']).to eq 'fluentd.example.com' }
189
- it { expect(subject['fluent_port']).to eq 42442 }
190
- it { expect(subject['messages_type']).to eq 'string' }
191
- it { expect(subject['severity_key']).to eq 'level' }
191
+ describe "settings with ENV['INDUX_DB_URL']" do
192
+ let(:influxdb_url) { 'http://influxdb:8086/rallets?messages_type=string&severity_key=level&username=user&password=pass&time_precision=ms&series=Log&retry=3' }
193
+
194
+ it "settings are properly set" do
195
+ stub_const("ENV", {'INFLUXDB_URL' => influxdb_url })
196
+ allow(InfluxdbLogger::InnerLogger).to receive(:new) do |settings|
197
+ expect(settings).to eq({
198
+ database: "rallets",
199
+ host: "influxdb",
200
+ port: 8086,
201
+ messages_type: "string",
202
+ severity_key: "level",
203
+ username: "user",
204
+ password: "pass",
205
+ series: "Log",
206
+ time_precision: "ms",
207
+ retry: 3,
208
+ batch_size: 1000,
209
+ interval: 1000
210
+ })
211
+ ActiveSupport::Logger.new(STDOUT)
212
+ end
213
+ InfluxdbLogger::Logger.new(settings: {})
192
214
  end
193
215
  end
194
216
 
195
- describe 'batch size' do
196
- it 'works well with batch size' do
197
- # logger = InfluxdbLogger::Logger.new(settings: settings, batch_size: 2))
198
- # logger.info('Immediately!')
199
- # expect(@my_logger.log).to eq(nil)
217
+ describe 'batch size', now: true do
218
+ it 'inner logger write_points after every two messages if batch_size is set to 2' do
219
+ logger = InfluxdbLogger::Logger.new(settings: settings, batch_size: 2)
220
+ logger.info('message 1')
221
+ expect(@my_logger.log.size).to eq 0
222
+ logger.info('message 2')
223
+ expect(@my_logger.log.size).to eq 2
224
+ logger.info('message 3')
225
+ expect(@my_logger.log.size).to eq 2
226
+ logger.info('message 4')
227
+ expect(@my_logger.log.size).to eq 4
200
228
  end
201
229
  end
202
230
  end
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.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rallets
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-25 00:00:00.000000000 Z
11
+ date: 2018-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec