appsignal 4.10.1-java → 5.0.0.rc.1-java

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 (89) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +19 -0
  3. data/Rakefile +87 -0
  4. data/appsignal.gemspec +8 -0
  5. data/build_matrix.yml +14 -1
  6. data/ext/appsignal_extension.c +14 -0
  7. data/lib/appsignal/backends.rb +55 -0
  8. data/lib/appsignal/config.rb +140 -1
  9. data/lib/appsignal/demo.rb +1 -1
  10. data/lib/appsignal/event_formatter/action_view/render_formatter.rb +34 -22
  11. data/lib/appsignal/event_formatter/active_job/perform_formatter.rb +35 -0
  12. data/lib/appsignal/event_formatter/active_record/sql_formatter.rb +19 -0
  13. data/lib/appsignal/event_formatter/elastic_search/search_formatter.rb +27 -0
  14. data/lib/appsignal/event_formatter/recorded_elsewhere.rb +17 -0
  15. data/lib/appsignal/event_formatter/rom/sql_formatter.rb +24 -0
  16. data/lib/appsignal/event_formatter/sequel/sql_formatter.rb +5 -0
  17. data/lib/appsignal/event_formatter/view_component/render_formatter.rb +21 -10
  18. data/lib/appsignal/event_formatter.rb +78 -0
  19. data/lib/appsignal/extension.rb +4 -0
  20. data/lib/appsignal/helpers/instrumentation.rb +236 -20
  21. data/lib/appsignal/helpers/metrics.rb +3 -24
  22. data/lib/appsignal/hooks/action_cable.rb +18 -6
  23. data/lib/appsignal/hooks/active_job.rb +128 -39
  24. data/lib/appsignal/hooks/at_exit.rb +4 -1
  25. data/lib/appsignal/hooks/excon.rb +20 -0
  26. data/lib/appsignal/hooks/faraday.rb +16 -0
  27. data/lib/appsignal/hooks/http.rb +5 -0
  28. data/lib/appsignal/hooks/resque.rb +1 -1
  29. data/lib/appsignal/hooks/sequel.rb +32 -2
  30. data/lib/appsignal/hooks/shoryuken.rb +3 -3
  31. data/lib/appsignal/hooks/sidekiq.rb +1 -1
  32. data/lib/appsignal/integrations/action_cable.rb +5 -2
  33. data/lib/appsignal/integrations/active_support_notifications.rb +59 -14
  34. data/lib/appsignal/integrations/data_mapper.rb +14 -2
  35. data/lib/appsignal/integrations/delayed_job_plugin.rb +45 -5
  36. data/lib/appsignal/integrations/dry_monitor.rb +39 -15
  37. data/lib/appsignal/integrations/excon/appsignal_middleware.rb +21 -0
  38. data/lib/appsignal/integrations/excon.rb +52 -15
  39. data/lib/appsignal/integrations/faraday.rb +47 -12
  40. data/lib/appsignal/integrations/http.rb +43 -1
  41. data/lib/appsignal/integrations/mongo_ruby_driver.rb +73 -4
  42. data/lib/appsignal/integrations/net_http.rb +31 -2
  43. data/lib/appsignal/integrations/puma.rb +4 -1
  44. data/lib/appsignal/integrations/que.rb +224 -37
  45. data/lib/appsignal/integrations/railtie.rb +4 -1
  46. data/lib/appsignal/integrations/rake.rb +9 -3
  47. data/lib/appsignal/integrations/redis.rb +22 -1
  48. data/lib/appsignal/integrations/redis_client.rb +22 -1
  49. data/lib/appsignal/integrations/resque.rb +57 -10
  50. data/lib/appsignal/integrations/shoryuken.rb +140 -12
  51. data/lib/appsignal/integrations/sidekiq.rb +73 -16
  52. data/lib/appsignal/integrations/webmachine.rb +47 -4
  53. data/lib/appsignal/loaders/padrino.rb +2 -1
  54. data/lib/appsignal/logger/extension_backend.rb +24 -0
  55. data/lib/appsignal/logger/opentelemetry_backend.rb +66 -0
  56. data/lib/appsignal/logger.rb +13 -9
  57. data/lib/appsignal/metrics/extension_backend.rb +47 -0
  58. data/lib/appsignal/metrics/opentelemetry_backend.rb +89 -0
  59. data/lib/appsignal/opentelemetry/attributes.rb +31 -0
  60. data/lib/appsignal/opentelemetry/dependencies.rb +35 -0
  61. data/lib/appsignal/opentelemetry/error_type.rb +37 -0
  62. data/lib/appsignal/opentelemetry/http_client_request.rb +83 -0
  63. data/lib/appsignal/opentelemetry/http_method.rb +59 -0
  64. data/lib/appsignal/opentelemetry/http_response.rb +30 -0
  65. data/lib/appsignal/opentelemetry/http_server_request.rb +45 -0
  66. data/lib/appsignal/opentelemetry/messaging.rb +82 -0
  67. data/lib/appsignal/opentelemetry/rendering.rb +29 -0
  68. data/lib/appsignal/opentelemetry/sql_db_system.rb +89 -0
  69. data/lib/appsignal/opentelemetry.rb +337 -0
  70. data/lib/appsignal/rack/abstract_middleware.rb +63 -4
  71. data/lib/appsignal/rack/body_wrapper.rb +18 -5
  72. data/lib/appsignal/rack/event_handler.rb +36 -3
  73. data/lib/appsignal/rack/grape_middleware.rb +1 -0
  74. data/lib/appsignal/rack/hanami_middleware.rb +2 -1
  75. data/lib/appsignal/rack/instrumentation_middleware.rb +1 -0
  76. data/lib/appsignal/rack/rails_instrumentation.rb +1 -0
  77. data/lib/appsignal/rack/sinatra_instrumentation.rb +1 -0
  78. data/lib/appsignal/rack.rb +39 -11
  79. data/lib/appsignal/sample_data.rb +4 -0
  80. data/lib/appsignal/transaction/base_backend.rb +107 -0
  81. data/lib/appsignal/transaction/extension_backend.rb +203 -0
  82. data/lib/appsignal/transaction/opentelemetry_backend.rb +796 -0
  83. data/lib/appsignal/transaction.rb +537 -144
  84. data/lib/appsignal/utils/stdout_and_logger_message.rb +9 -0
  85. data/lib/appsignal/version.rb +1 -1
  86. data/lib/appsignal.rb +9 -0
  87. data/sig/appsignal.rbi +426 -37
  88. data/sig/appsignal.rbs +386 -27
  89. metadata +23 -1
