influxdb-logger 1.0.1 → 1.0.2

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: 4f2d46b592a8e518d7b066a8dc8097f311fffe14
4
- data.tar.gz: 6adc6aa9db9821ca75b05548745b2776fc17b1db
3
+ metadata.gz: cd80eaf9eda418f4e29e848e43c8fdc27af5313d
4
+ data.tar.gz: aea9e2e75e2cf55dad07ed6c7416c78f0eec351b
5
5
  SHA512:
6
- metadata.gz: ab2d959f0ba12e9c0bc66942ec554a50b2e747fdfc5a4b9c4c08cb8573bf32506585b35e765d57fc7c4202915921be6f4f5215460141734e488ea913d249c2c8
7
- data.tar.gz: 5242ee4f75b035a70588384c709f064a6b4af0f990e41030eadbdfb7fabda4e7c35c98362fb521fccae88cb57995d99aaf6937331ae3d613f9ec919760e791cf
6
+ metadata.gz: ebdebe6c27c0cd8c7165fdf71e42f5fc416aa6573d85129ee2ed72734ed9eba0fd7e09cb8f368912859524b009d5251ae3470a102d9e239bfa7110d06c3b82c4
7
+ data.tar.gz: b9014899996b8f54788062748c91f1171ab290a63b674c222f67b87a344c1755c4f6be31bef47e406e08c7c7d2d53bb3a7e1207d79c3ae78b6952beedda45edb
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # influxdb-logger (Rails)
2
2
 
3
- Log to Influxdb in Rails
3
+ **Logger** for **Influxdb** in **Rails**
4
4
 
5
5
  ## Supported versions
6
6
 
@@ -10,7 +10,7 @@ Log to Influxdb in Rails
10
10
 
11
11
  Add this line to your application's Gemfile:
12
12
 
13
- gem 'influxdb-logger', '5.1.4'
13
+ gem 'influxdb-logger', '1.0.1'
14
14
 
15
15
  And then execute:
16
16
 
@@ -81,6 +81,12 @@ Supported parameters for `InfluxdbLogger::Logger.new`:
81
81
  })
82
82
  ```
83
83
 
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
+ ```ruby
86
+ InfluxdbLogger::Logger.new(async: false, ...)
87
+ ```
88
+
89
+
84
90
  ## Contributing
85
91
 
86
92
  1. Fork it
@@ -5,14 +5,37 @@ require 'cgi'
5
5
 
6
6
 
7
7
  class Time
8
+ def to_precision(precision)
9
+ case precision
10
+ when 'ns'
11
+ self.to_ns
12
+ when 'u'
13
+ (self.to_r * 1000000).to_i
14
+ when 'ms'
15
+ self.to_ms
16
+ when 's'
17
+ self.to_i
18
+ when 'm'
19
+ self.to_i / 60
20
+ when 'h'
21
+ self.to_i / 3600
22
+ else
23
+ self.to_ns
24
+ end
25
+ end
26
+
27
+ def to_ns
28
+ (self.to_r * 1000000000).to_i
29
+ end
30
+
8
31
  def to_ms
9
- (self.to_f * 1000.0).to_i
32
+ (self.to_r * 1000).to_i
10
33
  end
11
34
  end
12
35
 
13
36
  module InfluxdbLogger
14
37
  def log_to_file(message) # for test
15
- open("#{Rails.root}/log/my.log", 'w') { |f|
38
+ open("#{Rails.root}/log/my.log", 'a') { |f|
16
39
  f.puts message.inspect
17
40
  }
18
41
  end
@@ -22,7 +45,7 @@ module InfluxdbLogger
22
45
  # Severity label for logging. (max 5 char)
23
46
  SEV_LABEL = %w(DEBUG INFO WARN ERROR FATAL ANY)
24
47
 
25
- def self.new(log_tags: {}, settings: {}, batch_size: 1000, interval: 1000)
48
+ def self.new(async: true, log_tags: {}, settings: {}, batch_size: 1000, interval: 1000)
26
49
  Rails.application.config.log_tags = log_tags.values
27
50
  if Rails.application.config.respond_to?(:action_cable)
28
51
  Rails.application.config.action_cable.log_tags = log_tags.values.map do |x|
@@ -43,6 +66,7 @@ module InfluxdbLogger
43
66
 
44
67
  settings[:batch_size] ||= batch_size
45
68
  settings[:interval] ||= interval
69
+ settings[:async] = async
46
70
 
47
71
  level = SEV_LABEL.index(Rails.application.config.log_level.to_s.upcase)
48
72
  logger = InfluxdbLogger::InnerLogger.new(settings, level, log_tags)
@@ -84,17 +108,16 @@ module InfluxdbLogger
84
108
  @batch_size = options[:batch_size]
85
109
  @interval = options[:interval]
86
110
  @series = options[:series]
111
+ @retention = options[:retention]
87
112
  @global_tags = {}
88
113
  @last_flush_time = Time.now.to_ms
89
114
  @value_filter = options[:value_filter] || {}
115
+ @time_precision = options[:time_precision] || 'ns'
90
116
 
91
117
  @influxdb_logger = InfluxDB::Client.new(
92
- host: options[:host],
93
- database: options[:database],
94
- retry: options[:retry],
95
- username: options[:username],
96
- password: options[:password],
97
- time_precision: options[:time_precision]
118
+ **options.slice(:host, :database, :retry, :username, :password, :async),
119
+ time_precision: @time_precision,
120
+ discard_write_errors: true
98
121
  )
99
122
 
100
123
  @severity = 0
@@ -167,7 +190,7 @@ module InfluxdbLogger
167
190
 
168
191
  message = {
169
192
  series: @series,
170
- timestamp: Time.now.to_ms,
193
+ timestamp: Time.now.to_precision(@time_precision),
171
194
  tags: tags,
172
195
  values: values.merge({
173
196
  severity: format_severity(@severity)
@@ -191,7 +214,7 @@ module InfluxdbLogger
191
214
 
192
215
  def flush
193
216
  return if @messages.empty?
194
- @influxdb_logger.write_points(@messages)
217
+ @influxdb_logger.write_points(@messages, @time_precision, @retention)
195
218
  @severity = 0
196
219
  @messages.clear
197
220
  @last_flush_time = Time.now.to_ms
@@ -1,3 +1,3 @@
1
1
  module InfluxdbLogger
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
3
3
  end
data/spec/logger_spec.rb CHANGED
@@ -40,7 +40,7 @@ describe InfluxdbLogger::Logger do
40
40
  @log << point
41
41
  end
42
42
 
43
- def write_points(points)
43
+ def write_points(points, time_precision, rentention = nil)
44
44
  @log ||= []
45
45
  @log.concat(points)
46
46
  end
@@ -206,7 +206,8 @@ describe InfluxdbLogger::Logger do
206
206
  time_precision: "ms",
207
207
  retry: 3,
208
208
  batch_size: 1000,
209
- interval: 1000
209
+ interval: 1000,
210
+ async: true
210
211
  })
211
212
  ActiveSupport::Logger.new(STDOUT)
212
213
  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.1
4
+ version: 1.0.2
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-28 00:00:00.000000000 Z
11
+ date: 2018-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec