vx-lib-instrumentation 0.1.9
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 +7 -0
- data/.gitignore +17 -0
- data/.rspec +3 -0
- data/.travis.yml +1 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/vx/lib/instrumentation/logger.rb +103 -0
- data/lib/vx/lib/instrumentation/probe/action_controller.rb +6 -0
- data/lib/vx/lib/instrumentation/probe/action_dispatch.rb +15 -0
- data/lib/vx/lib/instrumentation/probe/action_mailer.rb +6 -0
- data/lib/vx/lib/instrumentation/probe/action_view.rb +8 -0
- data/lib/vx/lib/instrumentation/probe/active_record.rb +26 -0
- data/lib/vx/lib/instrumentation/probe/active_support.rb +6 -0
- data/lib/vx/lib/instrumentation/probe/faraday.rb +24 -0
- data/lib/vx/lib/instrumentation/rack/handle_exceptions_middleware.rb +51 -0
- data/lib/vx/lib/instrumentation/stderr.rb +10 -0
- data/lib/vx/lib/instrumentation/version.rb +3 -0
- data/lib/vx/lib/instrumentation.rb +83 -0
- data/spec/lib/instrumentation_spec.rb +38 -0
- data/spec/lib/logger_spec.rb +93 -0
- data/spec/lib/rack/handle_exceptions_middleware_spec.rb +36 -0
- data/spec/spec_helper.rb +5 -0
- data/vx-lib-instrumentation.gemspec +25 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ff72ddb7a88101050f23d3b0db56c0da7ae6b572
|
4
|
+
data.tar.gz: 2494e7d6263470e413b384abb6d2d182ea518b8c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6e9c21e8460dc11e54117e2aa1e949e512319c149483075250588a40d14512c169cb00255080eaf9a640b6bdfdd0c2d766b9142427d2215c37e0a2d9aca37766
|
7
|
+
data.tar.gz: f185366f214dcf37d10e3a848b36a2f03d91b50cc7d745022a0d8fee4f08f3ea29d95e38c592167648283bc39d01b2d5a329b06df2bce3f259fcde16f127672b
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
script: "rspec spec/"
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Dmitry Galinsky
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Vx::Instrumentation
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'vx-instrumentation'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install vx-instrumentation
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( http://github.com/<my-github-username>/vx-instrumentation/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'logger'
|
3
|
+
require 'active_support/core_ext/hash/deep_merge'
|
4
|
+
|
5
|
+
module Vx ; module Lib ; module Instrumentation
|
6
|
+
|
7
|
+
class Logger
|
8
|
+
|
9
|
+
def initialize(device)
|
10
|
+
@device = device
|
11
|
+
end
|
12
|
+
|
13
|
+
def method_missing(sym, *args, &block)
|
14
|
+
if @device.respond_to?(sym)
|
15
|
+
begin
|
16
|
+
@device.send(sym, *args, &block)
|
17
|
+
rescue Exception => e
|
18
|
+
$stderr.puts "#{e.class.to_s}, #{e.message.inspect} [#{sym.inspect} #{args.inspect}]"
|
19
|
+
$stderr.puts e.backtrace.map{|b| "\t#{b}" }.join("\n")
|
20
|
+
end
|
21
|
+
else
|
22
|
+
super
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def respond_to?(sym)
|
27
|
+
@device.respond_to?(sym)
|
28
|
+
end
|
29
|
+
|
30
|
+
class << self
|
31
|
+
attr_accessor :logger
|
32
|
+
|
33
|
+
def setup(target)
|
34
|
+
log = ::Logger.new(target, 7, 50_000_000)
|
35
|
+
log.formatter = Lib::Instrumentation::Logger::Formatter
|
36
|
+
@logger = new(log)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class Formatter
|
41
|
+
|
42
|
+
def self.safe_value(value, options = {})
|
43
|
+
new_value = case value.class.to_s
|
44
|
+
when "String", "Fixnum", "Float"
|
45
|
+
value
|
46
|
+
when "Symbol", "BigDecimal"
|
47
|
+
value.to_s
|
48
|
+
when "Array"
|
49
|
+
value = value.map(&:to_s)
|
50
|
+
options[:join_arrays] ? value.join("\n") : value
|
51
|
+
when 'NilClass'
|
52
|
+
nil
|
53
|
+
else
|
54
|
+
value.inspect
|
55
|
+
end
|
56
|
+
if new_value.is_a?(String)
|
57
|
+
new_value.encode('UTF-8', {:invalid => :replace, :undef => :replace, :replace => '?'})
|
58
|
+
else
|
59
|
+
new_value
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.make_safe_hash(msg, options = {})
|
64
|
+
msg.inject({}) do |acc, pair|
|
65
|
+
msg_key, msg_value = pair
|
66
|
+
|
67
|
+
if msg_key == "@fields"
|
68
|
+
acc[msg_key] = make_safe_hash(msg_value, join_arrays: true)
|
69
|
+
else
|
70
|
+
acc[msg_key] = safe_value(msg_value, options)
|
71
|
+
end
|
72
|
+
acc
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.call(severity, _, _, msg)
|
77
|
+
values = Lib::Instrumentation.default.dup
|
78
|
+
|
79
|
+
case
|
80
|
+
when msg.is_a?(Hash)
|
81
|
+
values.deep_merge! msg
|
82
|
+
when msg.respond_to?(:to_h)
|
83
|
+
values.merge! msg.to_h
|
84
|
+
else
|
85
|
+
values.deep_merge!(message: msg)
|
86
|
+
end
|
87
|
+
|
88
|
+
values.deep_merge!(severity: severity.to_s.downcase)
|
89
|
+
|
90
|
+
values = make_safe_hash(values)
|
91
|
+
|
92
|
+
if ENV['VX_INSTRUMENTATION_PRETTY']
|
93
|
+
::JSON.pretty_generate(values) + "\n"
|
94
|
+
else
|
95
|
+
::JSON.dump(values) + "\n"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
end ; end ; end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'active_support/notifications'
|
2
|
+
|
3
|
+
ActiveSupport::Notifications.subscribe(/\.action_dispatch$/) do |event, started, finished, _, payload|
|
4
|
+
req = payload[:request]
|
5
|
+
payload = {
|
6
|
+
path: req.fullpath,
|
7
|
+
ip: req.remote_ip,
|
8
|
+
method: req.method,
|
9
|
+
referer: req.referer,
|
10
|
+
content_length: req.content_length,
|
11
|
+
user_agent: req.user_agent
|
12
|
+
}
|
13
|
+
Vx::Lib::Instrumentation.delivery event, payload, event.split("."), started, finished
|
14
|
+
end
|
15
|
+
|
@@ -0,0 +1,8 @@
|
|
1
|
+
require 'active_support/notifications'
|
2
|
+
require 'vx/instrumentation'
|
3
|
+
|
4
|
+
ActiveSupport::Notifications.subscribe(/\.action_view$/) do |event, started, finished, _, payload|
|
5
|
+
if event[0] != "!"
|
6
|
+
Vx::Lib::Instrumentation.delivery event, payload, event.split("."), started, finished
|
7
|
+
end
|
8
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'active_support/notifications'
|
2
|
+
|
3
|
+
ActiveSupport::Notifications.subscribe(/\.active_record$/) do |event, started, finished, _, payload|
|
4
|
+
|
5
|
+
binds =
|
6
|
+
(payload[:binds] || []).map do |column, value|
|
7
|
+
if column
|
8
|
+
if column.binary?
|
9
|
+
value = "<#{value.bytesize} bytes of binary data>"
|
10
|
+
end
|
11
|
+
[column.name, value]
|
12
|
+
else
|
13
|
+
[nil, value]
|
14
|
+
end
|
15
|
+
end.inspect
|
16
|
+
|
17
|
+
payload = {
|
18
|
+
sql: payload[:sql],
|
19
|
+
binds: binds,
|
20
|
+
name: payload[:name],
|
21
|
+
duration: payload[:duration]
|
22
|
+
}
|
23
|
+
|
24
|
+
Vx::Lib::Instrumentation.delivery event, payload, event.split("."), started, finished
|
25
|
+
end
|
26
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'active_support/notifications'
|
2
|
+
|
3
|
+
ActiveSupport::Notifications.subscribe(/\.faraday$/) do |event, started, finished, _, payload|
|
4
|
+
|
5
|
+
render_http_header = ->(headers) {
|
6
|
+
(headers || []).map do |key,value|
|
7
|
+
if %{ PRIVATE-TOKEN Authorization }.include?(key)
|
8
|
+
value = value.gsub(/./, "*")
|
9
|
+
end
|
10
|
+
"#{key}: #{value}"
|
11
|
+
end.join("\n")
|
12
|
+
}
|
13
|
+
|
14
|
+
payload = {
|
15
|
+
method: payload[:method],
|
16
|
+
url: payload[:url].to_s,
|
17
|
+
status: payload[:status],
|
18
|
+
response_headers: render_http_header.call(payload[:response_headers]),
|
19
|
+
request_headers: render_http_header.call(payload[:request_headers])
|
20
|
+
}
|
21
|
+
|
22
|
+
Vx::Lib::Instrumentation.delivery event, payload, event.split("."), started, finished
|
23
|
+
end
|
24
|
+
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Vx ; module Lib ; module Instrumentation
|
2
|
+
module Rack
|
3
|
+
|
4
|
+
HandleExceptionsMiddleware = Struct.new(:app) do
|
5
|
+
|
6
|
+
IGNORED_EXCEPTIONS = %w{
|
7
|
+
ActionController::RoutingError
|
8
|
+
}
|
9
|
+
|
10
|
+
def clean_env(env)
|
11
|
+
env = env.select{|k,v| k !~ /^(action_dispatch|puma|session|rack\.session|action_controller)/ }
|
12
|
+
env['HTTP_COOKIE'] &&= env['HTTP_COOKIE'].scan(/.{80}/).join("\n")
|
13
|
+
env
|
14
|
+
end
|
15
|
+
|
16
|
+
def notify(exception, env)
|
17
|
+
unless ignore?(exception)
|
18
|
+
Lib::Instrumentation.handle_exception(
|
19
|
+
'handle_exception.rack',
|
20
|
+
exception,
|
21
|
+
clean_env(env)
|
22
|
+
)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def call(env)
|
27
|
+
begin
|
28
|
+
response = app.call(env)
|
29
|
+
rescue Exception => ex
|
30
|
+
notify ex, env
|
31
|
+
raise ex
|
32
|
+
end
|
33
|
+
|
34
|
+
if ex = framework_exception(env)
|
35
|
+
notify ex, env
|
36
|
+
end
|
37
|
+
|
38
|
+
response
|
39
|
+
end
|
40
|
+
|
41
|
+
def framework_exception(env)
|
42
|
+
env['rack.exception'] || env['action_dispatch.exception']
|
43
|
+
end
|
44
|
+
|
45
|
+
def ignore?(ex)
|
46
|
+
IGNORED_EXCEPTIONS.include? ex.class.name
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end ; end ; end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'thread'
|
2
|
+
|
3
|
+
require File.expand_path("../instrumentation/version", __FILE__)
|
4
|
+
require File.expand_path("../instrumentation/logger", __FILE__)
|
5
|
+
require File.expand_path("../instrumentation/stderr", __FILE__)
|
6
|
+
require File.expand_path("../instrumentation/rack/handle_exceptions_middleware", __FILE__)
|
7
|
+
|
8
|
+
module Vx ; module Lib ; module Instrumentation
|
9
|
+
|
10
|
+
extend Lib::Instrumentation::Stderr
|
11
|
+
|
12
|
+
DATE_FORMAT = '%Y-%m-%dT%H:%M:%S.%N%z'
|
13
|
+
THREAD_KEY = 'vx_lib_instrumentation_keys'
|
14
|
+
|
15
|
+
extend self
|
16
|
+
|
17
|
+
def root
|
18
|
+
File.expand_path("../", __FILE__)
|
19
|
+
end
|
20
|
+
|
21
|
+
def activate!
|
22
|
+
Dir[root + "/*.rb"].each do |f|
|
23
|
+
require f
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def install(target, log_level = 0)
|
28
|
+
$stdout.puts " --> activate Vx::Instrumentation, log stored in #{target}"
|
29
|
+
Lib::Instrumentation::Logger.setup target
|
30
|
+
Lib::Instrumentation::Logger.logger.level = log_level
|
31
|
+
|
32
|
+
activate!
|
33
|
+
end
|
34
|
+
|
35
|
+
def with(new_keys)
|
36
|
+
old_keys = Thread.current[THREAD_KEY]
|
37
|
+
begin
|
38
|
+
Thread.current[THREAD_KEY] = (old_keys || {}).merge(new_keys)
|
39
|
+
yield if block_given?
|
40
|
+
ensure
|
41
|
+
Thread.current[THREAD_KEY] = old_keys
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def default
|
46
|
+
Thread.current[THREAD_KEY] || {}
|
47
|
+
end
|
48
|
+
|
49
|
+
def handle_exception(event, ex, env = {})
|
50
|
+
tags = event.split(".")
|
51
|
+
tags << "exception"
|
52
|
+
tags.uniq!
|
53
|
+
|
54
|
+
payload = {
|
55
|
+
"@event" => event,
|
56
|
+
"@timestamp" => Time.now.strftime(DATE_FORMAT),
|
57
|
+
"@tags" => tags,
|
58
|
+
"@fields" => env,
|
59
|
+
process_id: Process.pid,
|
60
|
+
thread_id: Thread.current.object_id,
|
61
|
+
exception: ex.class.to_s,
|
62
|
+
message: ex.message.to_s,
|
63
|
+
backtrace: (ex.backtrace || []).map(&:to_s).join("\n"),
|
64
|
+
}
|
65
|
+
|
66
|
+
notify_stderr(ex)
|
67
|
+
Lib::Instrumentation::Logger.logger.error(payload)
|
68
|
+
end
|
69
|
+
|
70
|
+
def delivery(name, payload, tags, started, finished)
|
71
|
+
Lib::Instrumentation::Logger.logger.log(
|
72
|
+
::Logger::INFO,
|
73
|
+
"@event" => name,
|
74
|
+
process_id: Process.pid,
|
75
|
+
thread_id: Thread.current.object_id,
|
76
|
+
"@timestamp" => started.strftime(DATE_FORMAT),
|
77
|
+
"@duration" => (finished - started).to_f,
|
78
|
+
"@fields" => payload,
|
79
|
+
"@tags" => tags
|
80
|
+
)
|
81
|
+
end
|
82
|
+
|
83
|
+
end ; end ; end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Vx::Lib::Instrumentation do
|
4
|
+
let(:out) { StringIO.new }
|
5
|
+
let(:inst) { described_class }
|
6
|
+
let(:result) {
|
7
|
+
out.rewind
|
8
|
+
JSON.parse out.read
|
9
|
+
}
|
10
|
+
|
11
|
+
it "should successfully install" do
|
12
|
+
expect(inst.install out).to be
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should work with default values" do
|
16
|
+
inst.with(x: "y") do
|
17
|
+
inst.with(a: "b") do
|
18
|
+
expect(inst.default).to eq(
|
19
|
+
x: "y",
|
20
|
+
a: "b"
|
21
|
+
)
|
22
|
+
end
|
23
|
+
expect(inst.default).to eq(x: "y")
|
24
|
+
end
|
25
|
+
expect(inst.default).to eq({})
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should handle exception" do
|
29
|
+
inst::Logger.setup out
|
30
|
+
ex = Exception.new("message")
|
31
|
+
inst.handle_exception('event.name', ex, {key: "value"})
|
32
|
+
expect(result["@event"]).to eq 'event.name'
|
33
|
+
expect(result["@tags"]).to eq ["event", "name", "exception"]
|
34
|
+
expect(result["@fields"]).to eq("key" => "value")
|
35
|
+
expect(result["exception"]).to eq 'Exception'
|
36
|
+
expect(result["message"]).to eq 'message'
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Vx::Lib::Instrumentation::Logger do
|
4
|
+
let(:out) { StringIO.new }
|
5
|
+
let(:logger) { described_class.setup out }
|
6
|
+
let(:result) {
|
7
|
+
out.rewind
|
8
|
+
JSON.parse(out.read)
|
9
|
+
}
|
10
|
+
|
11
|
+
it "should write string mesage" do
|
12
|
+
logger.info "I am string"
|
13
|
+
expect(result).to eq(
|
14
|
+
"message" => "I am string",
|
15
|
+
"severity" => "info"
|
16
|
+
)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should write a simple hash" do
|
20
|
+
logger.info(key: "value")
|
21
|
+
expect(result).to eq(
|
22
|
+
'key' => 'value',
|
23
|
+
"severity" => "info"
|
24
|
+
)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should write a nested hash" do
|
28
|
+
logger.info(
|
29
|
+
root: {
|
30
|
+
child: {
|
31
|
+
subchild: "value"
|
32
|
+
}
|
33
|
+
}
|
34
|
+
)
|
35
|
+
expect(result).to eq(
|
36
|
+
"root" => "{:child=>{:subchild=>\"value\"}}",
|
37
|
+
"severity" => "info"
|
38
|
+
)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should write nested hash with @fields" do
|
42
|
+
logger.info(
|
43
|
+
"@fields" => {
|
44
|
+
child: {
|
45
|
+
subchild: "value"
|
46
|
+
}
|
47
|
+
}
|
48
|
+
)
|
49
|
+
expect(result).to eq(
|
50
|
+
"@fields" => {
|
51
|
+
"child"=>"{:subchild=>\"value\"}"
|
52
|
+
},
|
53
|
+
"severity" => "info"
|
54
|
+
)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should write nested hash with arrays" do
|
58
|
+
logger.info(
|
59
|
+
a: %w{ 1 2 3 },
|
60
|
+
"@fields" => {
|
61
|
+
c: %w{ 4 5 6 }
|
62
|
+
}
|
63
|
+
)
|
64
|
+
expect(result).to eq(
|
65
|
+
"a" => ["1", "2", "3"],
|
66
|
+
"@fields" => {
|
67
|
+
"c" => "4\n5\n6"
|
68
|
+
},
|
69
|
+
"severity" => "info"
|
70
|
+
)
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should work with default values" do
|
74
|
+
Vx::Lib::Instrumentation.with(foo: "bar") do
|
75
|
+
logger.info(key: "value")
|
76
|
+
end
|
77
|
+
expect(result).to eq(
|
78
|
+
"foo" => "bar",
|
79
|
+
"key" => "value",
|
80
|
+
"severity" => "info"
|
81
|
+
)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should work with nested default values" do
|
85
|
+
Vx::Lib::Instrumentation.with("@fields" => {a: 1}) do
|
86
|
+
logger.info("@fields" => {b: 2})
|
87
|
+
end
|
88
|
+
expect(result).to eq(
|
89
|
+
"@fields" => {"a"=>1, "b"=>2},
|
90
|
+
"severity" => "info"
|
91
|
+
)
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Vx::Lib::Instrumentation::Rack::HandleExceptionsMiddleware do
|
4
|
+
let(:env) { {} }
|
5
|
+
let(:app) { ->(e){ e.merge(foo: :bar) } }
|
6
|
+
let(:output) { StringIO.new }
|
7
|
+
let(:middleware) { described_class.new(app) }
|
8
|
+
let(:result) {
|
9
|
+
output.rewind
|
10
|
+
c = output.read
|
11
|
+
if c.to_s != ""
|
12
|
+
JSON.parse c
|
13
|
+
end
|
14
|
+
}
|
15
|
+
|
16
|
+
before do
|
17
|
+
Vx::Lib::Instrumentation::Logger.setup output
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should work when no exceptions raised" do
|
21
|
+
expect(middleware.call(env)).to eq(foo: :bar)
|
22
|
+
expect(result).to be_nil
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should catch raised exception" do
|
26
|
+
app = ->(_) { raise RuntimeError, "Ignore Me" }
|
27
|
+
mid = described_class.new(app)
|
28
|
+
expect{ mid.call(env) }.to raise_error(RuntimeError, 'Ignore Me')
|
29
|
+
|
30
|
+
expect(result["@tags"]).to eq ["handle_exception", "rack", "exception"]
|
31
|
+
expect(result["@event"]).to eq 'handle_exception.rack'
|
32
|
+
expect(result['exception']).to eq 'RuntimeError'
|
33
|
+
expect(result['message']).to eq 'Ignore Me'
|
34
|
+
expect(result['backtrace']).to_not be_empty
|
35
|
+
end
|
36
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'vx/lib/instrumentation/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "vx-lib-instrumentation"
|
8
|
+
spec.version = Vx::Lib::Instrumentation::VERSION
|
9
|
+
spec.authors = ["Dmitry Galinsky"]
|
10
|
+
spec.email = ["dima.exe@gmail.com"]
|
11
|
+
spec.summary = %q{ summary }
|
12
|
+
spec.description = %q{ description }
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency 'activesupport'
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: vx-lib-instrumentation
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.9
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dmitry Galinsky
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
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'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.5'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.5'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: " description "
|
56
|
+
email:
|
57
|
+
- dima.exe@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- lib/vx/lib/instrumentation.rb
|
70
|
+
- lib/vx/lib/instrumentation/logger.rb
|
71
|
+
- lib/vx/lib/instrumentation/probe/action_controller.rb
|
72
|
+
- lib/vx/lib/instrumentation/probe/action_dispatch.rb
|
73
|
+
- lib/vx/lib/instrumentation/probe/action_mailer.rb
|
74
|
+
- lib/vx/lib/instrumentation/probe/action_view.rb
|
75
|
+
- lib/vx/lib/instrumentation/probe/active_record.rb
|
76
|
+
- lib/vx/lib/instrumentation/probe/active_support.rb
|
77
|
+
- lib/vx/lib/instrumentation/probe/faraday.rb
|
78
|
+
- lib/vx/lib/instrumentation/rack/handle_exceptions_middleware.rb
|
79
|
+
- lib/vx/lib/instrumentation/stderr.rb
|
80
|
+
- lib/vx/lib/instrumentation/version.rb
|
81
|
+
- spec/lib/instrumentation_spec.rb
|
82
|
+
- spec/lib/logger_spec.rb
|
83
|
+
- spec/lib/rack/handle_exceptions_middleware_spec.rb
|
84
|
+
- spec/spec_helper.rb
|
85
|
+
- vx-lib-instrumentation.gemspec
|
86
|
+
homepage: ''
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.2.2
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: summary
|
110
|
+
test_files:
|
111
|
+
- spec/lib/instrumentation_spec.rb
|
112
|
+
- spec/lib/logger_spec.rb
|
113
|
+
- spec/lib/rack/handle_exceptions_middleware_spec.rb
|
114
|
+
- spec/spec_helper.rb
|