@@ -16,14 +16,7 @@ module Appsignal
16
16
  # @see https://docs.appsignal.com/metrics/custom.html
17
17
  # Metrics documentation
18
18
  def set_gauge(name, value, tags = {})
19
- Appsignal::Extension.set_gauge(
20
- name.to_s,
21
- value.to_f,
22
- Appsignal::Utils::Data.generate(tags)
23
- )
24
- rescue RangeError
25
- Appsignal.internal_logger
26
- .warn("The gauge value '#{value}' for metric '#{name}' is too big")
19
+ Appsignal::Backends.metrics.set_gauge(name, value, tags)
27
20
  end
28
21
 
29
22
  # Report a counter metric.
@@ -39,14 +32,7 @@ module Appsignal
39
32
  # @see https://docs.appsignal.com/metrics/custom.html
40
33
  # Metrics documentation
41
34
  def increment_counter(name, value = 1.0, tags = {})
42
- Appsignal::Extension.increment_counter(
43
- name.to_s,
44
- value.to_f,
45
- Appsignal::Utils::Data.generate(tags)
46
- )
47
- rescue RangeError
48
- Appsignal.internal_logger
49
- .warn("The counter value '#{value}' for metric '#{name}' is too big")
35
+ Appsignal::Backends.metrics.increment_counter(name, value, tags)
50
36
  end
51
37
 
52
38
  # Report a distribution metric.
@@ -62,14 +48,7 @@ module Appsignal
62
48
  # @see https://docs.appsignal.com/metrics/custom.html
63
49
  # Metrics documentation
64
50
  def add_distribution_value(name, value, tags = {})
65
- Appsignal::Extension.add_distribution_value(
66
- name.to_s,
67
- value.to_f,
68
- Appsignal::Utils::Data.generate(tags)
69
- )
70
- rescue RangeError
71
- Appsignal.internal_logger
72
- .warn("The distribution value '#{value}' for metric '#{name}' is too big")
51
+ Appsignal::Backends.metrics.add_distribution_value(name, value, tags)
73
52
  end
74
53
  end
75
54
  end
@@ -37,10 +37,16 @@ module Appsignal
37
37
  env[Appsignal::Hooks::ActionCableHook::REQUEST_ID] ||= request_id
38
38
 
39
39
  transaction =
40
- Appsignal::Transaction.create(Appsignal::Transaction::ACTION_CABLE)
40
+ Appsignal::Transaction.create(
41
+ Appsignal::Transaction::ACTION_CABLE,
42
+ :opentelemetry_scope => ["appsignal-ruby/action_cable", Appsignal::VERSION]
43
+ )
41
44
 
42
45
  begin
