vestige 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 19accfcc39cbff1dfd2abeeda518b8acd1204a9e
4
+ data.tar.gz: 5f564253bcf79090757f964456bbbd7a741c6bd3
5
+ SHA512:
6
+ metadata.gz: 50dd291006b14a4fd0e84a8419444c4920973ae51b6026d2c0e966e3514bb73865e934579e7a8807ec001b15a1313d52fc2ca9ae3c47eeaf725219ad5b592ea2
7
+ data.tar.gz: 427c7853f0aa6547bbd4b181e7910889fb2ce4f2396d4098cc8cfc4db6a39a096079207f4d8edd88b46716e74fafa4f413a91db2f712b9dca90023f244b9bd53
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in vestige.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Pavel Penkov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,72 @@
1
+ # Vestige
2
+
3
+ Distributed tracing for Rails applications.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'vestige'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Vestige relies on `ActionDispatch::RequestId` middleware to generate request id, saves it to thread local variable accessible as `Vestige.trace_id` and forwards it to different API clients to enable cross-application tracing. At the moment it's tightly coupled with Rails and supports Sidekiq, delayed_job and Faraday.
16
+
17
+ ### Sidekiq
18
+
19
+ Add client and server middlewares to `config/initializers/sidekiq.rb`
20
+
21
+ ```ruby
22
+ Sidekiq.configure_server do |config|
23
+ config.logger.formatter = Vestige::Sidekiq::Formatter.new
24
+
25
+ config.server_middleware do |chain|
26
+ chain.add Vestige::Sidekiq::Server
27
+ end
28
+
29
+ config.client_middleware do |chain| # Fan-out support
30
+ chain.add Vestige::Sidekiq::Client
31
+ end
32
+ end
33
+
34
+ Sidekiq.configure_client do |config|
35
+ config.logger.formatter = Vestige::Sidekiq::Formatter.new
36
+
37
+ config.client_middleware do |chain|
38
+ chain.add Vestige::Sidekiq::Client
39
+ end
40
+ end
41
+ ```
42
+
43
+ ### delayed_job
44
+
45
+ Run `rails g vestige:delayed_job:install && rake db:migrate` to add `trace_id` column. Add plugin to `config/initializers/delayed_job.rb`
46
+
47
+ ```ruby
48
+ Delayed::Worker.plugins << Vestige::DelayedJob::Plugin
49
+ ```
50
+
51
+ ### Faraday
52
+
53
+ Add middleware to Faraday stack
54
+
55
+ ```ruby
56
+ connection = Faraday.new("http://example.com") do |conn|
57
+ conn.request :vestige
58
+ conn.response :logger, Rails.logger
59
+ end
60
+ ```
61
+
62
+ ### Net::HTTP
63
+
64
+ Call `Vestige.patch_net_http!` in initializer
65
+
66
+ You'll probably want to tag Rails log as well by adding `config.log_tags = [:uuid]` to `config/application.rb`
67
+
68
+
69
+
70
+ ## License
71
+
72
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "vestige"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,28 @@
1
+ require "vestige/version"
2
+ require "vestige/rack"
3
+ require "vestige/net_http_patch"
4
+ require "vestige/railtie" if defined?(Rails.application)
5
+
6
+ %w(delayed_job sidekiq faraday).each do |lib|
7
+ begin
8
+ require lib
9
+ require "vestige/#{lib}"
10
+ rescue LoadError
11
+ end
12
+ end
13
+
14
+ module Vestige
15
+ TRACE_ID_KEY = "vestige_trace_id".freeze
16
+
17
+ def self.trace_id
18
+ Thread.current[TRACE_ID_KEY]
19
+ end
20
+
21
+ def self.trace_id=(trace_id)
22
+ Thread.current[TRACE_ID_KEY] = trace_id
23
+ end
24
+
25
+ def self.patch_net_http!
26
+ Net::HTTPGenericRequest.prepend(NetHttpPatch)
27
+ end
28
+ end
@@ -0,0 +1,26 @@
1
+ module Vestige
2
+ module DelayedJob
3
+ class Plugin < ::Delayed::Plugin
4
+ callbacks do |lifecycle|
5
+ lifecycle.around(:invoke_job) do |job, *args, &block|
6
+ begin
7
+ Vestige.trace_id = job.trace_id
8
+ if Rails.logger.respond_to?(:tagged)
9
+ Rails.logger.tagged(Vestige.trace_id, "delayed_job") { block.call(job, *args) }
10
+ else
11
+ block.call(job, *args)
12
+ end
13
+ ensure
14
+ Vestige.trace_id = nil
15
+ end
16
+ end
17
+
18
+ lifecycle.before(:enqueue) do |job, *_args, &_block|
19
+ job.trace_id = Vestige.trace_id
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ Delayed::Worker.plugins << Vestige::DelayedJob::Plugin
@@ -0,0 +1,14 @@
1
+ module Vestige
2
+ class Faraday < ::Faraday::Middleware
3
+ X_REQUEST_ID = "X-Request-Id".freeze
4
+
5
+ def call(env)
6
+ env[:request_headers].merge!(X_REQUEST_ID => Vestige.trace_id)
7
+ @app.call(env)
8
+ end
9
+ end
10
+ end
11
+
12
+ if Faraday::Middleware.respond_to?(:register_middleware)
13
+ Faraday::Request.register_middleware(vestige: lambda { Vestige::Faraday })
14
+ end
@@ -0,0 +1,18 @@
1
+ require 'rails/generators/migration'
2
+ require 'rails/generators/active_record'
3
+
4
+ module Vestige
5
+ module Generators
6
+ class InstallGenerator < ::Rails::Generators::Base
7
+ include ::ActiveRecord::Generators::Migration
8
+
9
+ source_root File.expand_path("../templates", __FILE__)
10
+
11
+ desc "Add request id column to delayed jobs"
12
+
13
+ def copy_migrations
14
+ migration_template "add_trace_id_to_delayed_jobs.rb", "db/migrate/add_trace_id_to_delayed_jobs.rb"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ class AddTraceIdToDelayedJobs < ActiveRecord::Migration
2
+ def change
3
+ change_table :delayed_jobs do |t|
4
+ t.string :trace_id
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ module Vestige
2
+ module NetHttpPatch
3
+ X_REQUEST_ID = 'x-request-id'.freeze
4
+
5
+ def initialize_http_header(initheader)
6
+ super
7
+ @header[X_REQUEST_ID] = Vestige.trace_id unless (@header.key?(X_REQUEST_ID) || Vestige.trace_id.nil?)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ module Vestige
2
+ class Rack
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ Vestige.trace_id = env['action_dispatch.request_id']
9
+ @app.call(env)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module Vestige
2
+ class Railtie < ::Rails::Railtie
3
+ initializer "vestige.configure_rails_initialization" do
4
+ require_relative 'rack'
5
+ ::Rails.application.middleware.insert_after(::ActionDispatch::RequestId, Rack)
6
+ end
7
+
8
+ generators do
9
+ require_relative "generators/install_generator"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,36 @@
1
+ module Vestige
2
+ module Sidekiq
3
+ class Client
4
+ def call(_worker_class, job, _queue, _redis_pool = nil)
5
+ job[TRACE_ID_KEY] = Vestige.trace_id
6
+ yield
7
+ end
8
+ end
9
+
10
+ class Server
11
+ def initialize(logger = nil)
12
+ @logger = logger
13
+ end
14
+
15
+ def call(_worker, msg, _queue)
16
+ Vestige.trace_id = msg[TRACE_ID_KEY]
17
+ current_logger.respond_to?(:tagged) ? current_logger.tagged(Vestige.trace_id, "sidekiq") { yield } : yield
18
+ ensure
19
+ Vestige.trace_id = nil
20
+ end
21
+
22
+ private
23
+
24
+ def current_logger
25
+ @logger || (Rails.logger if defined?(Rails.application)) || Sidekiq.logger
26
+ end
27
+ end
28
+
29
+ class Formatter
30
+ def call(severity, timestamp, _progname, msg)
31
+ tag = Vestige.trace_id ? "[#{Vestige.trace_id}] " : ''
32
+ "#{tag}#{timestamp.utc.iso8601} #{::Process.pid} TID-#{Thread.current.object_id.to_s(36)} #{severity}: #{msg}\n"
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module Vestige
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'vestige/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "vestige"
8
+ spec.version = Vestige::VERSION
9
+ spec.authors = ["Pavel Penkov"]
10
+ spec.email = ["ebonfortress@gmail.com"]
11
+
12
+ spec.summary = "Distributed tracing for Rails applications"
13
+ spec.description = "Distributed tracing for Rails applications"
14
+ spec.homepage = "https://github.com/PavelPenkov/vestige"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest", "~> 5.0"
25
+ spec.add_development_dependency "rack"
26
+ spec.add_development_dependency "simplecov"
27
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vestige
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pavel Penkov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Distributed tracing for Rails applications
84
+ email:
85
+ - ebonfortress@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bin/console
97
+ - bin/setup
98
+ - lib/vestige.rb
99
+ - lib/vestige/delayed_job.rb
100
+ - lib/vestige/faraday.rb
101
+ - lib/vestige/generators/install_generator.rb
102
+ - lib/vestige/generators/templates/add_trace_id_to_delayed_jobs.rb
103
+ - lib/vestige/net_http_patch.rb
104
+ - lib/vestige/rack.rb
105
+ - lib/vestige/railtie.rb
106
+ - lib/vestige/sidekiq.rb
107
+ - lib/vestige/version.rb
108
+ - vestige.gemspec
109
+ homepage: https://github.com/PavelPenkov/vestige
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.4.5.1
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Distributed tracing for Rails applications
133
+ test_files: []
134
+ has_rdoc: