gitlab-labkit 0.10.0 → 0.10.1

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
  SHA256:
3
- metadata.gz: 198c31e6a0717be721e68fb8e68e145e24f66ae17338eccc80c0c4e94f638be6
4
- data.tar.gz: e6ee6cf53603f78442d96b89bf8edf2720ee334d9441042dd9167fa9328a4406
3
+ metadata.gz: 2476531528be243ed0069feffe3bbfbc7d557556455616e7d4216aa686422409
4
+ data.tar.gz: 43223820267f18725601573595ba607178788178b1922835becaf7c72d593b2f
5
5
  SHA512:
6
- metadata.gz: 00544f82819064032c9bd3105ed265c56e078e9204702b9b91ed28f4ba760ae4481f1b766b9c0832a9648efa8c55212ef641d008d82912bb0dfab1ca8a9d2868
7
- data.tar.gz: 0b195fec25f24758afb2df4d17dc639388e537258f08182bcc3a5162253b8efab493578e11a99d2bf86370ecd476ea467cce49ffeafe372e5be3def4f15141b3
6
+ metadata.gz: 712fd95e6a754409e83a8b5ee690c1843b233794a33a7461dea2fdc84c49eb8e873c7298ee15087302c32b60faeca83fd3592a80e471640a3c80710fa094e54e
7
+ data.tar.gz: e6d4d048caa06702bd47326574d105f32d9d5e7c27d3c619ac529e4cb35f671a7cfca92e1d9b6460a1b6080c90bc05306f2df17381ca6e104ad30a5171a22bb8
@@ -4,10 +4,15 @@ module Labkit
4
4
  module Tracing
5
5
  # Rails provides classes for instrumenting Rails events
6
6
  module Rails
7
- autoload :ActionViewSubscriber, "labkit/tracing/rails/action_view_subscriber"
8
- autoload :ActiveRecordSubscriber, "labkit/tracing/rails/active_record_subscriber"
9
- autoload :ActiveSupportSubscriber, "labkit/tracing/rails/active_support_subscriber"
7
+ autoload :AbstractInstrumenter, "labkit/tracing/rails/abstract_instrumenter"
8
+ autoload :ActionView, "labkit/tracing/rails/action_view"
9
+ autoload :ActiveRecord, "labkit/tracing/rails/active_record"
10
+ autoload :ActiveSupport, "labkit/tracing/rails/active_support"
10
11
  autoload :RailsCommon, "labkit/tracing/rails/rails_common"
12
+
13
+ ActionViewSubscriber = ActionView::Subscriber
14
+ ActiveRecordSubscriber = ActiveRecord::Subscriber
15
+ ActiveSupportSubscriber = ActiveSupport::Subscriber
11
16
  end
12
17
  end
13
18
  end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/all"