43
- Appsignal.instrument "subscribed.action_cable" do
46
+ Appsignal.instrument(
47
+ "subscribed.action_cable",
48
+ :opentelemetry_scope => ["appsignal-ruby/action_cable", Appsignal::VERSION]
49
+ ) do
44
50
  inner.call
45
51
  end
46
52
  rescue Exception => exception
@@ -50,7 +56,7 @@ module Appsignal
50
56
  transaction.set_action_if_nil("#{channel.class}#subscribed")
51
57
  transaction.set_metadata("path", request.path)
52
58
  transaction.set_metadata("method", "websocket")
53
- transaction.add_params_if_nil { request.params }
59
+ transaction.add_request_payload_if_nil { request.params }
54
60
  transaction.add_headers_if_nil { request.env }
55
61
  transaction.add_session_data { request.session.to_h if request.respond_to? :session }
56
62
  transaction.add_tags(:request_id => request_id) if request_id
@@ -73,10 +79,16 @@ module Appsignal
73
79
  env[Appsignal::Hooks::ActionCableHook::REQUEST_ID] ||= request_id
74
80
 
75
81
  transaction =
76
- Appsignal::Transaction.create(Appsignal::Transaction::ACTION_CABLE)
82
+ Appsignal::Transaction.create(
83
+ Appsignal::Transaction::ACTION_CABLE,
84
+ :opentelemetry_scope => ["appsignal-ruby/action_cable", Appsignal::VERSION]
85
+ )
77
86
 
78
87
  begin
79
- Appsignal.instrument "unsubscribed.action_cable" do
88
+ Appsignal.instrument(
89
+ "unsubscribed.action_cable",
90
+ :opentelemetry_scope => ["appsignal-ruby/action_cable", Appsignal::VERSION]
91
+ ) do
80
92
  inner.call
81
93
  end
82
94
  rescue Exception => exception
@@ -86,7 +98,7 @@ module Appsignal
86
98
  transaction.set_action_if_nil("#{channel.class}#unsubscribed")
87
99
  transaction.set_metadata("path", request.path)
88
100
  transaction.set_metadata("method", "websocket")
89
- transaction.add_params_if_nil { request.params }
101
+ transaction.add_request_payload_if_nil { request.params }
90
102
  transaction.add_headers_if_nil { request.env }
91
103
  transaction.add_session_data { request.session.to_h if request.respond_to? :session }
92
104
  transaction.add_tags(:request_id => request_id) if request_id
@@ -6,6 +6,22 @@ module Appsignal
6
6
  class ActiveJobHook < Appsignal::Hooks::Hook
7
7
  register :active_job
8
8
 
9
+ # This integration records the enqueue itself, as a producer event that
10
+ # also injects trace context, and Active Job's own `enqueue.active_job`
11
+ # notification fires nested inside it. Claim the event so that the
12
+ # generic notification paths leave it alone.
13
+ #
14
+ # Claimed here, when this file is required, rather than in `install`.
15
+ # `install` only runs when Active Job instrumentation is turned on, but
16
+ # a customer who turns it off does not want to see the native
17
+ # notification reported instead. This call does not touch any
18
+ # `ActiveJob` constant, so it is safe to run even when the library is
19
+ # not present.
20
+ Appsignal::EventFormatter.register(
21
+ "enqueue.active_job",
22
+ Appsignal::EventFormatter::RecordedElsewhere
23
+ )
24
+
9
25
  def self.version_7_1_or_higher?
10
26
  @version_7_1_or_higher ||=
11
27
  if dependencies_present?
@@ -30,8 +46,11 @@ module Appsignal
30
46
  ActiveSupport.on_load(:active_job) do
31
47
  ::ActiveJob::Base
32
48
  .extend ::Appsignal::Hooks::ActiveJobHook::ActiveJobClassInstrumentation
49
+ # Carry W3C trace context across the enqueue/perform boundary in
50
+ # collector mode (no-ops otherwise). The patches are cheap and
51
+ # mode-gated inside their method bodies, so install them unconditionally.
33
52
  ::ActiveJob::Base
34
- .prepend ::Appsignal::Hooks::ActiveJobHook::ActiveJobEnqueueInstrumentation
53
+ .prepend ::Appsignal::Hooks::ActiveJobHook::ActiveJobTraceContext
35
54
 
36
55
  next unless Appsignal::Hooks::ActiveJobHook.version_7_1_or_higher?
37
56
 
@@ -44,42 +63,6 @@ module Appsignal
44
63
  end
45
64
  end
46
65
 
