skylight 4.3.2 → 5.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +399 -336
  3. data/CLA.md +1 -1
  4. data/CONTRIBUTING.md +2 -8
  5. data/LICENSE.md +7 -17
  6. data/README.md +1 -1
  7. data/ext/extconf.rb +45 -56
  8. data/ext/libskylight.yml +10 -6
  9. data/ext/skylight_native.c +22 -99
  10. data/lib/skylight.rb +201 -14
  11. data/lib/skylight/api.rb +32 -21
  12. data/lib/skylight/cli.rb +48 -46
  13. data/lib/skylight/cli/doctor.rb +62 -63
  14. data/lib/skylight/cli/helpers.rb +19 -19
  15. data/lib/skylight/cli/merger.rb +142 -138
  16. data/lib/skylight/config.rb +634 -199
  17. data/lib/skylight/deprecation.rb +17 -0
  18. data/lib/skylight/errors.rb +23 -9
  19. data/lib/skylight/extensions.rb +95 -0
  20. data/lib/skylight/extensions/source_location.rb +291 -0
  21. data/lib/skylight/formatters/http.rb +18 -0
  22. data/lib/skylight/gc.rb +99 -0
  23. data/lib/skylight/helpers.rb +81 -36
  24. data/lib/skylight/instrumenter.rb +336 -18
  25. data/lib/skylight/middleware.rb +134 -1
  26. data/lib/skylight/native.rb +60 -12
  27. data/lib/skylight/native_ext_fetcher.rb +13 -14
  28. data/lib/skylight/normalizers.rb +157 -0
  29. data/lib/skylight/normalizers/action_controller/process_action.rb +68 -0
  30. data/lib/skylight/normalizers/action_controller/send_file.rb +51 -0
  31. data/lib/skylight/normalizers/action_dispatch/process_middleware.rb +22 -0
  32. data/lib/skylight/normalizers/action_dispatch/route_set.rb +27 -0
  33. data/lib/skylight/normalizers/action_view/render_collection.rb +24 -0
  34. data/lib/skylight/normalizers/action_view/render_layout.rb +25 -0
  35. data/lib/skylight/normalizers/action_view/render_partial.rb +23 -0
  36. data/lib/skylight/normalizers/action_view/render_template.rb +23 -0
  37. data/lib/skylight/normalizers/active_job/perform.rb +90 -0
  38. data/lib/skylight/normalizers/active_model_serializers/render.rb +32 -0
  39. data/lib/skylight/normalizers/active_record/instantiation.rb +16 -0
  40. data/lib/skylight/normalizers/active_record/sql.rb +12 -0
  41. data/lib/skylight/normalizers/active_storage.rb +28 -0
  42. data/lib/skylight/normalizers/active_support/cache.rb +11 -0
  43. data/lib/skylight/normalizers/active_support/cache_clear.rb +16 -0
  44. data/lib/skylight/normalizers/active_support/cache_decrement.rb +16 -0
  45. data/lib/skylight/normalizers/active_support/cache_delete.rb +16 -0
  46. data/lib/skylight/normalizers/active_support/cache_exist.rb +16 -0
  47. data/lib/skylight/normalizers/active_support/cache_fetch_hit.rb +16 -0
  48. data/lib/skylight/normalizers/active_support/cache_generate.rb +16 -0
  49. data/lib/skylight/normalizers/active_support/cache_increment.rb +16 -0
  50. data/lib/skylight/normalizers/active_support/cache_read.rb +16 -0
  51. data/lib/skylight/normalizers/active_support/cache_read_multi.rb +16 -0
  52. data/lib/skylight/normalizers/active_support/cache_write.rb +16 -0
  53. data/lib/skylight/normalizers/coach/handler_finish.rb +44 -0
  54. data/lib/skylight/normalizers/coach/middleware_finish.rb +33 -0
  55. data/lib/skylight/normalizers/couch_potato/query.rb +20 -0
  56. data/lib/skylight/normalizers/data_mapper/sql.rb +12 -0
  57. data/lib/skylight/normalizers/default.rb +24 -0
  58. data/lib/skylight/normalizers/elasticsearch/request.rb +20 -0
  59. data/lib/skylight/normalizers/faraday/request.rb +38 -0
  60. data/lib/skylight/normalizers/grape/endpoint.rb +28 -0
  61. data/lib/skylight/normalizers/grape/endpoint_render.rb +25 -0
  62. data/lib/skylight/normalizers/grape/endpoint_run.rb +39 -0
  63. data/lib/skylight/normalizers/grape/endpoint_run_filters.rb +20 -0
  64. data/lib/skylight/normalizers/grape/format_response.rb +20 -0
  65. data/lib/skylight/normalizers/graphiti/render.rb +22 -0
  66. data/lib/skylight/normalizers/graphiti/resolve.rb +31 -0
  67. data/lib/skylight/normalizers/graphql/base.rb +127 -0
  68. data/lib/skylight/normalizers/render.rb +79 -0
  69. data/lib/skylight/normalizers/sequel/sql.rb +12 -0
  70. data/lib/skylight/normalizers/shrine.rb +32 -0
  71. data/lib/skylight/normalizers/sql.rb +45 -0
  72. data/lib/skylight/probes.rb +173 -0
  73. data/lib/skylight/probes/action_controller.rb +52 -0
  74. data/lib/skylight/probes/action_dispatch.rb +2 -0
  75. data/lib/skylight/probes/action_dispatch/request_id.rb +33 -0
  76. data/lib/skylight/probes/action_dispatch/routing/route_set.rb +30 -0
  77. data/lib/skylight/probes/action_view.rb +42 -0
  78. data/lib/skylight/probes/active_job.rb +27 -0
  79. data/lib/skylight/probes/active_job_enqueue.rb +35 -0
  80. data/lib/skylight/probes/active_model_serializers.rb +50 -0
  81. data/lib/skylight/probes/delayed_job.rb +144 -0
  82. data/lib/skylight/probes/elasticsearch.rb +36 -0
  83. data/lib/skylight/probes/excon.rb +25 -0
  84. data/lib/skylight/probes/excon/middleware.rb +65 -0
  85. data/lib/skylight/probes/faraday.rb +23 -0
  86. data/lib/skylight/probes/graphql.rb +38 -0
  87. data/lib/skylight/probes/httpclient.rb +44 -0
  88. data/lib/skylight/probes/middleware.rb +135 -0
  89. data/lib/skylight/probes/mongo.rb +156 -0
  90. data/lib/skylight/probes/mongoid.rb +13 -0
  91. data/lib/skylight/probes/net_http.rb +54 -0
  92. data/lib/skylight/probes/redis.rb +51 -0
  93. data/lib/skylight/probes/sequel.rb +29 -0
  94. data/lib/skylight/probes/sinatra.rb +66 -0
  95. data/lib/skylight/probes/sinatra_add_middleware.rb +10 -10
  96. data/lib/skylight/probes/tilt.rb +25 -0
  97. data/lib/skylight/railtie.rb +157 -27
  98. data/lib/skylight/sidekiq.rb +47 -0
  99. data/lib/skylight/subscriber.rb +108 -0
  100. data/lib/skylight/test.rb +151 -0
  101. data/lib/skylight/trace.rb +325 -22
  102. data/lib/skylight/user_config.rb +58 -0
  103. data/lib/skylight/util.rb +12 -0
  104. data/lib/skylight/util/allocation_free.rb +26 -0
  105. data/lib/skylight/util/clock.rb +57 -0
  106. data/lib/skylight/util/component.rb +22 -22
  107. data/lib/skylight/util/deploy.rb +16 -21
  108. data/lib/skylight/util/gzip.rb +20 -0
  109. data/lib/skylight/util/http.rb +106 -113
  110. data/lib/skylight/util/instrumenter_method.rb +26 -0
  111. data/lib/skylight/util/logging.rb +136 -0
  112. data/lib/skylight/util/lru_cache.rb +36 -0
  113. data/lib/skylight/util/platform.rb +1 -5
  114. data/lib/skylight/util/ssl.rb +1 -25
  115. data/lib/skylight/vendor/cli/thor/rake_compat.rb +1 -1
  116. data/lib/skylight/version.rb +5 -1
  117. data/lib/skylight/vm/gc.rb +60 -0
  118. metadata +126 -13
