civo-logger 0.2.7 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/civo-logger.gemspec +1 -0
- data/lib/civo/logger/middleware.rb +26 -43
- data/lib/civo/logger/version.rb +1 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 714d48ad34613d53567edc6d9cbb24ad574bc041
|
4
|
+
data.tar.gz: d6888f0c43e5e8d3c8b1eb81116278d61d849736
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 180403eef9272905b00dd8f9d32649234bd358c8290b3d4328993b5fe982b49fbafe8550ea65de293d07efb1c2382ce028dd2ad9e40f8a62d8790209c25fbe82
|
7
|
+
data.tar.gz: 299cb755a2178574083dc063380b15e67dcf11d7772c4365ba2afff0b672c09fd79f1f97c766766ab036723b34a3668b0db4798c4ced903d507bf1cefe1b5e8c
|
data/civo-logger.gemspec
CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
22
|
spec.require_paths = ["lib"]
|
23
23
|
|
24
|
+
spec.add_dependency "influxdb"
|
24
25
|
spec.add_development_dependency "bundler", "~> 1.14"
|
25
26
|
spec.add_development_dependency "rake", "~> 10.0"
|
26
27
|
spec.add_development_dependency "rspec", "~> 3.0"
|
@@ -1,3 +1,4 @@
|
|
1
|
+
require "influxdb"
|
1
2
|
require "active_support/core_ext/time/conversions"
|
2
3
|
require "active_support/core_ext/object/blank"
|
3
4
|
require "active_support/log_subscriber"
|
@@ -6,9 +7,11 @@ require "rack/body_proxy"
|
|
6
7
|
|
7
8
|
module Civo
|
8
9
|
class PerRequestLogger < ActiveSupport::LogSubscriber
|
9
|
-
cattr_accessor :redis
|
10
10
|
cattr_accessor :expires_in
|
11
11
|
STATS_RESOLUTION = 5.minutes
|
12
|
+
INFLUXDB_PRECISION = 'ms'
|
13
|
+
INFLUXDB_RETENTION = '7d.cpu'
|
14
|
+
|
12
15
|
|
13
16
|
def initialize(app, taggers = nil)
|
14
17
|
self.class.expires_in ||= 24.hours.to_i
|
@@ -16,17 +19,8 @@ module Civo
|
|
16
19
|
@taggers = taggers || []
|
17
20
|
Rails.logger = ActionController::Base.logger = ActiveRecord::Base.logger = self
|
18
21
|
$stdout.sync = true
|
19
|
-
|
20
|
-
|
21
|
-
elsif ENV["RAILS_LOGGER_HOSTNAME"].blank?
|
22
|
-
@redis = nil
|
23
|
-
else
|
24
|
-
options = {host: ENV["RAILS_LOGGER_HOSTNAME"], port: Rails.application.config.x.redis_port, timeout: 1}
|
25
|
-
if ENV["RAILS_LOGGER_PASSWORD"].present?
|
26
|
-
options[:password] = ENV["RAILS_LOGGER_PASSWORD"]
|
27
|
-
end
|
28
|
-
@redis = Redis.new(options)
|
29
|
-
end
|
22
|
+
options = {host: ENV["INFLUX_LOGGER_HOSTNAME"], port: ENV["INFLUX_LOGGER_PORT"], password: ENV["INFLUX_LOGGER_PASSWORD"], username: ENV["INFLUX_LOGGER_USERNAME"], timeout: 1, time_precision: "ms"}
|
23
|
+
@influxdb = InfluxDB::Client.new ENV["INFLUX_LOGGER_DATABASE"], options
|
30
24
|
end
|
31
25
|
|
32
26
|
def call(env)
|
@@ -105,7 +99,6 @@ module Civo
|
|
105
99
|
|
106
100
|
def start_worker(name)
|
107
101
|
@name = name
|
108
|
-
@key = Time.zone.now.strftime("%Y%m%d-%H%M%S")+"-#{name}"
|
109
102
|
@lines = []
|
110
103
|
@start = Time.now.to_f
|
111
104
|
@recording = true
|
@@ -114,32 +107,32 @@ module Civo
|
|
114
107
|
def end_worker
|
115
108
|
puts @lines.join("\n") if Rails.env.production?
|
116
109
|
|
117
|
-
if @
|
110
|
+
if @influx.nil?
|
118
111
|
puts @log
|
119
112
|
return
|
120
113
|
end
|
121
114
|
|
122
|
-
|
123
|
-
|
124
|
-
|
115
|
+
data = {
|
116
|
+
values: { log: @lines.join("\n") },
|
117
|
+
tags: { url: "worker://#{@name}", app: app_name },
|
118
|
+
timestamp: (Time.now.to_r * 1000).to_i
|
119
|
+
}
|
120
|
+
|
121
|
+
@influxdb.write_point("requests", data, INFLUXDB_PRECISION)# , INFLUXDB_RETENTION)
|
122
|
+
# rescue Redis::TimeoutError, Redis::CannotConnectError => e
|
123
|
+
# puts "ERROR! Redis error #{e.message}"
|
125
124
|
ensure
|
126
125
|
@recording = false
|
127
126
|
end
|
128
127
|
|
129
128
|
private
|
130
129
|
|
131
|
-
def request_key(request)
|
132
|
-
token = rand(8999) + 1000
|
133
|
-
@key = Time.zone.now.strftime("%Y%m%d-%H%M%S") +
|
134
|
-
"-#{token}-#{request.request_method}-#{request.fullpath.gsub("/", "_")}".strip
|
135
|
-
end
|
136
|
-
|
137
130
|
def call_app(request, env) # :doc:
|
138
131
|
@lines = []
|
139
132
|
@recording = true
|
140
133
|
@status = nil
|
141
134
|
@time_taken = nil
|
142
|
-
@
|
135
|
+
@url = request.fullpath
|
143
136
|
Rails.logger = ActionController::Base.logger = ActiveRecord::Base.logger = self
|
144
137
|
instrumenter = ActiveSupport::Notifications.instrumenter
|
145
138
|
instrumenter.start "request.action_dispatch", request: request
|
@@ -196,28 +189,18 @@ module Civo
|
|
196
189
|
@output = body.to_s
|
197
190
|
end
|
198
191
|
|
199
|
-
if @
|
192
|
+
if @influxdb.nil?
|
200
193
|
puts @log
|
201
194
|
return
|
202
195
|
end
|
203
196
|
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
@redis.set("#{app_name}:log-request-summary:#{@key}",
|
210
|
-
"#{request.request_method}:#{request.fullpath}:#{@output.length}:#{@status}:#{@time_taken}", ex: self.class.expires_in)
|
211
|
-
puts @log if Rails.env.production?
|
197
|
+
data = {
|
198
|
+
values: { log: Base64.urlsafe_encode64(@log[0, 65535]), output: Base64.urlsafe_encode64(@output), output_length: @output.length, status: @status || "-", ms_taken: @time_taken.to_i},
|
199
|
+
tags: { url: request.fullpath, method: request.request_method, app: app_name },
|
200
|
+
timestamp: (Time.now.to_r * 1000).to_i
|
201
|
+
}
|
212
202
|
|
213
|
-
|
214
|
-
if @status.to_i > 399
|
215
|
-
@redis.incr("#{app_name}:req-stat-failure:#{stat_key}")
|
216
|
-
@redis.expire("#{app_name}:req-stat-failure:#{stat_key}", 7.days)
|
217
|
-
else
|
218
|
-
@redis.incr("#{app_name}:req-stat-success:#{stat_key}")
|
219
|
-
@redis.expire("#{app_name}:req-stat-success:#{stat_key}", 7.days)
|
220
|
-
end
|
203
|
+
@influxdb.write_point("requests", data, INFLUXDB_PRECISION)# , INFLUXDB_RETENTION)
|
221
204
|
end
|
222
205
|
|
223
206
|
if Rails.env.development? || Rails.env.test?
|
@@ -226,8 +209,8 @@ module Civo
|
|
226
209
|
end
|
227
210
|
end
|
228
211
|
|
229
|
-
rescue Redis::TimeoutError, Redis::CannotConnectError => e
|
230
|
-
|
212
|
+
# rescue Redis::TimeoutError, Redis::CannotConnectError => e
|
213
|
+
# puts "ERROR! Redis error #{e.message}"
|
231
214
|
ensure
|
232
215
|
@recording = false
|
233
216
|
end
|
data/lib/civo/logger/version.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: civo-logger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andy Jeffries
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-05-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: influxdb
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|