47
- # Records an `enqueue.active_job` event when a job is enqueued, so the
48
- # enqueue shows up on the active transaction's timeline (e.g. when
49
- # enqueuing from within a web request or another job).
50
- #
51
- # Wrapping `enqueue` ourselves -- rather than relying on Rails' native
52
- # `enqueue.active_job` notification, which the AppSignal notifications
53
- # path now suppresses -- gives us a single event we own. Like all
54
- # AppSignal events, this only records when there's an active transaction;
55
- # an enqueue with no transaction is a transparent pass-through.
56
- #
57
- # @!visibility private
58
- module ActiveJobEnqueueInstrumentation
59
- def enqueue(*, **)
60
- # Skip recording the event when enqueue events are suppressed. That is
61
- # the case when enqueue instrumentation is disabled, and it keeps this
62
- # integration consistent with the standalone adapters (Sidekiq, ...),
63
- # which already gate their own enqueue event on this check.
64
- if Appsignal::Transaction.current? &&
65
- Appsignal::Transaction.current.job_enqueue_events_suppressed?
66
- return super
67
- end
68
-
69
- Appsignal.instrument("enqueue.active_job", "enqueue #{self.class.name} job") do
70
- # Active Job enqueues through an adapter (Sidekiq, Resque, ...) that
71
- # has its own enqueue instrumentation. Suppress it so the enqueue is
72
- # recorded once, as this event, rather than as nested Active Job +
73
- # adapter events.
74
- if Appsignal::Transaction.current?
75
- Appsignal::Transaction.current.suppress_job_enqueue_events { super }
76
- else
77
- super
78
- end
79
- end
80
- end
81
- end
82
-
83
66
  module ActiveJobClassInstrumentation
84
67
  def execute(job)
85
68
  enqueued_at = job["enqueued_at"]
@@ -101,12 +84,39 @@ module Appsignal
101
84
  # We don't have a separate integration for this QueueAdapter like
102
85
  # we do for Sidekiq.
103
86
  #
87
+ # Read the trace context off the job so the transaction links back
88
+ # to the enqueuer (no-op outside collector mode). Only here, in the
89
+ # standalone branch: when a wrapper integration (e.g. Sidekiq)
90
+ # created the transaction, it already extracted, so we must not
91
+ # extract a second time.
92
+ #
104
93
  # Prefer job_id from provider, instead of ActiveJob's internal ID.
105
- Appsignal::Transaction.create(Appsignal::Transaction::BACKGROUND_JOB)
94
+ Appsignal::Transaction.create(
95
+ Appsignal::Transaction::BACKGROUND_JOB,
96
+ :opentelemetry_context => Appsignal::OpenTelemetry.extract_job_context(job),
97
+ :opentelemetry_scope => ["appsignal-ruby/active_job", Appsignal::VERSION],
98
+ :opentelemetry_kind => :consumer,
99
+ :opentelemetry_relationship => :both
100
+ )
106
101
  end
107
102
 
103
+ unless has_wrapper_transaction
104
+ # Describes this span as a job being performed. The messaging
105
+ # system is what the trace timeline reads to recognize background
106
+ # job work, and `active_job` is the value OpenTelemetry's own Active
107
+ # Job instrumentation uses.
108
+ #
109
+ # Only set when this hook created the transaction. When an adapter
110
+ # integration created it, that adapter already named itself, and its
111
+ # answer is the more specific one.
112
+ transaction.add_opentelemetry_attributes(
113
+ Appsignal::OpenTelemetry::Messaging
114
+ .perform_attributes("active_job", :destination => job["queue_name"])
115
+ )
116
+ end
117
+
108
118
  begin
109
- transaction.add_params_if_nil(job["arguments"])
119
+ transaction.add_function_parameters_if_nil(job["arguments"])
110
120
 
111
121
  transaction_tags = ActiveJobHelpers.transaction_tags_for(job)
112
122
  transaction.add_tags(transaction_tags)
@@ -162,6 +172,85 @@ module Appsignal
162
172
  end
163
173
  end
164
174
 
