rails-tracer 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: '042756289491ce49c5e0a2c38a4d28a27103c893'
4
- data.tar.gz: 9a4f1916f6bcfd9f64dd25ec4d0f180baa5bcdba
3
+ metadata.gz: 341252ba5950c2280f05fa00ebfd31a96b7a1e67
4
+ data.tar.gz: 3e6a3ff67203d73dbc3d3e2306abe460ac7d7d19
5
5
  SHA512:
6
- metadata.gz: e90a6d17069102cdae33dd4db1cc90518cee06911f3a34554ab4b2ef78364db495a749ac7ef52afa5580dcd687bd56d2fa98847c49444422b2f320f6a7591960
7
- data.tar.gz: 6a6c8e227868cfa4b11641c48e885b972927e304aa4d6bfc5d09c519c46a54fd974a4ec902233b45f62b817a003045e7da00c4ef3590c400a92b4790bc22a72d
6
+ metadata.gz: a8ab55c727d3fd0c1b3c77db108b0ed0e8e6b252613aa3777c1f1307312444d9809383d638120557b6699b0e87e00ba34f073b82e883f1f40dd2e9eb2f64b33c
7
+ data.tar.gz: 27d5c6b05ea339a549dad913e37fd05cb857ceefe8539b241eccca877fafd6134451486f8b15930fd3725641a31ad34cf24af74162da099dc693fbf9074d8138
data/README.md CHANGED
@@ -2,10 +2,10 @@
2
2
 
3
3
  This gem is an attempt to introduce OpenTracing instrumentation into Rails. It's in a very early stage.
4
4
 
5
- When you use `rack-tracer`, the generated operation name corresponds to the request's http method e.g. GET, POST etc.
6
- It's not perfect. You need to dig into the trace to understand with what url it's related.
5
+ The following instrumentation is supported:
7
6
 
8
- The `rails-tracer` introduces another rack middleware, which is intended to be used together with `rack-tracer`, to generate more informative operation names in the form `ControllerName#action`.
7
+ * ActionDispatch - The library introduces a rack middleware, which is intended to be used together with `rack-tracer`, to generate more informative operation names based on information supplied by ActionDispatch.
8
+ * ActiveRecord - The library hooks up into Rails, and instruments all ActiveRecord query.
9
9
 
10
10
  ## Installation
11
11
 
@@ -23,7 +23,14 @@ Or install it yourself as:
23
23
 
24
24
  $ gem install rails-tracer
25
25
 
26
- ## Usage
26
+ ## ActionDispatch
27
+
28
+ When you use `rack-tracer`, the generated operation name corresponds to the request's http method e.g. GET, POST etc.
29
+ It's not perfect. You need to dig into the trace to understand with what url it's related.
30
+
31
+ The `rails-tracer` introduces another rack middleware, which is intended to be used together with `rack-tracer`, to generate more informative operation names in the form `ControllerName#action`.
32
+
33
+ ### Usage
27
34
 
28
35
  ```ruby
29
36
  require 'rack/tracer'
@@ -33,6 +40,41 @@ Rails.configuration.middleware.use(Rack::Tracer)
33
40
  Rails.configuration.middleware.insert_after(Rack::Tracer, Rails::Rack::Tracer)
34
41
  ```
35
42
 
43
+ ## ActiveRecord
44
+
45
+ The library hooks up into Rails using `ActiveSupport::Notifications`, and instruments all `ActiveRecord` query.
46
+
47
+ ### Usage
48
+
49
+ Auto-instrumentation example.
50
+
51
+ ```ruby
52
+ require 'rails/tracer'
53
+
54
+ ActiveRecord::Tracer.instrument(tracer: OpenTracing.global_tracer, active_span: -> { OpenTracing.global_tracer.active_span })
55
+ ```
56
+
57
+ There are times when you might want to skip ActiveRecord's magic, and use connection directly. Still the library
58
+ can help you with span creation. Instead of auto-instrumenting you can manually call `ActiveRecord::Tracer.start_span` as shown below.
59
+
60
+ ```ruby
61
+ def q(name, sql)
62
+ span = ActiveRecord::Tracer.start_span(name,
63
+ tracer: OpenTracing.global_tracer,
64
+ active_span: -> { OpenTracing.global_tracer.active_span },
65
+ sql: sql)
66
+ ActiveRecord::Base.
67
+ connection.
68
+ raw_connection.
69
+ query(sql).
70
+ each(as: :hash)
71
+ ensure
72
+ span&.finish
73
+ end
74
+
75
+ q("FirstUser", "SELECT * FROM users LIMIT 1")
76
+ ```
77
+
36
78
  ## Development
