ilove-tracing 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4be119c8bc6ab1a3cdec05a0784a4149143fe269970cace3c12a1e0af6df8e2a
4
+ data.tar.gz: 9d4d1b918e6d64a3a2ee88ee023616b397d9006d9f143682acbf7f8e43b7d2a5
5
+ SHA512:
6
+ metadata.gz: 842832d1f09cb3635b663c06d9c98762b9a0a4dbb906244b9a73ca17a7d9b38d1a2f3091401e946732198838f1c4306bf09aa919642c4fc2beabfda378c8f859
7
+ data.tar.gz: 16a2ede0dccfea191271afcf915ad5450feea5c4ed27909b593bd05a671e7ae5bab7d56920e753138cb0c6f291add17c0bbe5576d74c20756f706e71848b5ed8
@@ -0,0 +1,46 @@
1
+ version: 2.1
2
+
3
+ parameters:
4
+ project_name:
5
+ type: string
6
+ default: ilove-tracing
7
+ ruby_image:
8
+ type: string
9
+ default: circleci/ruby:2.6.5
10
+
11
+ jobs:
12
+ test_app_code:
13
+ parameters:
14
+ cache_name:
15
+ type: string
16
+ default: << pipeline.parameters.project_name >>-cache-{{ checksum "<< pipeline.parameters.project_name >>.gemspec" }}
17
+ working_directory: ~/src
18
+ docker:
19
+ - image: << pipeline.parameters.ruby_image >>
20
+ steps:
21
+ - checkout
22
+ - setup_remote_docker:
23
+ docker_layer_caching: true
24
+ - restore_cache:
25
+ keys:
26
+ - << parameters.cache_name >>
27
+ - run:
28
+ name: Bundle install
29
+ command: bundle install --path ~/.bundler
30
+ - save_cache:
31
+ key: << parameters.cache_name >>
32
+ paths:
33
+ - ~/.bundler
34
+ - run:
35
+ name: Rspec
36
+ command: bundle exec rspec --out /tmp/test-results/rspec.xml
37
+
38
+ workflows:
39
+ version: 2
40
+ app:
41
+ jobs:
42
+ - test_app_code:
43
+ filters:
44
+ tags:
45
+ only: /.*/
46
+
@@ -0,0 +1,4 @@
1
+ * @uno4ki
2
+ .circleci/ @pcheliniy
3
+ *.rb @severgroup-tt/ruby
4
+
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.5
7
+ before_install: gem install bundler -v 1.17.3
data/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## 0.1.0 - 2020-03-05
8
+
9
+ ### Added
10
+ * Tracing of
11
+ - Rack http requests
12
+ - ActiveRecord sql
13
+ - Twrip service calls
14
+ - Faraday outgoing requests
15
+ * Pass request id from incoming to outgoing requests
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at a.zimin@talenttech.ru. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in ilove-tracing.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,169 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ilove-tracing (0.2.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ actioncable (6.0.2.1)
10
+ actionpack (= 6.0.2.1)
11
+ nio4r (~> 2.0)
12
+ websocket-driver (>= 0.6.1)
13
+ actionmailbox (6.0.2.1)
14
+ actionpack (= 6.0.2.1)
15
+ activejob (= 6.0.2.1)
16
+ activerecord (= 6.0.2.1)
17
+ activestorage (= 6.0.2.1)
18
+ activesupport (= 6.0.2.1)
19
+ mail (>= 2.7.1)
20
+ actionmailer (6.0.2.1)
21
+ actionpack (= 6.0.2.1)
22
+ actionview (= 6.0.2.1)
23
+ activejob (= 6.0.2.1)
24
+ mail (~> 2.5, >= 2.5.4)
25
+ rails-dom-testing (~> 2.0)
26
+ actionpack (6.0.2.1)
27
+ actionview (= 6.0.2.1)
28
+ activesupport (= 6.0.2.1)
29
+ rack (~> 2.0, >= 2.0.8)
30
+ rack-test (>= 0.6.3)
31
+ rails-dom-testing (~> 2.0)
32
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
33
+ actiontext (6.0.2.1)
34
+ actionpack (= 6.0.2.1)
35
+ activerecord (= 6.0.2.1)
36
+ activestorage (= 6.0.2.1)
37
+ activesupport (= 6.0.2.1)
38
+ nokogiri (>= 1.8.5)
39
+ actionview (6.0.2.1)
40
+ activesupport (= 6.0.2.1)
41
+ builder (~> 3.1)
42
+ erubi (~> 1.4)
43
+ rails-dom-testing (~> 2.0)
44
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
45
+ activejob (6.0.2.1)
46
+ activesupport (= 6.0.2.1)
47
+ globalid (>= 0.3.6)
48
+ activemodel (6.0.2.1)
49
+ activesupport (= 6.0.2.1)
50
+ activerecord (6.0.2.1)
51
+ activemodel (= 6.0.2.1)
52
+ activesupport (= 6.0.2.1)
53
+ activestorage (6.0.2.1)
54
+ actionpack (= 6.0.2.1)
55
+ activejob (= 6.0.2.1)
56
+ activerecord (= 6.0.2.1)
57
+ marcel (~> 0.3.1)
58
+ activesupport (6.0.2.1)
59
+ concurrent-ruby (~> 1.0, >= 1.0.2)
60
+ i18n (>= 0.7, < 2)
61
+ minitest (~> 5.1)
62
+ tzinfo (~> 1.1)
63
+ zeitwerk (~> 2.2)
64
+ builder (3.2.4)
65
+ concurrent-ruby (1.1.6)
66
+ crass (1.0.6)
67
+ diff-lcs (1.3)
68
+ erubi (1.9.0)
69
+ faraday (0.17.3)
70
+ multipart-post (>= 1.2, < 3)
71
+ globalid (0.4.2)
72
+ activesupport (>= 4.2.0)
73
+ google-protobuf (3.11.4-universal-darwin)
74
+ i18n (1.8.2)
75
+ concurrent-ruby (~> 1.0)
76
+ loofah (2.4.0)
77
+ crass (~> 1.0.2)
78
+ nokogiri (>= 1.5.9)
79
+ mail (2.7.1)
80
+ mini_mime (>= 0.1.1)
81
+ marcel (0.3.3)
82
+ mimemagic (~> 0.3.2)
83
+ method_source (0.9.2)
84
+ mimemagic (0.3.3)
85
+ mini_mime (1.0.2)
86
+ mini_portile2 (2.4.0)
87
+ minitest (5.14.0)
88
+ multipart-post (2.1.1)
89
+ nio4r (2.5.2)
90
+ nokogiri (1.10.8)
91
+ mini_portile2 (~> 2.4.0)
92
+ rack (2.2.2)
93
+ rack-test (1.1.0)
94
+ rack (>= 1.0, < 3)
95
+ rails (6.0.2.1)
96
+ actioncable (= 6.0.2.1)
97
+ actionmailbox (= 6.0.2.1)
98
+ actionmailer (= 6.0.2.1)
99
+ actionpack (= 6.0.2.1)
100
+ actiontext (= 6.0.2.1)
101
+ actionview (= 6.0.2.1)
102
+ activejob (= 6.0.2.1)
103
+ activemodel (= 6.0.2.1)
104
+ activerecord (= 6.0.2.1)
105
+ activestorage (= 6.0.2.1)
106
+ activesupport (= 6.0.2.1)
107
+ bundler (>= 1.3.0)
108
+ railties (= 6.0.2.1)
109
+ sprockets-rails (>= 2.0.0)
110
+ rails-dom-testing (2.0.3)
111
+ activesupport (>= 4.2.0)
112
+ nokogiri (>= 1.6)
113
+ rails-html-sanitizer (1.3.0)
114
+ loofah (~> 2.3)
115
+ railties (6.0.2.1)
116
+ actionpack (= 6.0.2.1)
117
+ activesupport (= 6.0.2.1)
118
+ method_source
119
+ rake (>= 0.8.7)
120
+ thor (>= 0.20.3, < 2.0)
121
+ rake (13.0.1)
122
+ rspec (3.9.0)
123
+ rspec-core (~> 3.9.0)
124
+ rspec-expectations (~> 3.9.0)
125
+ rspec-mocks (~> 3.9.0)
126
+ rspec-core (3.9.1)
127
+ rspec-support (~> 3.9.1)
128
+ rspec-expectations (3.9.0)
129
+ diff-lcs (>= 1.2.0, < 2.0)
130
+ rspec-support (~> 3.9.0)
131
+ rspec-mocks (3.9.1)
132
+ diff-lcs (>= 1.2.0, < 2.0)
133
+ rspec-support (~> 3.9.0)
134
+ rspec-support (3.9.2)
135
+ sprockets (4.0.0)
136
+ concurrent-ruby (~> 1.0)
137
+ rack (> 1, < 3)
138
+ sprockets-rails (3.2.1)
139
+ actionpack (>= 4.0)
140
+ activesupport (>= 4.0)
141
+ sprockets (>= 3.0.0)
142
+ thor (1.0.1)
143
+ thread_safe (0.3.6)
144
+ twirp (1.4.1)
145
+ faraday (~> 0)
146
+ google-protobuf (~> 3.0, >= 3.0.0)
147
+ twirp_rails (0.3.0)
148
+ railties (~> 6.0)
149
+ twirp (~> 1)
150
+ tzinfo (1.2.6)
151
+ thread_safe (~> 0.1)
152
+ websocket-driver (0.7.1)
153
+ websocket-extensions (>= 0.1.0)
154
+ websocket-extensions (0.1.4)
155
+ zeitwerk (2.2.2)
156
+
157
+ PLATFORMS
158
+ ruby
159
+
160
+ DEPENDENCIES
161
+ bundler (~> 1.17)
162
+ ilove-tracing!
163
+ rails (~> 6.0)
164
+ rake (>= 12.3.3)
165
+ rspec (~> 3.0)
166
+ twirp_rails (~> 0.3.0)
167
+
168
+ BUNDLED WITH
169
+ 1.17.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Alexandr Zimin
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.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # I love tracing
2
+
3
+ Help to trace rack based applications and microservices. Create opentracing spans on:
4
+ - [Rack http requests](#tracing-rack-http-requests)
5
+ - [ActiveRecord sql](#tracing-activerecord-sql)
6
+ - [Twrip service calls](#tracing-twrip_rails-service-calls)
7
+ - [Faraday outgoing requests](#tracing-faraday-outgoing-requests)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'ilove-tracing'
15
+ gem 'opentracing'
16
+
17
+ gem 'jaeger-client' # or other opentracing client implementation
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ ## Usage
25
+
26
+ Create and customize the initializer file:
27
+
28
+ ```sh
29
+ $ rails g tracing:init
30
+ ```
31
+
32
+ This creates `initializers/ilove-tracing.rb` file.
33
+
34
+ _N.B. If you not use Rails - copy it from gem sources
35
+ `cp $(bundle show ilove-tracing)/lib/generators/tracing/init/templates/ilove_tracing.rb ./`_
36
+
37
+ ## Tracing Rack http requests
38
+
39
+ Tracer creates opentracing span named `incoming http request` with parent of incoming http context.
40
+
41
+ ## Tracing ActiveRecord SQL
42
+
43
+ Creates span of all executed queries named `sql.active_record`. Tags sql.name, sql.statement, sql.statement_name.
44
+
45
+ ## Tracing twrip_rails service calls
46
+
47
+ Instrument all twirp services mounted by twirp_rails. Creates span named `twirp call` on incoming twirp calls.
48
+ Tags: `service`, `method`.
49
+
50
+ To trace twirp services without twirp_rails call
51
+ ```ruby
52
+ require 'ilove/tracing/twirp.rb'
53
+
54
+ ILove::Tracing::Twirp.trace_service(service)
55
+ ```
56
+
57
+ ## Tracing faraday outgoing requests
58
+
59
+ Instrument all faraday outgoing requests. Creates span named `outgoing http request`.
60
+ Inject active span to request headers.
61
+ Tags: `url`, `method`.
62
+
63
+ _N.B. This option uses monkey patch of `Faraday::ConnectionOptions` to inject middleware to all faraday
64
+ (and twirp clients) requests._
65
+
66
+ ## Pass request_id header from incoming requests to outgoing requests.
67
+
68
+ If this options turned on (default if `config.enabled?`) then header `X-Request-Id` from incoming http requests passed to outgoing.
69
+
70
+ ## Development
71
+
72
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
73
+
74
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
75
+
76
+ ## Contributing
77
+
78
+ Bug reports and pull requests are welcome on GitHub at https://github.com/severgroup-tt/ilove-tracing. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
79
+
80
+ ## License
81
+
82
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
83
+
84
+ ## Code of Conduct
85
+
86
+ Everyone interacting in the Ilove::Tracing project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ilove-tracing/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,45 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ilove/tracing/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'ilove-tracing'
8
+ spec.version = ILove::Tracing::VERSION
9
+ spec.authors = ['Alexandr Zimin']
10
+ spec.email = ['a.zimin@talenttech.ru']
11
+
12
+ spec.summary = 'Opentracing plugins to instrument rails, twirp_rails, faraday, active_record.'
13
+ spec.description = 'Opentracing plugins to instrument rails, twirp_rails, faraday, active_record.'
14
+ spec.homepage = 'http://github.com/severgroup-tt/ilove-tracing'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+
22
+ spec.metadata['homepage_uri'] = spec.homepage
23
+ spec.metadata['source_code_uri'] = 'http://github.com/severgroup-tt/ilove-tracing'
24
+ spec.metadata['changelog_uri'] = 'http://github.com/severgroup-tt/ilove-tracing/blob/master/CHANGELOG.md'
25
+ else
26
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
27
+ 'public gem pushes.'
28
+ end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
33
+ `git ls-files -z`.split("\x0")
34
+ .reject { |f| f.match(%r{^(bin|doc|test|spec|features)/}) }
35
+ end
36
+ spec.bindir = 'exe'
37
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
38
+ spec.require_paths = ['lib']
39
+
40
+ spec.add_development_dependency 'bundler', '~> 1.17'
41
+ spec.add_development_dependency 'rake', '>= 12.3.3'
42
+ spec.add_development_dependency 'rspec', '~> 3.0'
43
+ spec.add_development_dependency 'rails', '~> 6.0'
44
+ spec.add_development_dependency 'twirp_rails', '~> 0.3.0'
45
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate init
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,10 @@
1
+ module Tracing
2
+ class InitGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('templates', __dir__)
4
+
5
+ desc 'This generator creates an initializer file at config/initializers'
6
+ def create_initializer_file
7
+ copy_file 'ilove_tracing.rb', 'config/initializers/ilove_tracing.rb'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,39 @@
1
+ ILove::Tracing.configure do |config|
2
+ # Enable tracing (default false - tracing disabled)
3
+ config.enabled = ENV['JAEGER_HOST'].present?
4
+
5
+ # Service name (with rails Rails.application.class.parent_name else nil)
6
+ # config.service_name = Rails.application.class.parent_name
7
+
8
+ # Tracing client :jaeger, :none or initialized client instance (default :jaeger)
9
+ # config.client = :jaeger
10
+
11
+ # Tracing client params (client initializer arguments)
12
+ # config.client_params do
13
+ # {
14
+ # host: ENV.fetch('JAEGER_HOST') { 'localhost' },
15
+ # port: ENV.fetch('JAEGER_PORT') { 6831 },
16
+ # service_name: config.service_name
17
+ # }
18
+ # end
19
+
20
+ # Trace all rack http requests (default true)
21
+ # if it tuned off - cross application tracing doesnt work
22
+ # config.trace_incoming_requests = true
23
+
24
+ # Trace all http faraday requests (default true)
25
+ # if it tuned off - cross application tracing doesnt work
26
+ # config.trace_outgoing_requests = true
27
+
28
+ # Trace twirp incoming requests (default true)
29
+ # config.trace_twirp_requests = true
30
+
31
+ # Trace sql queries (default true)
32
+ # config.trace_sql = true
33
+
34
+ # Pass request id from incoming requests to outgoing requests (default true)
35
+ # config.pass_request_id = true
36
+
37
+ # request id header name (default 'X-Request-Id')
38
+ # config.request_id_header = 'X-Request-Id'
39
+ end
@@ -0,0 +1,140 @@
1
+ require 'ilove/tracing/version'
2
+ require 'active_support/dependencies'
3
+
4
+ module ILove
5
+ module Tracing
6
+ class Configuration
7
+ def self.config_param(symbol, default_value = nil, &block)
8
+ raise 'wrong args' if !default_value.nil? && block_given?
9
+
10
+ if block_given?
11
+ class_variable_set("@@#{symbol}_default", block)
12
+ class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
13
+ def #{symbol}_default
14
+ instance_eval &@@#{symbol}_default
15
+ end
16
+ RUBY
17
+ else
18
+ class_variable_set("@@#{symbol}_default", default_value)
19
+ class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
20
+ def #{symbol}_default
21
+ @@#{symbol}_default
22
+ end
23
+ RUBY
24
+ end
25
+
26
+ class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
27
+ def #{symbol}
28
+ @#{symbol} ||= #{symbol}_default
29
+ end
30
+ RUBY
31
+
32
+ class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
33
+ def #{symbol}?
34
+ #{symbol}
35
+ end
36
+
37
+ def #{symbol}=(value)
38
+ @#{symbol} = value
39
+ end
40
+ RUBY
41
+ end
42
+
43
+
44
+ # Enable tracing (false), if it false - tracing disabled
45
+ config_param :enabled, false
46
+
47
+ # Service name (with rails Rails.application.class.parent_name else nil)
48
+ config_param(:service_name) { defined?(Rails) ? Rails.application.class.parent_name : nil }
49
+
50
+ # Tracing client (:jaeger) - :jaeger, :none or initialized client instance
51
+ config_param :client, :jaeger
52
+
53
+ # Tracing client params
54
+ # host: ENV.fetch('JAEGER_HOST') { 'localhost' },
55
+ # port: ENV.fetch('JAEGER_PORT') { 6831 },
56
+ # service_name: service_name
57
+ config_param(:client_params) do
58
+ {
59
+ host: ENV.fetch('JAEGER_HOST') { 'localhost' },
60
+ port: ENV.fetch('JAEGER_PORT') { 6831 },
61
+ service_name: service_name
62
+ }
63
+ end
64
+
65
+ # Trace all rack http requests (true)
66
+ # if it tuned off - cross application tracing doesnt work
67
+ config_param :trace_incoming_requests, true
68
+
69
+ # Trace all http faraday requests (true)
70
+ # if it tuned off - cross application tracing doesnt work
71
+ config_param :trace_outgoing_requests, true
72
+
73
+ # Trace twirp incoming requests (true)
74
+ config_param :trace_twirp_requests, true
75
+
76
+ # Trace sql queries (true)
77
+ config_param :trace_sql, true
78
+
79
+ # Pass request id from incoming requests to outgoing requests (true)
80
+ config_param :pass_request_id, true
81
+
82
+ # Pass request id from incoming requests to outgoing requests (true)
83
+ config_param :request_id_header, 'X-Request-Id'
84
+ end
85
+
86
+ def self.configuration
87
+ @cfg
88
+ end
89
+
90
+ def self.configure(&_block)
91
+ @cfg = Configuration.new
92
+ yield @cfg
93
+ setup @cfg
94
+ end
95
+
96
+ def self.setup(cfg)
97
+ return unless cfg.enabled?
98
+
99
+ require_relative 'tracing/client'
100
+
101
+ ILove::Tracing::Client.setup cfg
102
+
103
+ if cfg.trace_incoming_requests?
104
+ require_relative 'tracing/incoming_requests'
105
+
106
+ ILove::Tracing::IncomingRequests.setup cfg
107
+ end
108
+
109
+ if cfg.trace_outgoing_requests?
110
+ require_relative 'tracing/outgoing_requests'
111
+
112
+ ILove::Tracing::OutgoingRequests.setup cfg
113
+ end
114
+
115
+ if cfg.trace_twirp_requests?
116
+ require_relative 'tracing/twirp'
117
+
118
+ ILove::Tracing::Twirp.setup cfg
119
+ end
120
+
121
+ if cfg.trace_sql?
122
+ require_relative 'tracing/sql'
123
+
124
+ ILove::Tracing::Sql.setup cfg
125
+ end
126
+
127
+ if cfg.pass_request_id?
128
+ require_relative 'tracing/request_id'
129
+
130
+ ILove::Tracing::RequestId.setup cfg
131
+ end
132
+
133
+ if cfg.pass_request_id? || cfg.trace_outgoing_requests?
134
+ ILove::Tracing::FaradayMiddleware.setup cfg
135
+ end
136
+ end
137
+
138
+ class Error < StandardError; end
139
+ end
140
+ end
@@ -0,0 +1,20 @@
1
+ module ILove
2
+ module Tracing
3
+ module Client
4
+ def self.setup(cfg)
5
+ case cfg.client
6
+ when :jaeger
7
+ require('jaeger/client') rescue raise("Add gem 'jaeger-client' to Gemfile")
8
+
9
+ OpenTracing.global_tracer = Jaeger::Client.build(cfg.client_params)
10
+ when :none
11
+ nil
12
+ when Symbol
13
+ raise "Unknown client #{cfg.client}"
14
+ else
15
+ OpenTracing.global_tracer = cfg.client
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,32 @@
1
+ module ILove
2
+ module Tracing
3
+ module FaradayMiddleware
4
+ @@active_middleware = Set.new
5
+
6
+ def self.active_middleware
7
+ @@active_middleware
8
+ end
9
+
10
+ def self.add_active_middleware(middleware)
11
+ @@active_middleware << middleware
12
+ end
13
+
14
+ module FaradayConnectionOptions
15
+ def new_builder(block)
16
+ super.tap do |builder|
17
+ ILove::Tracing::FaradayMiddleware.active_middleware.each do |middleware|
18
+ builder.insert(0, middleware)
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ def self.setup(cfg)
25
+ return if active_middleware.empty?
26
+ raise 'Faraday is not defined, can not add required outgoing middleware' unless defined?(Faraday)
27
+
28
+ Faraday::ConnectionOptions.prepend(FaradayConnectionOptions)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,28 @@
1
+ module ILove
2
+ module Tracing
3
+ module IncomingRequests
4
+ class TraceRackMiddleware
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ extracted_ctx = OpenTracing.global_tracer.extract(OpenTracing::FORMAT_RACK, env)
11
+ span = OpenTracing.start_span('incoming http request', child_of: extracted_ctx)
12
+ scope = OpenTracing.scope_manager.activate span
13
+
14
+ @app.call(env)
15
+ ensure
16
+ scope.close
17
+ end
18
+ end
19
+
20
+
21
+ def self.setup(cfg)
22
+ raise 'incoming requests tracing requires rails' unless defined?(Rails)
23
+
24
+ Rails.application.middleware.unshift TraceRackMiddleware
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,27 @@
1
+ require_relative 'faraday_middleware'
2
+
3
+ module ILove
4
+ module Tracing
5
+ module OutgoingRequests
6
+ class FaradayTraceMiddleware < Faraday::Middleware
7
+ def call(env)
8
+ scope = OpenTracing.start_active_span 'outgoing http request',
9
+ child_of: OpenTracing.active_span,
10
+ tags: { url: env.url, method: env.method }
11
+
12
+ OpenTracing.inject scope.span.context, OpenTracing::FORMAT_RACK, env[:request_headers]
13
+
14
+ @app.call(env).on_complete do
15
+ scope.close
16
+ end
17
+ end
18
+ end
19
+
20
+ def self.setup(cfg)
21
+ raise 'Cannot trace outgoing request without Faraday' unless defined?(Faraday)
22
+
23
+ ILove::Tracing::FaradayMiddleware.add_active_middleware FaradayTraceMiddleware
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,54 @@
1
+ require_relative 'faraday_middleware'
2
+
3
+ module ILove
4
+ module Tracing
5
+ module RequestId
6
+ def self.cfg
7
+ @@cfg
8
+ end
9
+
10
+ def self.request_id
11
+ Thread.current[:request_id]
12
+ end
13
+
14
+ def self.request_id=(val)
15
+ Thread.current[:request_id] = val
16
+ end
17
+
18
+ class RequestIdRackMiddleware
19
+ def initialize(app)
20
+ @app = app
21
+ end
22
+
23
+ def rack_header_name
24
+ @@rack_header_name ||= 'HTTP_' + ILove::Tracing::RequestId.cfg.request_id_header.gsub(/-/, '_').upcase
25
+ end
26
+
27
+ def call(env)
28
+ ILove::Tracing::RequestId.request_id = env[rack_header_name]
29
+
30
+ @app.call(env)
31
+ ensure
32
+ ILove::Tracing::RequestId.request_id = nil
33
+ end
34
+ end
35
+
36
+ class RequestIdFaradayMiddleware < Faraday::Middleware
37
+ def call(env)
38
+ env.request_headers[ILove::Tracing::RequestId.cfg.request_id_header] = ILove::Tracing::RequestId.request_id
39
+
40
+ @app.call(env)
41
+ end
42
+ end
43
+
44
+ def self.setup(cfg)
45
+ raise 'pass request id requires rails' unless defined?(::Rails)
46
+ raise 'pass request id requires faraday' unless defined?(::Faraday)
47
+
48
+ @@cfg = cfg
49
+ Rails.application.middleware.unshift RequestIdRackMiddleware
50
+ ILove::Tracing::FaradayMiddleware.add_active_middleware RequestIdFaradayMiddleware
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,22 @@
1
+ module ILove
2
+ module Tracing
3
+ module Sql
4
+ def self.setup(_cfg)
5
+ raise 'Cannot trace sql without ActiveSupport' unless defined?(::ActiveSupport)
6
+
7
+ ActiveSupport::Notifications.subscribe('sql.active_record') do |_name, start, finish, _id, payload|
8
+ span = OpenTracing.start_span 'sql.active_record',
9
+ start_time: start,
10
+ child_of: OpenTracing.active_span,
11
+ tags: {
12
+ 'sql.statement' => payload[:sql],
13
+ 'sql.statement_name' => payload[:statement_name],
14
+ 'sql.name' => payload[:name]
15
+ }
16
+
17
+ span.finish end_time: finish
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,49 @@
1
+ module ILove
2
+ module Tracing
3
+ module Twirp
4
+ def self.setup(cfg)
5
+ raise 'Cannot trace twirp without twirp_rails gem' unless defined?(::TwirpRails)
6
+
7
+ ::TwirpRails::Routes::Helper.on_create_service do |service|
8
+ trace_service service
9
+ end
10
+ end
11
+
12
+ def self.trace_service(service)
13
+ scope = nil
14
+
15
+ service.before do |_rack_env, env|
16
+ method_name = env[:rpc_method]
17
+
18
+ scope = OpenTracing.start_active_span 'twirp call',
19
+ child_of: OpenTracing.active_span,
20
+ tags: { service: service.full_name, method: method_name }
21
+ end
22
+
23
+ service.on_error do |err, _env|
24
+ if scope
25
+ scope.span.set_tag :error_code, err.code
26
+ scope.span.set_tag :error_msg, err.msg
27
+ scope.close
28
+ scope = nil
29
+ end
30
+ end
31
+
32
+ service.on_success do |_env|
33
+ if scope
34
+ scope.close
35
+ scope = nil
36
+ end
37
+ end
38
+
39
+ service.exception_raised do |e, _env|
40
+ if scope
41
+ scope.span.set_tag :exception, e.class.name
42
+ scope.close
43
+ scope = nil
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,5 @@
1
+ module ILove
2
+ module Tracing
3
+ VERSION = "0.2.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ilove-tracing
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexandr Zimin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-03-06 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.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 12.3.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 12.3.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '6.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '6.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: twirp_rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.3.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.3.0
83
+ description: Opentracing plugins to instrument rails, twirp_rails, faraday, active_record.
84
+ email:
85
+ - a.zimin@talenttech.ru
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".circleci/config.yml"
91
+ - ".github/CODEOWNERS"
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - ".travis.yml"
95
+ - CHANGELOG.md
96
+ - CODE_OF_CONDUCT.md
97
+ - Gemfile
98
+ - Gemfile.lock
99
+ - LICENSE.txt
100
+ - README.md
101
+ - Rakefile
102
+ - ilove-tracing.gemspec
103
+ - lib/generators/tracing/init/USAGE
104
+ - lib/generators/tracing/init/init_generator.rb
105
+ - lib/generators/tracing/init/templates/ilove_tracing.rb
106
+ - lib/ilove/tracing.rb
107
+ - lib/ilove/tracing/client.rb
108
+ - lib/ilove/tracing/faraday_middleware.rb
109
+ - lib/ilove/tracing/incoming_requests.rb
110
+ - lib/ilove/tracing/outgoing_requests.rb
111
+ - lib/ilove/tracing/request_id.rb
112
+ - lib/ilove/tracing/sql.rb
113
+ - lib/ilove/tracing/twirp.rb
114
+ - lib/ilove/tracing/version.rb
115
+ homepage: http://github.com/severgroup-tt/ilove-tracing
116
+ licenses:
117
+ - MIT
118
+ metadata:
119
+ homepage_uri: http://github.com/severgroup-tt/ilove-tracing
120
+ source_code_uri: http://github.com/severgroup-tt/ilove-tracing
121
+ changelog_uri: http://github.com/severgroup-tt/ilove-tracing/blob/master/CHANGELOG.md
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubygems_version: 3.0.6
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Opentracing plugins to instrument rails, twirp_rails, faraday, active_record.
141
+ test_files: []