175
+ # Reads and writes W3C trace context on the ActiveJob enqueue/perform
176
+ # boundary, wire-compatible with OpenTelemetry's ActiveJob instrumentation.
177
+ # All of this no-ops outside collector mode.
178
+ #
179
+ # Context rides on the job under `__otel_headers`, the same carrier OTel
180
+ # uses. Stock `serialize`/`deserialize` only carry a fixed key set, so --
181
+ # like OTel -- we patch both plus an accessor to round-trip it. The on-wire
182
+ # value is run through ActiveJob's argument serializer (an array of
183
+ # `[key, value]` pairs), matching OTel byte-for-byte so an AppSignal- and an
184
+ # OTel-instrumented service read each other's jobs.
185
+ module ActiveJobTraceContext
186
+ # Inject on enqueue from inside a producer event, so the job carries this
187
+ # transaction's context and the perform later links back. Mirrors the
188
+ # Sidekiq client middleware: an AppSignal event (a producer span in
189
+ # collector mode), not a direct SDK span. `Appsignal.instrument` is a
190
+ # transparent pass-through when there's no active transaction, and
191
+ # `inject_context` no-ops outside collector mode.
192
+ def enqueue(*, **)
193
+ # When enqueue instrumentation is disabled, drop the trace context
194
+ # along with the event. Without an enqueue event there is no producer
195
+ # span, so the context we would write is that of whatever span is
196
+ # current, such as the surrounding web request. The job that performs
197
+ # later would then link back to a span that is not a producer.
198
+ return super if Appsignal.config && !Appsignal.config[:enable_job_enqueue_instrumentation]
199
+
200
+ # Another enqueue integration is already recording this enqueue, so
201
+ # don't record it a second time.
202
+ if Appsignal::Transaction.current? &&
203
+ Appsignal::Transaction.current.job_enqueue_events_suppressed?
204
+ return super
205
+ end
206
+
207
+ Appsignal.instrument(
208
+ "enqueue.active_job",
209
+ "enqueue #{self.class.name} job",
210
+ :opentelemetry_kind => :producer,
211
+ :opentelemetry_scope => ["appsignal-ruby/active_job", Appsignal::VERSION]
212
+ ) do
213
+ Appsignal::Transaction.current.add_opentelemetry_attributes(
214
+ Appsignal::OpenTelemetry::Messaging
215
+ .enqueue_attributes("active_job", :destination => queue_name)
216
+ )
217
+ Appsignal::OpenTelemetry.inject_context(__otel_headers)
218
+ # Active Job enqueues through an adapter (Sidekiq, Resque, ...) that
219
+ # has its own enqueue instrumentation. Suppress it so the enqueue is
220
+ # recorded once, as this event, rather than as nested Active Job +
221
+ # adapter events.
222
+ if Appsignal::Transaction.current?
223
+ Appsignal::Transaction.current.suppress_job_enqueue_events { super }
224
+ else
225
+ super
226
+ end
227
+ end
228
+ end
229
+
230
+ def serialize
231
+ super.tap do |data|
232
+ Appsignal::OpenTelemetry.if_started do
233
+ next if __otel_headers.empty?
234
+
235
+ data["__otel_headers"] = ::ActiveJob::Arguments.serialize(__otel_headers)
236
+ end
237
+ end
238
+ end
239
+
240
+ def deserialize(job_data)
241
+ super
242
+ serialized = job_data["__otel_headers"]
243
+ @__otel_headers =
244
+ serialized ? ::ActiveJob::Arguments.deserialize(serialized).to_h : {}
245
+ end
246
+
247
+ def __otel_headers
248
+ @__otel_headers ||= {}
249
+ end
250
+
251
+ attr_writer :__otel_headers
252
+ end
253
+
165
254
  module ActiveJobHelpers