4
+
5
+ module Labkit
6
+ module Tracing
7
+ module Rails
8
+ # https://edgeapi.rubyonrails.org/classes/ActiveSupport/Notifications/Instrumenter.html#method-c-new
9
+ class AbstractInstrumenter
10
+ def start(name, id, payload)
11
+ scope = OpenTracing.start_active_span(span_name(payload))
12
+
13
+ scope_stack.push scope
14
+ end
15
+
16
+ def finish(name, id, payload)
17
+ scope = scope_stack.pop
18
+ span = scope.span
19
+
20
+ exception = payload[:exception]
21
+ Labkit::Tracing::TracingUtils.log_exception_on_span(span, exception) if exception
22
+
23
+ tags(payload).each do |k, v|
24
+ span.set_tag(k, v)
25
+ end
26
+
27
+ scope.close
28
+ end
29
+
30
+ def scope_stack
31
+ Thread.current[:_labkit_trace_scope_stack] ||= []
32
+ end
33
+
34
+ def span_name(payload)
35
+ raise "span_name not implemented"
36
+ end
37
+
38
+ def tags(payload)
39
+ {}
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Labkit
4
+ module Tracing
5
+ module Rails
6
+ module ActionView
7
+ autoload :RenderCollectionInstrumenter, "labkit/tracing/rails/action_view/render_collection_instrumenter"
8
+ autoload :RenderPartialInstrumenter, "labkit/tracing/rails/action_view/render_partial_instrumenter"
9
+ autoload :RenderTemplateInstrumenter, "labkit/tracing/rails/action_view/render_template_instrumenter"
10
+ autoload :Subscriber, "labkit/tracing/rails/action_view/subscriber"
11
+
12
+ COMPONENT_TAG = "ActionView"
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Labkit
4
+ module Tracing
5
+ module Rails
6
+ module ActionView
7
+ # For more information on the payloads: https://guides.rubyonrails.org/active_support_instrumentation.html
8
+ class RenderCollectionInstrumenter < AbstractInstrumenter
9
+ def span_name(payload)
10
+ "render_collection"
11
+ end
12
+
13
+ def tags(payload)
14
+ {
15
+ "component" => COMPONENT_TAG,
16
+ "template.id" => payload[:identifier],
17
+ "template.count" => payload[:count] || 0,
18
+ "template.cache.hits" => payload[:cache_hits] || 0,
19
+ }
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Labkit
4
+ module Tracing
5
+ module Rails
6
+ module ActionView
7
+ # For more information on the payloads: https://guides.rubyonrails.org/active_support_instrumentation.html
8
+ class RenderPartialInstrumenter < AbstractInstrumenter
9
+ def span_name(payload)
10
+ "render_partial"
11
+ end
12
+
13
+ def tags(payload)
14
+ { "component" => COMPONENT_TAG, "template.id" => payload[:identifier] }
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Labkit
4
+ module Tracing
5
+ module Rails
6
+ module ActionView
7
+ # For more information on the payloads: https://guides.rubyonrails.org/active_support_instrumentation.html
8
+ class RenderTemplateInstrumenter < AbstractInstrumenter
9
+ def span_name(payload)
10
+ "render_template"
11
+ end
12
+
13
+ def tags(payload)
14
+ { "component" => COMPONENT_TAG, "template.id" => payload[:identifier], "template.layout" => payload[:layout] }
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Labkit
4
+ module Tracing
5
+ module Rails
6
+ module ActionView
7
+ # ActionView bridges action view notifications to
8
+ # the distributed tracing subsystem
9
+ class Subscriber
10
+ include RailsCommon
11
+
12
+ RENDER_TEMPLATE_NOTIFICATION_TOPIC = "render_template.action_view"
13
+ RENDER_COLLECTION_NOTIFICATION_TOPIC = "render_collection.action_view"
14
+ RENDER_PARTIAL_NOTIFICATION_TOPIC = "render_partial.action_view"
15
+
16
+ # Instruments Rails ActionView events for opentracing.
17
+ # Returns a lambda, which, when called will unsubscribe from the notifications
18
+ def self.instrument
19
+ subscriptions = [
20
+ ::ActiveSupport::Notifications.subscribe(RENDER_TEMPLATE_NOTIFICATION_TOPIC, RenderTemplateInstrumenter.new),
21
+ ::ActiveSupport::Notifications.subscribe(RENDER_COLLECTION_NOTIFICATION_TOPIC, RenderCollectionInstrumenter.new),
22
+ ::ActiveSupport::Notifications.subscribe(RENDER_PARTIAL_NOTIFICATION_TOPIC, RenderPartialInstrumenter.new),
23
+ ]
24
+
25
+ create_unsubscriber subscriptions
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Labkit
4
+ module Tracing
5
+ module Rails
6
+ module ActiveRecord
7
+ autoload :SqlInstrumenter, "labkit/tracing/rails/active_record/sql_instrumenter"
8
+ autoload :Subscriber, "labkit/tracing/rails/active_record/subscriber"
9
+
10
+ COMPONENT_TAG = "ActiveRecord"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Labkit
4
+ module Tracing
5
+ module Rails
6
+ module ActiveRecord
7
+ # For more information on the payloads: https://guides.rubyonrails.org/active_support_instrumentation.html
8
+ class SqlInstrumenter < AbstractInstrumenter
9
+ OPERATION_NAME_PREFIX = "active_record:"
10
+ DEFAULT_OPERATION_NAME = "sqlquery"
11
+
12
+ def span_name(payload)
13
+ OPERATION_NAME_PREFIX + (payload[:name].presence || DEFAULT_OPERATION_NAME)
14
+ end
15
+
16
+ def tags(payload)
17
+ {
18
+ "component" => COMPONENT_TAG,
19
+ "span.kind" => "client",
20
+ "db.type" => "sql",
21
+ "db.connection_id" => payload[:connection_id],
22
+ "db.cached" => payload[:cached] || false,
23
+ "db.statement" => payload[:sql],
24
+ }
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Labkit
4
+ module Tracing
5
+ module Rails
6
+ module ActiveRecord
7
+ # ActiveRecord bridges active record notifications to
8
+ # the distributed tracing subsystem
9
+ class Subscriber
10
+ include RailsCommon
11
+
12
+ ACTIVE_RECORD_NOTIFICATION_TOPIC = "sql.active_record"
13
+
14
+ # Instruments Rails ActiveRecord events for opentracing.
15
+ # Returns a lambda, which, when called will unsubscribe from the notifications
16
+ def self.instrument
17
+ subscription = ::ActiveSupport::Notifications.subscribe(ACTIVE_RECORD_NOTIFICATION_TOPIC, SqlInstrumenter.new)
18
+
19
+ create_unsubscriber [subscription]
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Labkit
4
+ module Tracing
5
+ module Rails
6
+ module ActiveSupport
7
+ autoload :CacheDeleteInstrumenter, "labkit/tracing/rails/active_support/cache_delete_instrumenter"
8
+ autoload :CacheFetchHitInstrumenter, "labkit/tracing/rails/active_support/cache_fetch_hit_instrumenter"
9
+ autoload :CacheGenerateInstrumenter, "labkit/tracing/rails/active_support/cache_generate_instrumenter"
10
+ autoload :CacheReadInstrumenter, "labkit/tracing/rails/active_support/cache_read_instrumenter"
11
+ autoload :CacheWriteInstrumenter, "labkit/tracing/rails/active_support/cache_write_instrumenter"
12
+ autoload :Subscriber, "labkit/tracing/rails/active_support/subscriber"
13
+
14
+ COMPONENT_TAG = "ActiveSupport"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Labkit
4
+ module Tracing
5
+ module Rails
6
+ module ActiveSupport
7
+ # For more information on the payloads: https://guides.rubyonrails.org/active_support_instrumentation.html
8
+ class CacheDeleteInstrumenter < AbstractInstrumenter
9
+ def span_name(payload)
10
+ "cache_delete"
11
+ end
12
+
13
+ def tags(payload)
14
+ { "component" => COMPONENT_TAG, "cache.key" => payload[:key] }
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Labkit
4
+ module Tracing
5
+ module Rails
6
+ module ActiveSupport
7
+ # For more information on the payloads: https://guides.rubyonrails.org/active_support_instrumentation.html
8
+ class CacheFetchHitInstrumenter < AbstractInstrumenter
9
+ def span_name(payload)
10
+ "cache_fetch_hit"
11
+ end
12
+
13
+ def tags(payload)
14
+ { "component" => COMPONENT_TAG, "cache.key" => payload[:key] }
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Labkit
4
+ module Tracing
5
+ module Rails
6
+ module ActiveSupport
7
+ # For more information on the payloads: https://guides.rubyonrails.org/active_support_instrumentation.html
8
+ class CacheGenerateInstrumenter < AbstractInstrumenter
9
+ def span_name(payload)
10
+ "cache_generate"
11
+ end
12
+
13
+ def tags(payload)
14
+ { "component" => COMPONENT_TAG, "cache.key" => payload[:key] }
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Labkit
4
+ module Tracing
5
+ module Rails
6
+ module ActiveSupport
7
+ # For more information on the payloads: https://guides.rubyonrails.org/active_support_instrumentation.html
8
+ class CacheReadInstrumenter < AbstractInstrumenter
9
+ def span_name(payload)
10
+ "cache_read"
11
+ end
12
+
13
+ def tags(payload)
14
+ {
15
+ "component" => COMPONENT_TAG,
16
+ "cache.key" => payload[:key],
17
+ "cache.hit" => payload[:hit],
18
+ "cache.super_operation" => payload[:super_operation],
19
+ }
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Labkit
4
+ module Tracing
5
+ module Rails
6
+ module ActiveSupport
7
+ # For more information on the payloads: https://guides.rubyonrails.org/active_support_instrumentation.html
8
+ class CacheWriteInstrumenter < AbstractInstrumenter
9
+ def span_name(payload)
10
+ "cache_write"
11
+ end
12
+
13
+ def tags(payload)
14
+ { "component" => COMPONENT_TAG, "cache.key" => payload[:key] }
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Labkit
4
+ module Tracing
5
+ module Rails
6
+ module ActiveSupport
7
+ # ActiveSupport bridges action active support notifications to
8
+ # the distributed tracing subsystem
9
+ class Subscriber
10
+ include RailsCommon
11
+
12
+ CACHE_READ_TOPIC = "cache_read.active_support"
13
+ CACHE_GENERATE_TOPIC = "cache_generate.active_support"
14
+ CACHE_FETCH_HIT_TOPIC = "cache_fetch_hit.active_support"
15
+ CACHE_WRITE_TOPIC = "cache_write.active_support"
16
+ CACHE_DELETE_TOPIC = "cache_delete.active_support"
17
+
18
+ # Instruments Rails ActiveSupport events for opentracing.
19
+ # Returns a lambda, which, when called will unsubscribe from the notifications
20
+ def self.instrument
21
+ subscriptions = [
22
+ ::ActiveSupport::Notifications.subscribe(CACHE_READ_TOPIC, CacheReadInstrumenter.new),
23
+ ::ActiveSupport::Notifications.subscribe(CACHE_GENERATE_TOPIC, CacheGenerateInstrumenter.new),
24
+ ::ActiveSupport::Notifications.subscribe(CACHE_FETCH_HIT_TOPIC, CacheFetchHitInstrumenter.new),
25
+ ::ActiveSupport::Notifications.subscribe(CACHE_WRITE_TOPIC, CacheWriteInstrumenter.new),
26
+ ::ActiveSupport::Notifications.subscribe(CACHE_DELETE_TOPIC, CacheDeleteInstrumenter.new),
27
+ ]
28
+
29
+ create_unsubscriber subscriptions
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -8,19 +8,13 @@ module Labkit
8
8
  # RailsCommon is a mixin for providing instrumentation