@@ -0,0 +1,25 @@
1
+ module Skylight
2
+ module Probes
3
+ module Excon
4
+ # Probe for instrumenting Excon requests. Installs {Excon::Middleware} to achieve this.
5
+ class Probe
6
+ def install
7
+ if defined?(::Excon::Middleware)
8
+ # Don't require until installation since it depends on Excon being loaded
9
+ require "skylight/probes/excon/middleware"
10
+
11
+ idx = ::Excon.defaults[:middlewares].index(::Excon::Middleware::Instrumentor)
12
+
13
+ # TODO: Handle possibility of idx being nil
14
+ ::Excon.defaults[:middlewares].insert(idx, Probes::Excon::Middleware)
15
+ else
16
+ Skylight.error "The installed version of Excon doesn't support Middlewares. " \
17
+ "The Excon probe will be disabled."
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ register(:excon, "Excon", "excon", Excon::Probe.new)
24
+ end
25
+ end
@@ -0,0 +1,65 @@
1
+ require "skylight/formatters/http"
2
+
3
+ module Skylight
4
+ module Probes
5
+ module Excon
6
+ # Middleware for Excon that instruments requests
7
+ class Middleware < ::Excon::Middleware::Base
8
+ def initialize(*)
9
+ @requests = {}.compare_by_identity
10
+ super
11
+ end
12
+
13
+ # TODO: Review the following:
14
+ # - Consider whether a LIFO queue would be sufficient
15
+ # - Check that errors can't be called without a request
16
+
17
+ def request_call(datum)
18
+ begin_instrumentation(datum)
19
+ super
20
+ end
21
+
22
+ def response_call(datum)
23
+ super
24
+ ensure
25
+ end_instrumentation(datum)
26
+ end
27
+
28
+ def error_call(datum)
29
+ super
30
+ ensure
31
+ end_instrumentation(datum)
32
+ end
33
+
34
+ private
35
+
36
+ def begin_instrumentation(datum)
37
+ method = datum[:method].to_s
38
+ scheme = datum[:scheme]
39
+ host = datum[:host]
40
+
41
+ # TODO: Maybe don't show other default ports like 443
42
+ port = datum[:port] == 80 ? nil : datum[:port]
43
+ path = datum[:path]
44
+ query = datum[:query]
45
+
46
+ opts = Formatters::HTTP.build_opts(method, scheme, host, port, path, query)
47
+
48
+ @requests[datum] = Skylight.instrument(opts)
49
+ rescue Exception => e
50
+ Skylight.error "failed to begin instrumentation for Excon; msg=%s", e.message
51
+ end
52
+
53
+ def end_instrumentation(datum)
54
+ if (request = @requests.delete(datum))
55
+ meta = {}
56
+ meta[:exception_object] = datum[:error] if datum[:error].is_a?(Exception)
57
+ Skylight.done(request, meta)
58
+ end
59
+ rescue Exception => e
60
+ Skylight.error "failed to end instrumentation for Excon; msg=%s", e.message
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,23 @@
1
+ module Skylight
2
+ module Probes
3
+ module Faraday
4
+ module Instrumentation
5
+ def builder
6
+ unless defined?(@__sk__setup)
7
+ @__sk__setup = true
8
+ @builder.insert 0, ::Faraday::Request::Instrumentation
9
+ end
10
+ @builder
11
+ end
12
+ end
13
+
14
+ class Probe
15
+ def install
16
+ ::Faraday::Connection.prepend(Instrumentation)
17
+ end
18
+ end
19
+ end
20
+
21
+ register(:faraday, "Faraday", "faraday", Faraday::Probe.new)
22
+ end
23
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/inflector"
4
+
5
+ module Skylight
6
+ module Probes
7
+ module GraphQL
8
+ module Instrumentation
9
+ def initialize(*, **)
10
+ super
11
+
12
+ return unless defined?(@tracers)
13
+
14
+ unless @tracers.include?(::GraphQL::Tracing::ActiveSupportNotificationsTracing)
15
+ @tracers << ::GraphQL::Tracing::ActiveSupportNotificationsTracing
16
+ end
17
+ end
18
+ end
19
+
20
+ class Probe
21
+ def install
22
+ tracing_klass_name = "::GraphQL::Tracing::ActiveSupportNotificationsTracing"
23
+ klasses_to_probe = %w[::GraphQL::Execution::Multiplex ::GraphQL::Query]
24
+
25
+ return unless ([tracing_klass_name] + klasses_to_probe).all?(&method(:safe_constantize))
26
+
27
+ klasses_to_probe.each { |klass_name| safe_constantize(klass_name).prepend(Instrumentation) }
28
+ end
29
+
30
+ def safe_constantize(klass_name)
31
+ ActiveSupport::Inflector.safe_constantize(klass_name)
32
+ end
33
+ end
34
+ end
35
+
36
+ register(:graphql, "GraphQL", "graphql", GraphQL::Probe.new)
37
+ end
38
+ end
@@ -0,0 +1,44 @@
1
+ require "skylight/formatters/http"
2
+
3
+ module Skylight
4
+ module Probes
5
+ module HTTPClient
6
+ module Instrumentation
7
+ # HTTPClient has request methods on the class object itself,
8
+ # but they internally instantiate a client and perform the method
9
+ # on that, so this instance method override will cover both
10
+ # `HTTPClient.get(...)` and `HTTPClient.new.get(...)`
11
+
12
+ def do_request(method, uri, *)
13
+ return super if Probes::HTTPClient::Probe.disabled?
14
+
15
+ opts = Formatters::HTTP.build_opts(method, uri.scheme, uri.host, uri.port, uri.path, uri.query)
16
+
17
+ Skylight.instrument(opts) { super }
18
+ end
19
+ end
20
+
21
+ class Probe
22
+ DISABLED_KEY = :__skylight_httpclient_disabled
23
+
24
+ def self.disable
25
+ old_value = Thread.current[DISABLED_KEY]
26
+ Thread.current[DISABLED_KEY] = true
27
+ yield
28
+ ensure
29
+ Thread.current[DISABLED_KEY] = old_value
30
+ end
31
+
32
+ def self.disabled?
33
+ !!Thread.current[DISABLED_KEY]
34
+ end
35
+
36
+ def install
37
+ ::HTTPClient.prepend(Instrumentation)
38
+ end
39
+ end
40
+ end
41
+
42
+ register(:httpclient, "HTTPClient", "httpclient", HTTPClient::Probe.new)
43
+ end
44
+ end
@@ -0,0 +1,135 @@
1
+ module Skylight
2
+ module Probes
3
+ module Middleware
4
+ # for Rails >= 6.0, which includes InstrumentationProxy
5
+ module InstrumentationExtensions
6
+ def initialize(middleware, class_name)
7
+ super
8
+
9
+ # NOTE: Caching here leads to better performance, but will not notice if the method is overridden
10
+ # We don't have access to the config here so we can't check whether source locations are enabled.
11
+ # However, this only happens once per middleware so it should be minimal impact.
12
+ @payload[:sk_source_location] =
13
+ begin
14
+ if middleware.is_a?(Proc)
15
+ middleware.source_location
16
+ elsif middleware.respond_to?(:call)
17
+ middleware.method(:call).source_location
18
+ end
19
+ rescue StandardError
20
+ nil
21
+ end
22
+ end
23
+ end
24
+
25
+ # for Rails <= 5.2 ActionDispatch::MiddlewareStack::Middleware
26
+ module Instrumentation
27
+ def build(*)
28
+ sk_instrument_middleware(super)
29
+ end
30
+
31
+ def sk_instrument_middleware(middleware)
32
+ return middleware if middleware.is_a?(Skylight::Middleware)
33
+
34
+ # Not sure how this would actually happen
35
+ return middleware if middleware.respond_to?(:__has_sk__)
36
+
37
+ # On Rails 3, ActionDispatch::Session::CookieStore is frozen, for one
38
+ return middleware if middleware.frozen?
39
+
40
+ Skylight::Probes::Middleware::Probe.add_instrumentation(middleware)
41
+
42
+ middleware
43
+ end
44
+ end
45
+
46
+ class Probe
47
+ DISABLED_KEY = :__skylight_middleware_disabled
48
+
49
+ def self.disable!
50
+ @disabled = true
51
+ end
52
+
53
+ def self.enable!
54
+ @disabled = false
55
+ end
56
+
57
+ def self.disabled?
58
+ !!@disabled
59
+ end
60
+
61
+ module InstanceInstrumentation
62
+ def call(*args)
63
+ return super(*args) if Skylight::Probes::Middleware::Probe.disabled?
64
+
65
+ trace = Skylight.instrumenter&.current_trace
66
+ return super(*args) unless trace
67
+
68
+ begin
69
+ name = self.class.name || __sk_default_name
70
+
71
+ trace.endpoint = name
72
+
73
+ source_file, source_line = method(__method__).super_method.source_location
74
+
75
+ spans =
76
+ Skylight.instrument(
77
+ title: name,
78
+ category: __sk_category,
79
+ source_file: source_file,
80
+ source_line: source_line
81
+ )
82
+
83
+ proxied_response =
84
+ Skylight::Middleware.with_after_close(super(*args), debug_identifier: "Middleware: #{name}") do
85
+ Skylight.done(spans)
86
+ end
87
+ rescue Exception => e
88
+ Skylight.done(spans, exception_object: e)
89
+ raise
90
+ ensure
91
+ unless e || proxied_response
92
+ # If we've gotten to this point, the most likely scenario is that
93
+ # a throw/catch has bypassed a portion of the callstack. Since these spans would not otherwise
94
+ # be closed, mark them deferred to indicate that they should be implicitly closed.
95
+ # See Trace#deferred_spans or Trace#stop for more information.
96
+ Skylight.done(spans, defer: true)
97
+ end
98
+ end
99
+ end
100
+
101
+ def __sk_default_name
102
+ "Anonymous Middleware"
103
+ end
104
+
105
+ def __sk_category
106
+ "rack.middleware"
107
+ end
108
+
109
+ def __has_sk__
110
+ true
111
+ end
112
+ end
113
+
114
+ def self.add_instrumentation(middleware)
115
+ middleware.singleton_class.prepend(InstanceInstrumentation)
116
+ end
117
+
118
+ def install
119
+ if defined?(::ActionDispatch::MiddlewareStack::InstrumentationProxy)
120
+ ::ActionDispatch::MiddlewareStack::InstrumentationProxy.prepend(InstrumentationExtensions)
121
+ else
122
+ ::ActionDispatch::MiddlewareStack::Middleware.prepend(Instrumentation)
123
+ end
124
+ end
125
+ end
126
+ end
127
+
128
+ register(
129
+ :middleware,
130
+ "ActionDispatch::MiddlewareStack::Middleware",
131
+ "actionpack/action_dispatch",
132
+ Middleware::Probe.new
133
+ )
134
+ end
135
+ end
@@ -0,0 +1,156 @@
1
+ module Skylight
2
+ module Probes
3
+ module Mongo
4
+ CAT = "db.mongo.command".freeze
5
+
6
+ class Probe
7
+ def install
8
+ ::Mongo::Monitoring::Global.subscribe(::Mongo::Monitoring::COMMAND, Subscriber.new)
9
+ end
10
+ end
11
+
12
+ class Subscriber
13
+ include Skylight::Util::Logging
14
+
15
+ COMMANDS = %i[insert find count distinct update findandmodify findAndModify delete aggregate].freeze
16
+
17
+ COMMAND_NAMES = { findandmodify: "findAndModify".freeze }.freeze
18
+
19
+ def initialize
20
+ @events = {}
21
+ end
22
+
23
+ def started(event)
24
+ begin_instrumentation(event)
25
+ end
26
+
27
+ def succeeded(event)
28
+ end_instrumentation(event)
29
+ end
30
+
31
+ def failed(event)
32
+ end_instrumentation(event)
33
+ end
34
+
35
+ # For logging
36
+ def config
37
+ Skylight.config
38
+ end
39
+
40
+ private
41
+
42
+ def begin_instrumentation(event)
43
+ return unless COMMANDS.include?(event.command_name.to_sym)
44
+
45
+ command_name = COMMAND_NAMES[event.command_name] || event.command_name.to_s
46
+
47
+ title = "#{event.database_name}.#{command_name}"
48
+
49
+ command = event.command
50
+
51
+ # Not sure if this will always exist
52
+ # Delete so the description will be less redundant
53
+ if (target = command[event.command_name])
54
+ title << " #{target}"
55
+ end
56
+
57
+ payload = {}
58
+
59
+ # Ruby Hashes are ordered based on insertion so do the most important ones first
60
+
61
+ add_value("key".freeze, command, payload)
62
+ add_bound("query".freeze, command, payload)
63
+ add_bound("filter".freeze, command, payload)
64
+ add_value("sort".freeze, command, payload)
65
+
66
+ add_bound("update".freeze, command, payload) if command_name == "findAndModify".freeze
67
+
68
+ add_value("remove".freeze, command, payload)
69
+ add_value("new".freeze, command, payload)
70
+
71
+ if (updates = command["updates".freeze])
72
+ # AFAICT the gem generally just sends one item in the updates array
73
+ update = updates[0]
74
+ update_payload = {}
75
+ add_bound("q".freeze, update, update_payload)
76
+ add_bound("u".freeze, update, update_payload)
77
+ add_value("multi".freeze, update, update_payload)
78
+ add_value("upsert".freeze, update, update_payload)
79
+
80
+ payload["updates".freeze] = [update_payload]
81
+
82
+ payload["updates".freeze] << "..." if updates.length > 1
83
+ end
84
+
85
+ if (deletes = command["deletes".freeze])
86
+ # AFAICT the gem generally just sends one item in the updates array
87
+ delete = deletes[0]
88
+ delete_payload = {}
89
+ add_bound("q".freeze, delete, delete_payload)
90
+ add_value("limit".freeze, delete, delete_payload)
91
+
92
+ payload["deletes".freeze] = [delete_payload]
93
+
94
+ payload["deletes".freeze] << "..." if deletes.length > 1
95
+ end
96
+
97
+ if (pipeline = command["pipeline".freeze])
98
+ payload["pipeline".freeze] = pipeline.map { |segment| extract_binds(segment) }
99
+ end
100
+
101
+ # We're ignoring documents from insert because they could have completely inconsistent
102
+ # format which would make it hard to merge.
103
+
104
+ opts = {
105
+ category: CAT,
106
+ title: title,
107
+ description: payload.empty? ? nil : payload.to_json,
108
+ meta: {
109
+ database: event.database_name
110
+ },
111
+ internal: true
112
+ }
113
+
114
+ @events[event.operation_id] = Skylight.instrument(opts)
115
+ rescue Exception => e
116
+ error "failed to begin instrumentation for Mongo; msg=%s", e.message
117
+ end
118
+
119
+ def end_instrumentation(event)
120
+ if (original_event = @events.delete(event.operation_id))
121
+ meta = {}
122
+ if event.is_a?(::Mongo::Monitoring::Event::CommandFailed)
123
+ meta[:exception] = ["CommandFailed", event.message]
124
+ end
125
+ Skylight.done(original_event, meta)
126
+ end
127
+ rescue Exception => e
128
+ error "failed to end instrumentation for Mongo; msg=%s", e.message
129
+ end
130
+
131
+ def add_value(key, command, payload)
132
+ if command.key?(key)
133
+ value = command[key]
134
+ payload[key] = value
135
+ end
136
+ end
137
+
138
+ def add_bound(key, command, payload)
139
+ if (value = command[key])
140
+ payload[key] = extract_binds(value)
141
+ end
142
+ end
143
+
144
+ def extract_binds(hash)
145
+ ret = {}
146
+
147
+ hash.each { |k, v| ret[k] = v.is_a?(Hash) ? extract_binds(v) : "?".freeze }
148
+
149
+ ret
150
+ end
151
+ end
152
+ end
153
+
154
+ register(:mongo, "Mongo", "mongo", Mongo::Probe.new)
155
+ end
156
+ end