166
255
  ACTION_MAILER_CLASSES = [
167
256
  "ActionMailer::DeliveryJob",
@@ -38,7 +38,10 @@ module Appsignal
38
38
 
39
39
  report_error = true
40
40
 
41
- Appsignal.report_error(error) do |transaction|
41
+ Appsignal.report_error(
42
+ error,
43
+ :opentelemetry_scope => ["appsignal-ruby/at_exit", Appsignal::VERSION]
44
+ ) do |transaction|
42
45
  transaction.set_namespace("unhandled")
43
46
  end
44
47
  ensure
@@ -12,15 +12,35 @@ module Appsignal
12
12
 
13
13
  def install
14
14
  require "appsignal/integrations/excon"
15
+ require "appsignal/integrations/excon/appsignal_middleware"
15
16
  # Instrument the request at the connection, rather than by registering
16
17
  # AppSignal as Excon's instrumentor. An instrumentor is told about a
17
18
  # request in pieces, none of which covers the wait for the response, and
18
19
  # there is only room for one of them, so registering ours would replace
19
20
  # any the application set up itself.
20
21
  ::Excon::Connection.prepend Appsignal::Integrations::ExconIntegration
22
+ install_middleware
21
23
 
22
24
  Appsignal::Environment.report_enabled("excon")
23
25
  end
26
+
27
+ private
28
+
29
+ # Trace context is written onto the outgoing request by a middleware,
30
+ # because that is where Excon exposes the request's headers.
31
+ #
32
+ # Insert it just before the Mock middleware, the innermost one, where the
33
+ # response is produced. That way it runs before the request is sent.
34
+ # Appending to the end would place it after Mock, which short-circuits the
35
+ # chain before reaching it.
36
+ def install_middleware
37
+ middlewares = ::Excon.defaults[:middlewares].dup
38
+ return if middlewares.include?(Appsignal::Integrations::ExconMiddleware)
39
+
40
+ index = middlewares.index(::Excon::Middleware::Mock) || middlewares.length
41
+ middlewares.insert(index, Appsignal::Integrations::ExconMiddleware)
42
+ ::Excon.defaults[:middlewares] = middlewares
43
+ end
24
44
  end
25
45
  end
26
46
  end
@@ -6,6 +6,22 @@ module Appsignal
6
6
  class FaradayHook < Appsignal::Hooks::Hook
7
7
  register :faraday
8
8
 
9
+ # This integration records the request itself, so Faraday's own
10
+ # instrumentation middleware would report the same work again as a
11
+ # `request.faraday` notification. Claim it so the generic notification
12
+ # paths leave it alone.
13
+ #
14
+ # Claimed here, when this file is required, rather than in `install`.
15
+ # `install` only runs when Faraday instrumentation is turned on, but a
16
+ # customer who turns it off does not want to see the native
17
+ # notification reported instead. This call does not touch any
18
+ # `Faraday` constant, so it is safe to run even when the library is not
19
+ # present.
20
+ Appsignal::EventFormatter.register(
21
+ "request.faraday",
22
+ Appsignal::EventFormatter::RecordedElsewhere
23
+ )
24
+
9
25
  def dependencies_present?
10
26
  defined?(::Faraday) && Appsignal.config && Appsignal.config[:instrument_faraday]
11
27
  end
@@ -32,6 +32,11 @@ module Appsignal
32
32
  if defined?(HTTP::Session)
33
33
  HTTP::Session.prepend Appsignal::Integrations::HttpIntegration::KeywordOptions
34
34
  end
35
+ # Propagate trace context onto every outgoing hop (redirects included) at
36
+ # `Client#perform`, where the live request headers are reachable. Kept
37
+ # separate from the request-boundary event above: it only injects context
38
+ # and no-ops outside collector mode.
39
+ HTTP::Client.prepend Appsignal::Integrations::HttpIntegration::ContextInjection
35
40
 
36
41
  Appsignal::Environment.report_enabled("http_rb")
37
42
  end
@@ -15,7 +15,7 @@ module Appsignal
15
15
  Resque::Job.prepend Appsignal::Integrations::ResqueIntegration
16
16
 
17
17
  # Resque enqueues through the `Resque.push` singleton method, so prepend
18
- # onto its singleton class to record the enqueue event.
18
+ # onto its singleton class to write the trace context onto outgoing jobs.
19
19
  Resque.singleton_class.prepend Appsignal::Integrations::ResquePushIntegration
20
20
  end
21
21
  end
@@ -10,8 +10,13 @@ module Appsignal
10
10
  "sql.sequel",
11
11
  nil,
12
12
  sql,
13
- Appsignal::EventFormatter::SQL_BODY_FORMAT
13
+ Appsignal::EventFormatter::SQL_BODY_FORMAT,
14
+ :opentelemetry_kind => :client,
15
+ :opentelemetry_scope => ["appsignal-ruby/sequel", Appsignal::VERSION]
14
16
  ) do
17
+ Appsignal::Transaction.current.add_opentelemetry_attributes(
18
+ Appsignal::Hooks::SequelHook.sequel_db_attributes(self)
19
+ )
15
20
  super
16
21
  end
17
22
  end
@@ -25,8 +30,13 @@ module Appsignal
25
30
  "sql.sequel",
26
31
  nil,
27
32
  sql,
28
- Appsignal::EventFormatter::SQL_BODY_FORMAT
33
+ Appsignal::EventFormatter::SQL_BODY_FORMAT,
34
+ :opentelemetry_kind => :client,
35
+ :opentelemetry_scope => ["appsignal-ruby/sequel", Appsignal::VERSION]
29
36
  ) do
37
+ Appsignal::Transaction.current.add_opentelemetry_attributes(
38
+ Appsignal::Hooks::SequelHook.sequel_db_attributes(self)
39
+ )
30
40
  super
31
41
  end
32
42
  end
@@ -35,6 +45,26 @@ module Appsignal
35
45
  class SequelHook < Appsignal::Hooks::Hook
36
46
  register :sequel
37
47
 
48
+ # The query's `Sequel::Database` names both the engine it talks to and
49
+ # the database it is connected to, neither of which the sql.sequel
50
+ # formatter can see -- it only gets the query text. Shared by both
51
+ # extensions above, whichever one a given Sequel version registers.
52
+ #
53
+ # @!visibility private
54
+ def self.sequel_db_attributes(database)
55
+ attributes = {}
56
+
57
+ name = Appsignal::OpenTelemetry::SqlDbSystem.name_for_sequel(database.database_type)
58
+ attributes["db.system.name"] = name if name
59
+
60
+ # `opts[:database]` is Sequel's own option key for the database to
61
+ # connect to, so it doubles as the database's name.
62
+ namespace = database.opts[:database].to_s
63
+ attributes["db.namespace"] = namespace unless namespace.empty?
64
+
65
+ attributes
66
+ end
67
+
38
68
  def dependencies_present?
39
69
  defined?(::Sequel::Database) &&
40
70
  Appsignal.config &&
@@ -19,9 +19,9 @@ module Appsignal
19
19
  end
20
20
 
21
21
  # Servers enqueue jobs too, so they need the client middleware that
22
- # records the enqueue event. Shoryuken only yields `configure_client`
23
- # outside the server, so register it here as well for enqueues from
24
- # within a worker.
22
+ # writes the trace context onto outgoing messages. Shoryuken only
23
+ # yields `configure_client` outside the server, so register it here as
24
+ # well for enqueues from within a worker.
25
25
  config.client_middleware do |chain|
26
26
  chain.add Appsignal::Integrations::ShoryukenClientMiddleware
27
27
  end
@@ -45,7 +45,7 @@ module Appsignal
45
45
  end
46
46
 
47
47
  # Servers enqueue jobs too, so they need the client middleware that
48
- # records the enqueue event.
48
+ # writes the trace context onto outgoing jobs.
49
49
  config.client_middleware do |chain|
50
50
  chain.add Appsignal::Integrations::SidekiqClientMiddleware
51
51
  end
@@ -11,7 +11,10 @@ module Appsignal
11
11
  request_id = request.request_id || SecureRandom.uuid
12
12
  env[Appsignal::Hooks::ActionCableHook::REQUEST_ID] ||= request_id
13
13
 
14
- transaction = Appsignal::Transaction.create(Appsignal::Transaction::ACTION_CABLE)
14
+ transaction = Appsignal::Transaction.create(
15
+ Appsignal::Transaction::ACTION_CABLE,
16
+ :opentelemetry_scope => ["appsignal-ruby/action_cable", Appsignal::VERSION]
17
+ )
15
18
 
16
19
  begin
17
20
  super
@@ -20,7 +23,7 @@ module Appsignal
20
23
  raise exception
21
24
  ensure
22
25
  transaction.set_action_if_nil("#{self.class}##{args.first["action"]}")
23
- transaction.add_params_if_nil(args.first)
26
+ transaction.add_request_payload_if_nil(args.first)
24
27
  transaction.add_session_data { request.session.to_h if request.respond_to? :session }
25
28
  transaction.set_metadata("path", request.path)
26
29
  transaction.set_metadata("method", "websocket")
@@ -7,24 +7,52 @@ module Appsignal
7
7
  class << self
8
8
  BANG = "!"
9
9
 
10
- # Events a dedicated AppSignal integration already records, so the
11
- # generic notifications path must not record them a second time. The
12
- # ActiveJob hook owns `enqueue.active_job` (it wraps the enqueue in its
13
- # own event, with Rails' native notification nested inside), and the
14
- # Faraday integration owns `request.faraday`.
15
- SUPPRESSED_EVENT_NAMES = ["enqueue.active_job", "request.faraday"].freeze
16
-
17
10
  def start_event(name)
18
11
  return unless record_event?(name)
19
12
 
20
- Appsignal::Transaction.current.start_event
13
+ # The event's formatter says what kind of work the event is, such as
14
+ # a SQL query being an outgoing call to a database, and can name the
15
+ # library the instrumentation is for. Both are immutable once the
16
+ # span exists, so they have to be set here at event start.
17
+ #
18
+ # A formatter that names no library leaves the scope to be derived
19
+ # from the event name, which is right for everything Rails reports.
20
+ Appsignal::Transaction.current.start_event(
21
+ :opentelemetry_kind => Appsignal::EventFormatter.opentelemetry_kind(name),
22
+ :opentelemetry_scope =>
23
+ Appsignal::EventFormatter.opentelemetry_scope(name) || scope_for(name)
24
+ )
25
+ end
26
+
27
+ # ActiveSupport::Notifications bridges many Rails components through this
28
+ # one path (`sql.active_record`, `render_template.action_view`, ...), so
29
+ # derive the instrumentation scope from the event name's group: the part
30
+ # after the last dot. That gives each Rails component its own scope
31
+ # (`appsignal-ruby/active_record`, `appsignal-ruby/action_view`, ...)
32
+ # rather than lumping them under one. A name without a group falls back
33
+ # to the default scope in the backend.
34
+ def scope_for(name)
35
+ # Only names with a group (a dot) map to a component scope. A name
36
+ # without one has no component to attribute it to, so it falls back to
37
+ # the default scope in the backend (returning nil here).
38
+ parts = name.to_s.split(".")
39
+ return if parts.length < 2 || parts.last.empty?
40
+
41
+ ["appsignal-ruby/#{parts.last}", Appsignal::VERSION]
21
42
  end