9
9
  # functionality for the rails instrumentation classes
10
10
  module RailsCommon
11
- extend ActiveSupport::Concern
11
+ extend ::ActiveSupport::Concern
12
12
 
13
13
  class_methods do
14
14
  def create_unsubscriber(subscriptions)
15
- -> { subscriptions.each { |subscriber| ActiveSupport::Notifications.unsubscribe(subscriber) } }
15
+ -> { subscriptions.each { |subscriber| ::ActiveSupport::Notifications.unsubscribe(subscriber) } }
16
16
  end
17
17
  end
18
-
19
- def generate_span_for_notification(operation_name, start, finish, payload, tags)
20
- exception = payload[:exception]
21
-
22
- TracingUtils.postnotify_span(operation_name, start, finish, tags: tags, exception: exception)
23
- end
24
18
  end
25
19
  end
26
20
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitlab-labkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.10.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Newdigate
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-25 00:00:00.000000000 Z
11
+ date: 2020-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -301,9 +301,22 @@ files:
301
301
  - lib/labkit/tracing/jaeger_factory.rb
302
302
  - lib/labkit/tracing/rack_middleware.rb
303
303
  - lib/labkit/tracing/rails.rb
304
- - lib/labkit/tracing/rails/action_view_subscriber.rb
305
- - lib/labkit/tracing/rails/active_record_subscriber.rb
306
- - lib/labkit/tracing/rails/active_support_subscriber.rb
304
+ - lib/labkit/tracing/rails/abstract_instrumenter.rb
305
+ - lib/labkit/tracing/rails/action_view.rb
306
+ - lib/labkit/tracing/rails/action_view/render_collection_instrumenter.rb
307
+ - lib/labkit/tracing/rails/action_view/render_partial_instrumenter.rb
308
+ - lib/labkit/tracing/rails/action_view/render_template_instrumenter.rb
309
+ - lib/labkit/tracing/rails/action_view/subscriber.rb
310
+ - lib/labkit/tracing/rails/active_record.rb
311
+ - lib/labkit/tracing/rails/active_record/sql_instrumenter.rb
312
+ - lib/labkit/tracing/rails/active_record/subscriber.rb
313
+ - lib/labkit/tracing/rails/active_support.rb
314
+ - lib/labkit/tracing/rails/active_support/cache_delete_instrumenter.rb
315
+ - lib/labkit/tracing/rails/active_support/cache_fetch_hit_instrumenter.rb
316
+ - lib/labkit/tracing/rails/active_support/cache_generate_instrumenter.rb
317
+ - lib/labkit/tracing/rails/active_support/cache_read_instrumenter.rb
318
+ - lib/labkit/tracing/rails/active_support/cache_write_instrumenter.rb
319
+ - lib/labkit/tracing/rails/active_support/subscriber.rb
307
320
  - lib/labkit/tracing/rails/rails_common.rb
