vx-instrumentation 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3e2d666dc400fcb14d81f99233e4bf4e544aa594
4
+ data.tar.gz: ec357619cff76c059cd349c862db172ae4f9ebe5
5
+ SHA512:
6
+ metadata.gz: 53d7910af3fb3949d6c9df4ebd96b529fe89ed9ee0ee82a62138ff9159875b4d3f5f94579ebf05a79c8024333a4ed247c818b5828bff16d669e4d4fe699e6490
7
+ data.tar.gz: 9b447bed92a4a082d2372d856ecc84908fd0b0a16af577751441939e48ccd1ff42cfc43b39ca4b6c56df523d6cd5dc21ced96e3bd19f1c610029af5610286c0e
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ -fd
3
+ --order=rand
data/.travis.yml ADDED
@@ -0,0 +1 @@
1
+ script: "rspec spec/"
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in vx-instrumentation.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'rspec'
8
+ end
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,18 @@
1
+ module Vx
2
+ module Instrumentation
3
+ class ActionDispatch < Subscriber
4
+
5
+ event(/\.action_dispatch$/)
6
+
7
+ def process
8
+ req = payload.delete(:request)
9
+ self.payload = {
10
+ path: req.fullpath,
11
+ ip: req.ip,
12
+ method: req.method,
13
+ }
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,33 @@
1
+ module Vx
2
+ module Instrumentation
3
+ class ActiveRecord < Subscriber
4
+
5
+ event(/\.active_record$/)
6
+
7
+ def process
8
+ self.payload = {
9
+ sql: payload[:sql].compact,
10
+ binds: render_binds,
11
+ name: payload[:name],
12
+ duration: payload[:duration]
13
+ }
14
+ end
15
+
16
+ private
17
+
18
+ def render_binds
19
+ (payload[:binds] || []).map do |column, value|
20
+ if column
21
+ if column.binary?
22
+ value = "<#{value.bytesize} bytes of binary data>"
23
+ end
24
+ [column.name, value]
25
+ else
26
+ [nil, value]
27
+ end
28
+ end.inspect
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,9 @@
1
+ module Vx
2
+ module Instrumentation
3
+ class AmqpConsumer < Subscriber
4
+
5
+ event(/\.consumer\.amqp$/)
6
+
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,31 @@
1
+ module Vx
2
+ module Instrumentation
3
+ class Faraday < Subscriber
4
+
5
+ event 'request.faraday'
6
+
7
+ def process
8
+ self.name = 'request.http'
9
+ self.payload = {
10
+ method: payload[:method],
11
+ url: payload[:url].to_s,
12
+ status: payload[:status],
13
+ response_headers: render_http_header(payload[:response_headers]),
14
+ request_headers: render_http_header(payload[:request_headers])
15
+ }
16
+ end
17
+
18
+ private
19
+
20
+ def render_http_header(headers)
21
+ headers.map do |key,value|
22
+ if %{ PRIVATE-TOKEN Authorization }.include?(key)
23
+ value = value.gsub(/./, "*")
24
+ end
25
+ "#{key}: #{value}"
26
+ end.join("\n")
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,95 @@
1
+ require 'json'
2
+ require 'logger'
3
+
4
+ module Vx
5
+
6
+ module Instrumentation
7
+
8
+ class Logger
9
+
10
+ def initialize(device)
11
+ @device = device
12
+ end
13
+
14
+ def method_missing(sym, *args, &block)
15
+ if @device.respond_to?(sym)
16
+ begin
17
+ @device.send(sym, *args, &block)
18
+ rescue Exception => e
19
+ $stderr.puts "#{e.class.to_s} in #{e.message.inspect} [#{sym.inspect} #{args.inspect}]"
20
+ $stderr.puts e.backtrace.map{|b| " #{b}" }.join("\n")
21
+ end
22
+ else
23
+ super
24
+ end
25
+ end
26
+
27
+ def respond_to?(sym)
28
+ @device.respond_to?(sym)
29
+ end
30
+
31
+ class << self
32
+ attr_accessor :logger
33
+
34
+ def setup(target)
35
+ log = ::Logger.new(target)
36
+ log.formatter = Formatter
37
+ $stdout.puts " --> #{self.to_s} to #{target}"
38
+ @logger = new(log)
39
+ end
40
+ end
41
+
42
+ class Formatter
43
+
44
+ def self.safe_value(value, options = {})
45
+ case value.class.to_s
46
+ when "String", "Fixnum", "Float"
47
+ value
48
+ when "Symbol", "BigDecimal"
49
+ value.to_s
50
+ when "Array"
51
+ value = value.map(&:to_s)
52
+ options[:join_arrays] ? value.join("\n") : value
53
+ else
54
+ value.inspect
55
+ end
56
+ end
57
+
58
+ def self.make_safe_hash(msg, options = {})
59
+ msg.inject({}) do |acc, pair|
60
+ msg_key, msg_value = pair
61
+
62
+ if msg_key == "@fields"
63
+ acc[msg_key] = make_safe_hash(msg_value, join_arrays: true)
64
+ else
65
+ acc[msg_key] = safe_value(msg_value, options)
66
+ end
67
+ acc
68
+ end
69
+ end
70
+
71
+ def self.call(severity, _, _, msg)
72
+ values = Vx::Instrumentation.default.dup
73
+
74
+ case
75
+ when msg.is_a?(Hash)
76
+ values.merge! msg
77
+ when msg.respond_to?(:to_h)
78
+ values.merge! msg.to_h
79
+ else
80
+ values.merge!(message: msg)
81
+ end
82
+
83
+ values.merge!(severity: severity.to_s.downcase)
84
+
85
+ values = make_safe_hash(values)
86
+
87
+ ::JSON.dump(values) + "\n"
88
+ end
89
+
90
+ end
91
+
92
+ end
93
+
94
+ end
95
+ end
@@ -0,0 +1,9 @@
1
+ module Vx
2
+ module Instrumentation
3
+ class Rails < Subscriber
4
+
5
+ event(/\.(action_controller|action_view|action_mailer|active_support|railties)$/)
6
+
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,33 @@
1
+ require 'logger'
2
+ require 'active_support/notifications'
3
+
4
+ module Vx
5
+ module Instrumentation
6
+ Subscriber = Struct.new(:name, :payload, :tags) do
7
+
8
+ def process ; end
9
+
10
+ class << self
11
+
12
+ def install
13
+ ev = event || /.*/
14
+ $stdout.puts " --> add instrumentation #{self.to_s} to #{ev.inspect}"
15
+ ActiveSupport::Notifications.subscribe(ev) do |name, started, finished, uid, payload|
16
+ if name[0] != '!'
17
+ tags = name.split(".")
18
+ inst = new(name, payload, tags).tap(&:process)
19
+ Instrumentation.delivery(inst.name, inst.payload, inst.tags.uniq, started, finished)
20
+ end
21
+ end
22
+ end
23
+
24
+ def event(name = nil)
25
+ @event = name if name
26
+ @event
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,5 @@
1
+ module Vx
2
+ module Instrumentation
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,79 @@
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/subscriber", __FILE__)
6
+
7
+ require File.expand_path("../instrumentation/faraday", __FILE__)
8
+ require File.expand_path("../instrumentation/active_record", __FILE__)
9
+ require File.expand_path("../instrumentation/action_dispatch", __FILE__)
10
+ require File.expand_path("../instrumentation/rails", __FILE__)
11
+ require File.expand_path("../instrumentation/amqp_consumer", __FILE__)
12
+
13
+ module Vx
14
+ module Instrumentation
15
+
16
+ DATE_FORMAT = '%Y-%m-%dT%H:%M:%S.%N%z'
17
+ THREAD_KEY = 'vx_instrumentation_keys'
18
+
19
+ extend self
20
+
21
+ def install(target, log_level = 0)
22
+ Instrumentation::Logger.setup target
23
+ Instrumentation::Logger.logger.level = log_level
24
+ ObjectSpace.each_object(Class) do |c|
25
+ next unless c.superclass == Instrumentation::Subscriber
26
+ c.install
27
+ end
28
+ end
29
+
30
+ def with(new_keys)
31
+ old_keys = Thread.current[THREAD_KEY]
32
+ begin
33
+ Thread.current[THREAD_KEY] = (old_keys || {}).merge(new_keys)
34
+ yield if block_given?
35
+ ensure
36
+ Thread.current[THREAD_KEY] = old_keys
37
+ end
38
+ end
39
+
40
+ def default
41
+ Thread.current[THREAD_KEY] || {}
42
+ end
43
+
44
+ def handle_exception(event, ex, env = {})
45
+ tags = event.split(".")
46
+ tags << "exception"
47
+ tags.uniq!
48
+
49
+ payload = {
50
+ "@event" => event,
51
+ "@process_id" => Process.pid,
52
+ "@thread_id" => Thread.current.object_id,
53
+ "@timestamp" => Time.now.strftime(DATE_FORMAT),
54
+ "@tags" => tags,
55
+ "@fields" => env,
56
+ exception: ex.class.to_s,
57
+ message: ex.message.to_s,
58
+ backtrace: (ex.backtrace || []).map(&:to_s).join("\n"),
59
+ }
60
+ puts payload.inspect
61
+ Vx::Instrumentation::Logger.logger.error(payload)
62
+ end
63
+
64
+ def delivery(name, payload, tags, started, finished)
65
+ Vx::Instrumentation::Logger.logger.log(
66
+ ::Logger::INFO,
67
+ "@event" => name,
68
+ "@process_id" => Process.pid,
69
+ "@thread_id" => Thread.current.object_id,
70
+ "@timestamp" => started.strftime(DATE_FORMAT),
71
+ "@duration" => (finished - started).to_f,
72
+ "@fields" => payload,
73
+ "@tags" => tags
74
+ )
75
+ end
76
+
77
+ end
78
+
79
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vx::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,83 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vx::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 woth with default values" do
74
+ Vx::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
+ end
@@ -0,0 +1,5 @@
1
+ require File.expand_path("../../lib/vx/instrumentation", __FILE__)
2
+ require 'rspec/autorun'
3
+
4
+ RSpec.configure do |config|
5
+ end
@@ -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/instrumentation/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "vx-instrumentation"
8
+ spec.version = Vx::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', '~> 4.0'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.5"
24
+ spec.add_development_dependency "rake"
25
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vx-instrumentation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Galinsky
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-01 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: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '4.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/instrumentation.rb
70
+ - lib/vx/instrumentation/action_dispatch.rb
71
+ - lib/vx/instrumentation/active_record.rb
72
+ - lib/vx/instrumentation/amqp_consumer.rb
73
+ - lib/vx/instrumentation/faraday.rb
74
+ - lib/vx/instrumentation/logger.rb
75
+ - lib/vx/instrumentation/rails.rb
76
+ - lib/vx/instrumentation/subscriber.rb
77
+ - lib/vx/instrumentation/version.rb
78
+ - spec/lib/instrumentation_spec.rb
79
+ - spec/lib/logger_spec.rb
80
+ - spec/spec_helper.rb
81
+ - vx-instrumentation.gemspec
82
+ homepage: ''
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.0.14
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: summary
106
+ test_files:
107
+ - spec/lib/instrumentation_spec.rb
108
+ - spec/lib/logger_spec.rb
109
+ - spec/spec_helper.rb
110
+ has_rdoc: