gitlab-labkit 0.10.0 → 0.12.2

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.
Files changed (27) hide show
  1. checksums.yaml +4 -4
  2. data/lib/labkit/context.rb +2 -1
  3. data/lib/labkit/tracing.rb +4 -0
  4. data/lib/labkit/tracing/rails.rb +8 -3
  5. data/lib/labkit/tracing/rails/abstract_instrumenter.rb +46 -0
  6. data/lib/labkit/tracing/rails/action_view.rb +16 -0
  7. data/lib/labkit/tracing/rails/action_view/render_collection_instrumenter.rb +25 -0
  8. data/lib/labkit/tracing/rails/action_view/render_partial_instrumenter.rb +20 -0
  9. data/lib/labkit/tracing/rails/action_view/render_template_instrumenter.rb +20 -0
  10. data/lib/labkit/tracing/rails/action_view/subscriber.rb +31 -0
  11. data/lib/labkit/tracing/rails/active_record.rb +14 -0
  12. data/lib/labkit/tracing/rails/active_record/sql_instrumenter.rb +30 -0
  13. data/lib/labkit/tracing/rails/active_record/subscriber.rb +25 -0
  14. data/lib/labkit/tracing/rails/active_support.rb +18 -0
  15. data/lib/labkit/tracing/rails/active_support/cache_delete_instrumenter.rb +20 -0
  16. data/lib/labkit/tracing/rails/active_support/cache_fetch_hit_instrumenter.rb +20 -0
  17. data/lib/labkit/tracing/rails/active_support/cache_generate_instrumenter.rb +20 -0
  18. data/lib/labkit/tracing/rails/active_support/cache_read_instrumenter.rb +25 -0
  19. data/lib/labkit/tracing/rails/active_support/cache_write_instrumenter.rb +20 -0
  20. data/lib/labkit/tracing/rails/active_support/subscriber.rb +35 -0
  21. data/lib/labkit/tracing/rails/rails_common.rb +2 -8
  22. data/lib/labkit/tracing/redis/redis_interceptor_helper.rb +2 -1
  23. data/lib/labkit/tracing/tracing_utils.rb +17 -3
  24. metadata +18 -5
  25. data/lib/labkit/tracing/rails/action_view_subscriber.rb +0 -70
  26. data/lib/labkit/tracing/rails/active_record_subscriber.rb +0 -52
  27. data/lib/labkit/tracing/rails/active_support_subscriber.rb +0 -86
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 198c31e6a0717be721e68fb8e68e145e24f66ae17338eccc80c0c4e94f638be6
4
- data.tar.gz: e6ee6cf53603f78442d96b89bf8edf2720ee334d9441042dd9167fa9328a4406
3
+ metadata.gz: f4aea03a17735936eac617c462fb55b220f639d99d13c029de0c15dc7ace629e
4
+ data.tar.gz: fbf9048f2750ce87927864deb37b82d69652b27695aff4876745f509b9d4d4de
5
5
  SHA512:
6
- metadata.gz: 00544f82819064032c9bd3105ed265c56e078e9204702b9b91ed28f4ba760ae4481f1b766b9c0832a9648efa8c55212ef641d008d82912bb0dfab1ca8a9d2868
7
- data.tar.gz: 0b195fec25f24758afb2df4d17dc639388e537258f08182bcc3a5162253b8efab493578e11a99d2bf86370ecd476ea467cce49ffeafe372e5be3def4f15141b3
6
+ metadata.gz: 994ef11d2b1911eba33581b62136d4cc67b112491280710f31ea50ac04a6fb0037aa820ff3315c3146827acd6b017e10c3d6d1817d4a7aabd587c4567bcb2504
7
+ data.tar.gz: c522466510fad218e8ecb0376da5fe0478124f3a2adebfccedda7727b76cfa3d753f10ccb39ca94242746a0feee5d357cfd1ea2078b9b3ca9b823e688887e7dd
@@ -21,7 +21,8 @@ module Labkit
21
21
  LOG_KEY = "meta"
22
22
  CORRELATION_ID_KEY = "correlation_id"
23
23
  RAW_KEYS = [CORRELATION_ID_KEY].freeze
24
- KNOWN_KEYS = %w[user project root_namespace subscription_plan caller_id].freeze
24
+ KNOWN_KEYS = %w[user project root_namespace subscription_plan caller_id
25
+ related_class feature_category].freeze
25
26
 
26
27
  class << self
27
28
  def with_context(attributes = {})
@@ -28,6 +28,10 @@ module Labkit
28
28
  ENV["GITLAB_TRACING_URL"]
29
29
  end
30
30
 
31
+ def self.stacktrace_operations
32
+ @stacktrace_operations ||= Set.new(ENV["GITLAB_TRACING_INCLUDE_STACKTRACE"].to_s.split(",").map(&:strip))
33
+ end
34
+
31
35
  def self.tracing_url_enabled?
32
36
  enabled? && tracing_url_template.present?
33
37
  end
@@ -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,46 @@
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
+ Labkit::Tracing::TracingUtils.log_common_fields_on_span(span, span_name(payload))
21
+
22
+ exception = payload[:exception]
23
+ Labkit::Tracing::TracingUtils.log_exception_on_span(span, exception) if exception
24
+
25
+ tags(payload).each do |k, v|
26
+ span.set_tag(k, v)
27
+ end
28
+
29
+ scope.close
30
+ end
31
+
32
+ def scope_stack
33
+ Thread.current[:_labkit_trace_scope_stack] ||= []
34
+ end
35
+
36
+ def span_name(payload)
37
+ raise "span_name not implemented"
38
+ end
39
+
40
+ def tags(payload)
41
+ {}
42
+ end
43
+ end
44
+ end
45
+ end
46
+ 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
@@ -8,7 +8,7 @@ module Labkit
8
8
  # RedisInterceptorHelper is a helper for the RedisInterceptor. This is not a public API
9
9
  class RedisInterceptorHelper
10
10
  # For optimization, compile this once
11
- MASK_REDIS_RE = /^([\w-]+(?:\W+[\w-]+(?:\W+[\w-]+)?)?)(.?)/.freeze
11
+ MASK_REDIS_RE = /^([\w{}-]+(?:\W+[\w{}-]+(?:\W+[\w{}-]+)?)?)(.?)/.freeze
12
12
 
13
13
  def self.call_with_tracing(command, client)
14
14
  Labkit::Tracing::TracingUtils.with_tracing(operation_name: "redis.call", tags: tags_from_command(command, client)) do |_span|
@@ -99,6 +99,7 @@ module Labkit
99
99
  return "" if argument.empty?
100
100
 
101
101
  matches = argument.match(MASK_REDIS_RE)
102
+
102
103
  matches[2].empty? ? matches[0] : matches[0] + "*****"
103
104
  end
104
105
  private_class_method :mask_redis_arg
@@ -12,9 +12,7 @@ module Labkit
12
12
  scope = tracer.start_active_span(operation_name, child_of: child_of, tags: tags)
13
13
  span = scope.span
14
14
 
15
- # Add correlation details to the span if we have them
16
- correlation_id = Labkit::Correlation::CorrelationId.current_id
17
- span.set_tag("correlation_id", correlation_id) if correlation_id
15
+ log_common_fields_on_span(span, operation_name)
18
16
 
19
17
  begin
20
18
  yield span
@@ -35,11 +33,19 @@ module Labkit
35
33
  def self.postnotify_span(operation_name, start_time, end_time, tags: nil, child_of: nil, exception: nil)
36
34
  span = OpenTracing.start_span(operation_name, start_time: start_time, tags: tags, child_of: child_of)
37
35
 
36
+ log_common_fields_on_span(span, operation_name)
38
37
  log_exception_on_span(span, exception) if exception
39
38
 
40
39
  span.finish(end_time: end_time)
41
40
  end
42
41
 
42
+ # Add common fields to a span
43
+ def self.log_common_fields_on_span(span, operation_name)
44
+ correlation_id = Labkit::Correlation::CorrelationId.current_id
45
+ span.set_tag("correlation_id", correlation_id) if correlation_id
46
+ span.log_kv(stack: caller.join('\n')) if include_stacktrace?(operation_name)
47
+ end
48
+
43
49
  # Add exception logging to a span
44
50
  def self.log_exception_on_span(span, exception)
45
51
  span.set_tag("error", true)
@@ -60,6 +66,14 @@ module Labkit
60
66
  { :"event" => "error", :"error.kind" => exception.class.to_s, :"error.object" => Labkit::Logging::Sanitizer.sanitize_field(exception.to_s) }
61
67
  end
62
68
  end
69
+
70
+ def self.include_stacktrace?(operation_name)
71
+ @include_stacktrace ||= Hash.new do |result, name|
72
+ result[name] = Tracing.stacktrace_operations.any? { |stacktrace_operation| name.starts_with?(stacktrace_operation) }
73
+ end
74
+
75
+ @include_stacktrace[operation_name]
76
+ end
63
77
  end
64
78
  end
65
79
  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.12.2
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-10-08 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