308
321
  - lib/labkit/tracing/redis.rb
309
322
  - lib/labkit/tracing/redis/redis_interceptor.rb
@@ -1,70 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Labkit
4
- module Tracing
5
- module Rails
6
- # ActionViewSubscriber bridges action view notifications to
7
- # the distributed tracing subsystem
8
- class ActionViewSubscriber
9
- include RailsCommon
10
-
11
- COMPONENT_TAG = "ActionView"
12
- RENDER_TEMPLATE_NOTIFICATION_TOPIC = "render_template.action_view"
13
- RENDER_COLLECTION_NOTIFICATION_TOPIC = "render_collection.action_view"
14
- RENDER_PARTIAL_NOTIFICATION_TOPIC = "render_partial.action_view"
15
-
16
- # Instruments Rails ActionView events for opentracing.
17
- # Returns a lambda, which, when called will unsubscribe from the notifications
18
- def self.instrument
19
- subscriber = new
20
-
21
- subscriptions = [
22
- ActiveSupport::Notifications.subscribe(RENDER_TEMPLATE_NOTIFICATION_TOPIC) do |_, start, finish, _, payload|
23
- subscriber.notify_render_template(start, finish, payload)
24
- end,
25
- ActiveSupport::Notifications.subscribe(RENDER_COLLECTION_NOTIFICATION_TOPIC) do |_, start, finish, _, payload|
26
- subscriber.notify_render_collection(start, finish, payload)
27
- end,
28
- ActiveSupport::Notifications.subscribe(RENDER_PARTIAL_NOTIFICATION_TOPIC) do |_, start, finish, _, payload|
29
- subscriber.notify_render_partial(start, finish, payload)
30
- end,
31
- ]
32
-
33
- create_unsubscriber subscriptions
34
- end
35
-
36
- # For more information on the payloads: https://guides.rubyonrails.org/active_support_instrumentation.html
37
- def notify_render_template(start, finish, payload)
38
- generate_span_for_notification("render_template", start, finish, payload, tags_for_render_template(payload))
39
- end
40
-
41
- def notify_render_collection(start, finish, payload)
42
- generate_span_for_notification("render_collection", start, finish, payload, tags_for_render_collection(payload))
43
- end
44
-
45
- def notify_render_partial(start, finish, payload)
46
- generate_span_for_notification("render_partial", start, finish, payload, tags_for_render_partial(payload))
47
- end
48
-
49
- private
50
-
51
- def tags_for_render_template(payload)
52
- { "component" => COMPONENT_TAG, "template.id" => payload[:identifier], "template.layout" => payload[:layout] }
53
- end
54
-
55
- def tags_for_render_collection(payload)
56
- {
57
- "component" => COMPONENT_TAG,
58
- "template.id" => payload[:identifier],
59
- "template.count" => payload[:count] || 0,
60
- "template.cache.hits" => payload[:cache_hits] || 0,
61
- }
62
- end
63
-
64
- def tags_for_render_partial(payload)
65
- { "component" => COMPONENT_TAG, "template.id" => payload[:identifier] }
66
- end
67
- end
68
- end
69
- end
70
- end
@@ -1,52 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Labkit
4
- module Tracing
5
- module Rails
6
- # ActiveRecordSubscriber bridges active record notifications to
7
- # the distributed tracing subsystem
8
- class ActiveRecordSubscriber
9
- include RailsCommon
10
-
11
- ACTIVE_RECORD_NOTIFICATION_TOPIC = "sql.active_record"
12
- OPERATION_NAME_PREFIX = "active_record:"
13
- DEFAULT_OPERATION_NAME = "sqlquery"
14
-
15
- # Instruments Rails ActiveRecord events for opentracing.
16
- # Returns a lambda, which, when called will unsubscribe from the notifications
17
- def self.instrument
18
- subscriber = new
19
-
20
- subscription =
21
- ActiveSupport::Notifications.subscribe(ACTIVE_RECORD_NOTIFICATION_TOPIC) do |_, start, finish, _, payload|
22
- subscriber.notify(start, finish, payload)
23
- end
24
-
25
- create_unsubscriber [subscription]
26
- end
27
-
28
- # For more information on the payloads: https://guides.rubyonrails.org/active_support_instrumentation.html
29
- def notify(start, finish, payload)
30
- generate_span_for_notification(notification_name(payload), start, finish, payload, tags_for_notification(payload))
31
- end
32
-
33
- private
34
-
35
- def notification_name(payload)
36
- OPERATION_NAME_PREFIX + (payload[:name].presence || DEFAULT_OPERATION_NAME)
37
- end
38
-
39
- def tags_for_notification(payload)
40
- {
41
- "component" => "ActiveRecord",
42
- "span.kind" => "client",
43
- "db.type" => "sql",
44
- "db.connection_id" => payload[:connection_id],
45
- "db.cached" => payload[:cached] || false,
46
- "db.statement" => payload[:sql],
47
- }
48
- end
49
- end
50
- end
51
- end
52
- end
@@ -1,86 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Labkit
4
- module Tracing
5
- module Rails
6
- # ActiveSupport bridges action active support notifications to
7
- # the distributed tracing subsystem
8
- class ActiveSupportSubscriber
9
- include RailsCommon
10
-
11
- COMPONENT_TAG = "ActiveSupport"
12
-
13
- CACHE_READ_TOPIC = "cache_read.active_support"
14
- CACHE_GENERATE_TOPIC = "cache_generate.active_support"
15
- CACHE_FETCH_HIT_TOPIC = "cache_fetch_hit.active_support"
16
- CACHE_WRITE_TOPIC = "cache_write.active_support"
17
- CACHE_DELETE_TOPIC = "cache_delete.active_support"
18
-
19
- # Instruments Rails ActiveSupport events for opentracing.
20
- # Returns a lambda, which, when called will unsubscribe from the notifications
21
- def self.instrument
22
- subscriber = new
23
-
24
- subscriptions = [
25
- ActiveSupport::Notifications.subscribe(CACHE_READ_TOPIC) do |_, start, finish, _, payload|
26
- subscriber.notify_cache_read(start, finish, payload)
27
- end,
28
- ActiveSupport::Notifications.subscribe(CACHE_GENERATE_TOPIC) do |_, start, finish, _, payload|
29
- subscriber.notify_cache_generate(start, finish, payload)
30
- end,
31
- ActiveSupport::Notifications.subscribe(CACHE_FETCH_HIT_TOPIC) do |_, start, finish, _, payload|
32
- subscriber.notify_cache_fetch_hit(start, finish, payload)
33
- end,
34
- ActiveSupport::Notifications.subscribe(CACHE_WRITE_TOPIC) do |_, start, finish, _, payload|
35
- subscriber.notify_cache_write(start, finish, payload)
36
- end,
37
- ActiveSupport::Notifications.subscribe(CACHE_DELETE_TOPIC) do |_, start, finish, _, payload|
38
- subscriber.notify_cache_delete(start, finish, payload)
39
- end,
40
- ]
41
-
42
- create_unsubscriber subscriptions
43
- end
44
-
45
- # For more information on the payloads: https://guides.rubyonrails.org/active_support_instrumentation.html#active-support
46
- def notify_cache_read(start, finish, payload)
47
- generate_span_for_notification("cache_read", start, finish, payload, tags_for_cache_read(payload))
48
- end
49
-
50
- def notify_cache_generate(start, finish, payload)
51
- generate_span_for_notification("cache_generate", start, finish, payload, tags_for_key(payload))
52
- end
53
-
54
- def notify_cache_fetch_hit(start, finish, payload)
55
- generate_span_for_notification("cache_fetch_hit", start, finish, payload, tags_for_key(payload))
56
- end
57
-
58
- def notify_cache_write(start, finish, payload)
59
- generate_span_for_notification("cache_write", start, finish, payload, tags_for_key(payload))
60
- end
61
-
62
- def notify_cache_delete(start, finish, payload)
63
- generate_span_for_notification("cache_delete", start, finish, payload, tags_for_key(payload))
64
- end
65
-
66
- private
67
-
68
- def tags_for_cache_read(payload)
69
- {
70
- "component" => COMPONENT_TAG,
71
- "cache.key" => payload[:key],
72
- "cache.hit" => payload[:hit],
73
- "cache.super_operation" => payload[:super_operation],
74
- }
75
- end
76
-
77
- def tags_for_key(payload)
78
- {
79
- "component" => COMPONENT_TAG,
80
- "cache.key" => payload[:key],
81
- }
82
- end
83
- end
84
- end
85
- end
86
- end