rails-tracer 0.2.0 → 0.3.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: 341252ba5950c2280f05fa00ebfd31a96b7a1e67
4
- data.tar.gz: 3e6a3ff67203d73dbc3d3e2306abe460ac7d7d19
3
+ metadata.gz: a34ff90ac921cc85b1949fee9beaa086bd4d90f6
4
+ data.tar.gz: f930e3bb94f2d4a902b5adaaf39374b694f82668
5
5
  SHA512:
6
- metadata.gz: a8ab55c727d3fd0c1b3c77db108b0ed0e8e6b252613aa3777c1f1307312444d9809383d638120557b6699b0e87e00ba34f073b82e883f1f40dd2e9eb2f64b33c
7
- data.tar.gz: 27d5c6b05ea339a549dad913e37fd05cb857ceefe8539b241eccca877fafd6134451486f8b15930fd3725641a31ad34cf24af74162da099dc693fbf9074d8138
6
+ metadata.gz: 21bd5494160f32eb6acc36eac9fd7894921a453bf8742994f6e5978c41ff1c04384e119b684a15a6ac00be0451182bfa0810841495ecd6c6fa83ebfb91552f04
7
+ data.tar.gz: 7bbf9130efddb14404d00b7a11fc010701618d6a9edd43ac337572a81ddd8e255a501e7cbf2f611e6a58c3da529072863b824ce267fb4dec0f5223c9c8165e98
data/README.md CHANGED
@@ -6,6 +6,7 @@ The following instrumentation is supported:
6
6
 
7
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
8
  * ActiveRecord - The library hooks up into Rails, and instruments all ActiveRecord query.
9
+ * ActionSupport::Cache - The library hooks up into Rails, and instruments cache events.
9
10
 
10
11
  ## Installation
11
12
 
@@ -51,7 +52,8 @@ Auto-instrumentation example.
51
52
  ```ruby
52
53
  require 'rails/tracer'
53
54
 
54
- ActiveRecord::Tracer.instrument(tracer: OpenTracing.global_tracer, active_span: -> { OpenTracing.global_tracer.active_span })
55
+ ActiveRecord::Tracer.instrument(tracer: OpenTracing.global_tracer,
56
+ active_span: -> { OpenTracing.global_tracer.active_span })
55
57
  ```
56
58
 
57
59
  There are times when you might want to skip ActiveRecord's magic, and use connection directly. Still the library
@@ -75,6 +77,39 @@ end
75
77
  q("FirstUser", "SELECT * FROM users LIMIT 1")
76
78
  ```
77
79
 
80
+ ## ActiveSupport::Cache
81
+
82
+ The library hooks up into Rails using `ActiveSupport::Notifications`, and instruments all `ActiveSupport::Cache` events.
83
+
84
+ ### Usage
85
+
86
+ Auto-instrumentation example.
87
+
88
+ ```ruby
89
+ require 'rails/tracer'
90
+
91
+ ActiveSupport::Cache::Tracer.instrument(tracer: OpenTracing.global_tracer,
92
+ active_span: -> { OpenTracing.global_tracer.active_span })
93
+ ```
94
+
95
+ If you want to skip the auto-instrumentation, still the library can help you with span creation and setting up proper tags. Instead of auto-instrumenting, as shown above, you can manually call `ActiveSupport::Cache::Tracer.start_span` as shown below.
96
+
97
+ ```ruby
98
+ def read(key)
99
+ span = ActiveSupport::Cache::Tracer.start_span("InMemoryCache#read",
100
+ tracer: OpenTracing.global_tracer,
101
+ active_span: -> { OpenTracing.global_tracer.active_span },
102
+ key: key)
103
+ result = in_memory_cache[key]
104
+ span.set_tag('cache.hit', !!result)
105
+ result
106
+ ensure
107
+ span&.finish
108
+ end
109
+
110
+ read("user-1")
111
+ ```
112
+
78
113
  ## Development
79
114
 
80
115
  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,50 @@
1
+ module ActiveSupport
2
+ module Cache
3
+ module Tracer
4
+ class << self
5
+ # TODO: In Rails 4.0+ it's possible to subscribe to start events
6
+ def instrument(tracer: OpenTracing.global_tracer, active_span: nil)
7
+ events = %w(read write generate delete clear)
8
+ events.each do |event|
9
+ ActiveSupport::Notifications.subscribe("cache_#{event}.active_support") do |*args|
10
+ ActiveSupport::Cache::Tracer.instrument_event(tracer: tracer,
11
+ active_span: active_span,
12
+ event: event,
13
+ args: args)
14
+ end
15
+ end
16
+ end
17
+
18
+ def instrument_event(tracer: OpenTracing.global_tracer, active_span: nil, event:, args:)
19
+ _, start, finish, _, payload = *args
20
+
21
+ span = start_span("cache.#{event}",
22
+ event: event,
23
+ tracer: tracer,
24
+ active_span: active_span,
25
+ start_time: start,
26
+ **payload)
27
+
28
+ span.finish(end_time: finish)
29
+ end
30
+
31
+ def start_span(operation_name, tracer: OpenTracing.global_tracer, active_span: nil, start_time: Time.now, event:, **fields)
32
+ span = tracer.start_span(operation_name,
33
+ child_of: active_span.respond_to?(:call) ? active_span.call : active_span,
34
+ start_time: start_time,
35
+ tags: {
36
+ 'component' => 'ActiveSupport::Cache',
37
+ 'span.kind' => 'client',
38
+ 'cache.key' => fields.fetch(:key, 'unknown')
39
+ })
40
+
41
+ if event == 'read'
42
+ span.set_tag('cache.hit', fields.fetch(:hit, false))
43
+ end
44
+
45
+ span
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
data/lib/rails/tracer.rb CHANGED
@@ -1,2 +1,3 @@
1
- require_relative "rack/tracer"
2
- require_relative "active_record/tracer"
1
+ require "rails/rack/tracer"
2
+ require "rails/active_record/tracer"
3
+ require "rails/active_support/cache/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.2.0"
7
+ spec.version = "0.3.0"
8
8
  spec.authors = ["iaintshine"]
9
9
  spec.email = ["bodziomista@gmail.com"]
10
10
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-tracer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - iaintshine
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-15 00:00:00.000000000 Z
11
+ date: 2017-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opentracing
@@ -224,6 +224,7 @@ files:
224
224
  - bin/console
225
225
  - bin/setup
226
226
  - lib/rails/active_record/tracer.rb
227
+ - lib/rails/active_support/cache/tracer.rb
227
228
  - lib/rails/rack/tracer.rb
228
229
  - lib/rails/tracer.rb
229
230
  - rails-tracer.gemspec