37
79
 
38
80
  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.
@@ -0,0 +1,48 @@
1
+ module ActiveRecord
2
+ module Tracer
3
+ DEFAULT_OPERATION_NAME = "sql.query".freeze
4
+
5
+ class << self
6
+ def instrument(tracer: OpenTracing.global_tracer, active_span: nil)
7
+ ::ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
8
+ ActiveRecord::Tracer.sql(tracer: tracer, active_span: active_span, args: args)
9
+ end
10
+ end
11
+
12
+ def sql(tracer: OpenTracing.global_tracer, active_span: nil, args:)
13
+ _, start, finish, _, payload = *args
14
+
15
+ span = start_span(payload.fetch(:name),
16
+ tracer: tracer,
17
+ active_span: active_span,
18
+ start_time: start,
19
+ sql: payload.fetch(:sql),
20
+ cached: payload.fetch(:cached, false),
21
+ connection_id: payload.fetch(:connection_id))
22
+
23
+ span.finish(end_time: finish)
24
+ end
25
+
26
+
27
+ def start_span(operation_name, tracer: OpenTracing.global_tracer, active_span: nil, start_time: Time.now, **fields)
28
+ connection_config = ::ActiveRecord::Base.connection_config
29
+
30
+ span = tracer.start_span(operation_name || DEFAULT_OPERATION_NAME,
31
+ child_of: active_span.respond_to?(:call) ? active_span.call : active_span,
32
+ start_time: start_time,
33
+ tags: {
34
+ 'component' => 'ActiveRecord',
35
+ 'span.kind' => 'client',
36
+ 'db.user' => connection_config.fetch(:username, 'unknown'),
37
+ 'db.instance' => connection_config.fetch(:database),
38
+ 'db.vendor' => connection_config.fetch(:adapter),
39
+ 'db.connection_id' => fields.fetch(:connection_id, 'unknown'),
40
+ 'db.cached' => fields.fetch(:cached, false),
41
+ 'db.statement' => fields.fetch(:sql),
42
+ 'db.type' => 'sql'
43
+ })
44
+ span
45
+ end
46
+ end
47
+ end
48
+ end
data/lib/rails/tracer.rb CHANGED
@@ -1 +1,2 @@
1
1
  require_relative "rack/tracer"
2
+ require_relative "active_record/tracer"
data/rails-tracer.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "rails-tracer"
7
- spec.version = "0.1.1"
7
+ spec.version = "0.2.0"
8
8
  spec.authors = ["iaintshine"]
9
9
  spec.email = ["bodziomista@gmail.com"]
10
10
 
@@ -22,6 +22,12 @@ Gem::Specification.new do |spec|
22
22
  spec.add_dependency 'opentracing', '~> 0.3.1'
23
23
  spec.add_dependency "rack-tracer", "~> 0.3.0"
24
24
 
25
+ spec.add_development_dependency "rails", "~> 5.1.3"
26
+ spec.add_development_dependency "sqlite3"
27
+ spec.add_development_dependency "puma", "~> 3.7"
28
+ spec.add_development_dependency "rspec-rails", "~> 3.6"
29
+ spec.add_development_dependency "database_cleaner", "~> 1.6"
30
+
25
31
  spec.add_development_dependency "test-tracer", "~> 1.1"
26
32
  spec.add_development_dependency "bundler", "~> 1.15"
27
33
  spec.add_development_dependency "rake", "~> 10.0"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-tracer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - iaintshine
@@ -38,6 +38,76 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.3.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 5.1.3
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 5.1.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
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: puma
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.7'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.7'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.6'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.6'
97
+ - !ruby/object:Gem::Dependency
98
+ name: database_cleaner
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.6'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.6'
41
111
  - !ruby/object:Gem::Dependency
42
112
  name: test-tracer
43
113
  requirement: !ruby/object:Gem::Requirement
@@ -153,6 +223,7 @@ files:
153
223
  - Rakefile
154
224
  - bin/console
155
225
  - bin/setup
226
+ - lib/rails/active_record/tracer.rb
156
227
  - lib/rails/rack/tracer.rb
157
228
  - lib/rails/tracer.rb
158
229
  - rails-tracer.gemspec