22
43
 
23
44
  def finish_event(name, payload = {})
24
45
  return unless record_event?(name)
25
46
 
26
47
  title, body, body_format = Appsignal::EventFormatter.format(name, payload)
27
- Appsignal::Transaction.current.finish_event(
48
+ transaction = Appsignal::Transaction.current
49
+ # Set while the event's span is still open, so the attributes land on
50
+ # the event rather than on the transaction.
51
+ transaction.add_opentelemetry_attributes(
52
+ Appsignal::EventFormatter.opentelemetry_attributes(name, payload)
53
+ )
54
+ record_error_type(transaction, payload)
55
+ transaction.finish_event(
28
56
  name.to_s,
29
57
  title,
30
58
  body,
@@ -32,12 +60,29 @@ module Appsignal
32
60
  )
33
61
  end
34
62
 
35
- # Events starting with a bang are internal to Rails; suppressed events
36
- # are recorded by a dedicated integration instead. Both `start_event`
37
- # and `finish_event` gate on this so the event stack stays balanced.
63
+ # Says what kind of failure ended the event, which the OpenTelemetry
64
+ # semantic conventions ask for on a span whose operation failed.
65
+ #
66
+ # ActiveSupport puts the exception in the payload when the instrumented
67
+ # block raised, and it does so before it hands control to any of the
68
+ # paths this integration hooks into. So the failure is readable here and
69
+ # there is nothing to rescue, whether the event was reported through a
70
+ # block or through a `start` and `finish` pair.
71
+ def record_error_type(transaction, payload)
72
+ error = payload[:exception_object]
73
+ return unless error
74
+
75
+ transaction.add_opentelemetry_attributes(
76
+ Appsignal::OpenTelemetry::ErrorType.attributes_for(error.class.name)
77
+ )
78
+ end
79
+
80
+ # Events starting with a bang are internal to Rails. An event that the
81
+ # registry says a dedicated integration records is not recorded again
82
+ # here. Both `start_event` and `finish_event` gate on this so the event
83
+ # stack stays balanced.
38
84
  def record_event?(name)
39
- name = name.to_s
40
- name[0] != BANG && !SUPPRESSED_EVENT_NAMES.include?(name)
85
+ name.to_s[0] != BANG && Appsignal::EventFormatter.record?(name)
41
86
  end
42
87
  end
43
88
 
@@ -12,22 +12,34 @@ module Appsignal
12
12
  ].freeze
13
13
 
14
14
  def log(message)
15
+ attributes = {}
16
+
15
17
  # If scheme is SQL-like, try to sanitize it, otherwise clear the body
16
18
  if SQL_CLASSES.include?(self.class.to_s)
17
19
  body_content = message.query
18
20
  body_format = Appsignal::EventFormatter::SQL_BODY_FORMAT
21
+ # The connection class names the engine it talks to, one of the four
22
+ # SQL_CLASSES above; a class this map does not recognise leaves the
23
+ # SQL sentinel to apply, same as it would for a fifth SQL_CLASSES
24
+ # entry this map has not been taught about.
25
+ db_system = Appsignal::OpenTelemetry::SqlDbSystem.name_for_data_mapper(self.class.to_s)
26
+ attributes["db.system.name"] = db_system if db_system
19
27
  else
20
28
  body_content = ""
21
29
  body_format = Appsignal::EventFormatter::DEFAULT
22
30
  end
23
31
 
24
- # Record event
32
+ # Record event. The query is an outgoing call to the database, so tag it
33
+ # as a client span (collector mode); no-op in agent mode.
25
34
  Appsignal::Transaction.current.record_event(
26
35
  "query.data_mapper",
27
36
  "DataMapper Query",
28
37
  body_content,
29
38
  message.duration,
30
- body_format
39
+ body_format,
40
+ :opentelemetry_kind => :client,
41
+ :opentelemetry_scope => ["appsignal-ruby/data_mapper", Appsignal::VERSION],
42
+ :opentelemetry_attributes => attributes
31
43
  )
32
44
  super
33
45
  end