appsignal 4.10.4-java → 5.0.0-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 (93) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +108 -0
  3. data/Rakefile +195 -4
  4. data/appsignal.gemspec +8 -0
  5. data/build_matrix.yml +12 -0
  6. data/ext/appsignal_extension.c +14 -0
  7. data/lib/appsignal/backends.rb +55 -0
  8. data/lib/appsignal/cli/diagnose.rb +2 -10
  9. data/lib/appsignal/config.rb +377 -13
  10. data/lib/appsignal/demo.rb +12 -10
  11. data/lib/appsignal/event_formatter/action_view/render_formatter.rb +34 -22
  12. data/lib/appsignal/event_formatter/active_job/perform_formatter.rb +35 -0
  13. data/lib/appsignal/event_formatter/active_record/sql_formatter.rb +19 -0
  14. data/lib/appsignal/event_formatter/elastic_search/search_formatter.rb +27 -0
  15. data/lib/appsignal/event_formatter/recorded_elsewhere.rb +17 -0
  16. data/lib/appsignal/event_formatter/rom/sql_formatter.rb +24 -0
  17. data/lib/appsignal/event_formatter/sequel/sql_formatter.rb +5 -0
  18. data/lib/appsignal/event_formatter/view_component/render_formatter.rb +21 -10
  19. data/lib/appsignal/event_formatter.rb +78 -0
  20. data/lib/appsignal/extension.rb +4 -0
  21. data/lib/appsignal/helpers/instrumentation.rb +324 -20
  22. data/lib/appsignal/helpers/metrics.rb +3 -24
  23. data/lib/appsignal/hooks/action_cable.rb +26 -8
  24. data/lib/appsignal/hooks/active_job.rb +184 -47
  25. data/lib/appsignal/hooks/at_exit.rb +4 -1
  26. data/lib/appsignal/hooks/excon.rb +20 -0
  27. data/lib/appsignal/hooks/faraday.rb +16 -0
  28. data/lib/appsignal/hooks/http.rb +5 -0
  29. data/lib/appsignal/hooks/resque.rb +1 -1
  30. data/lib/appsignal/hooks/sequel.rb +32 -2
  31. data/lib/appsignal/hooks/shoryuken.rb +3 -3
  32. data/lib/appsignal/hooks/sidekiq.rb +1 -1
  33. data/lib/appsignal/integrations/action_cable.rb +5 -2
  34. data/lib/appsignal/integrations/active_support_notifications.rb +59 -19
  35. data/lib/appsignal/integrations/data_mapper.rb +14 -2
  36. data/lib/appsignal/integrations/delayed_job_plugin.rb +81 -8
  37. data/lib/appsignal/integrations/dry_monitor.rb +39 -15
  38. data/lib/appsignal/integrations/excon/appsignal_middleware.rb +21 -0
  39. data/lib/appsignal/integrations/excon.rb +52 -15
  40. data/lib/appsignal/integrations/faraday.rb +47 -12
  41. data/lib/appsignal/integrations/http.rb +43 -1
  42. data/lib/appsignal/integrations/mongo_ruby_driver.rb +73 -4
  43. data/lib/appsignal/integrations/net_http.rb +31 -2
  44. data/lib/appsignal/integrations/puma.rb +4 -1
  45. data/lib/appsignal/integrations/que.rb +256 -37
  46. data/lib/appsignal/integrations/railtie.rb +4 -1
  47. data/lib/appsignal/integrations/rake.rb +9 -3
  48. data/lib/appsignal/integrations/redis.rb +22 -1
  49. data/lib/appsignal/integrations/redis_client.rb +22 -1
  50. data/lib/appsignal/integrations/resque.rb +81 -11
  51. data/lib/appsignal/integrations/shoryuken.rb +159 -12
  52. data/lib/appsignal/integrations/sidekiq.rb +94 -16
  53. data/lib/appsignal/integrations/webmachine.rb +56 -5
  54. data/lib/appsignal/loaders/padrino.rb +2 -1
  55. data/lib/appsignal/logger/extension_backend.rb +24 -0
  56. data/lib/appsignal/logger/opentelemetry_backend.rb +66 -0
  57. data/lib/appsignal/logger.rb +13 -9
  58. data/lib/appsignal/metrics/extension_backend.rb +47 -0
  59. data/lib/appsignal/metrics/opentelemetry_backend.rb +89 -0
  60. data/lib/appsignal/opentelemetry/attributes.rb +31 -0
  61. data/lib/appsignal/opentelemetry/dependencies.rb +35 -0
  62. data/lib/appsignal/opentelemetry/error_type.rb +37 -0
  63. data/lib/appsignal/opentelemetry/http_client_request.rb +83 -0
  64. data/lib/appsignal/opentelemetry/http_method.rb +59 -0
  65. data/lib/appsignal/opentelemetry/http_response.rb +30 -0
  66. data/lib/appsignal/opentelemetry/http_server_request.rb +79 -0
  67. data/lib/appsignal/opentelemetry/messaging.rb +82 -0
  68. data/lib/appsignal/opentelemetry/proxied_exporter.rb +83 -0
  69. data/lib/appsignal/opentelemetry/rendering.rb +29 -0
  70. data/lib/appsignal/opentelemetry/sql_db_system.rb +89 -0
  71. data/lib/appsignal/opentelemetry.rb +495 -0
  72. data/lib/appsignal/rack/abstract_middleware.rb +66 -4
  73. data/lib/appsignal/rack/body_wrapper.rb +18 -5
  74. data/lib/appsignal/rack/event_handler.rb +44 -4
  75. data/lib/appsignal/rack/grape_middleware.rb +1 -0
  76. data/lib/appsignal/rack/hanami_middleware.rb +2 -1
  77. data/lib/appsignal/rack/instrumentation_middleware.rb +1 -0
  78. data/lib/appsignal/rack/rails_instrumentation.rb +1 -0
  79. data/lib/appsignal/rack/sinatra_instrumentation.rb +1 -0
  80. data/lib/appsignal/rack.rb +68 -12
  81. data/lib/appsignal/sample_data.rb +4 -0
  82. data/lib/appsignal/transaction/base_backend.rb +128 -0
  83. data/lib/appsignal/transaction/extension_backend.rb +229 -0
  84. data/lib/appsignal/transaction/opentelemetry_backend.rb +847 -0
  85. data/lib/appsignal/transaction.rb +714 -164
  86. data/lib/appsignal/utils/request_headers.rb +78 -0
  87. data/lib/appsignal/utils/stdout_and_logger_message.rb +9 -0
  88. data/lib/appsignal/utils.rb +1 -0
  89. data/lib/appsignal/version.rb +1 -1
  90. data/lib/appsignal.rb +10 -0
  91. data/sig/appsignal.rbi +630 -37
  92. data/sig/appsignal.rbs +582 -27
  93. metadata +25 -1
@@ -6,6 +6,30 @@ 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
+
25
+ # Claimed for the same reason as the single enqueue above: this integration
26
+ # records the batch itself, as one producer event, and Active Job's own
27
+ # `enqueue_all.active_job` notification fires nested inside it.
28
+ Appsignal::EventFormatter.register(
29
+ "enqueue_all.active_job",
30
+ Appsignal::EventFormatter::RecordedElsewhere
31
+ )
32
+
9
33
  def self.version_7_1_or_higher?
10
34
  @version_7_1_or_higher ||=
11
35
  if dependencies_present?
@@ -83,14 +107,17 @@ module Appsignal
83
107
  ActiveSupport.on_load(:active_job) do
84
108
  ::ActiveJob::Base
85
109
  .extend ::Appsignal::Hooks::ActiveJobHook::ActiveJobClassInstrumentation
110
+ # Carry W3C trace context across the enqueue/perform boundary in
111
+ # collector mode (no-ops otherwise). The patches are cheap and
112
+ # mode-gated inside their method bodies, so install them unconditionally.
86
113
  ::ActiveJob::Base
87
- .prepend ::Appsignal::Hooks::ActiveJobHook::ActiveJobEnqueueInstrumentation
114
+ .prepend ::Appsignal::Hooks::ActiveJobHook::ActiveJobTraceContext
88
115
 
89
116
  # Wrap the method Active Job records a bulk enqueue through, but only
90
117
  # when it is still the method the wrapper knows how to read. When it
91
- # is not, the batch goes unrecorded: Rails' own notification stays
92
- # suppressed, because a worse event is not worth reporting in place
93
- # of the one we set out to report.
118
+ # is not, the batch goes unrecorded: the claim above stands, because
119
+ # a worse event is not worth reporting in place of the one we set out
120
+ # to report.
94
121
  if !Appsignal::Hooks::ActiveJobHook.instrument_enqueue_all_defined?
95
122
  # Nothing to instrument on a version with no bulk enqueue path, so
96
123
  # there is nothing to report either.
@@ -123,45 +150,10 @@ module Appsignal
123
150
  end
124
151
  end
125
152
 
126
- # Records an `enqueue.active_job` event when a job is enqueued, so the
127
- # enqueue shows up on the active transaction's timeline (e.g. when
128
- # enqueuing from within a web request or another job).
129
- #
130
- # Wrapping `enqueue` ourselves -- rather than relying on Rails' native
131
- # `enqueue.active_job` notification, which the AppSignal notifications
132
- # path now suppresses -- gives us a single event we own. Like all
133
- # AppSignal events, this only records when there's an active transaction;
134
- # an enqueue with no transaction is a transparent pass-through.
135
- #
136
- # @!visibility private
137
- module ActiveJobEnqueueInstrumentation
138
- def enqueue(*, **)
139
- # Skip recording the event when enqueue events are suppressed. That is
140
- # the case when enqueue instrumentation is disabled, and it keeps this
141
- # integration consistent with the standalone adapters (Sidekiq, ...),
142
- # which already gate their own enqueue event on this check.
143
- if Appsignal::Transaction.current? &&
144
- Appsignal::Transaction.current.job_enqueue_events_suppressed?
145
- return super
146
- end
147
-
148
- Appsignal.instrument("enqueue.active_job", "enqueue #{self.class.name} job") do
149
- # Active Job enqueues through an adapter (Sidekiq, Resque, ...) that
150
- # has its own enqueue instrumentation. Suppress it so the enqueue is
151
- # recorded once, as this event, rather than as nested Active Job +
152
- # adapter events.
153
- if Appsignal::Transaction.current?
154
- Appsignal::Transaction.current.suppress_job_enqueue_events { super }
155
- else
156
- super
157
- end
158
- end
159
- end
160
- end
161
-
162
153
  # Records an `enqueue_all.active_job` event when a batch of jobs is
163
154
  # enqueued with `ActiveJob.perform_all_later`, so the batch shows up on the
164
- # active transaction's timeline as one event.
155
+ # active transaction's timeline as one event, and as one producer span in
156
+ # collector mode.
165
157
  #
166
158
  # This wraps `instrument_enqueue_all` rather than `perform_all_later`, for
167
159
  # two reasons. It is the method that records the batch, so it is called
@@ -176,15 +168,31 @@ module Appsignal
176
168
  private
177
169
 
178
170
  def instrument_enqueue_all(_queue_adapter, jobs)
179
- # Skip recording the event when enqueue events are suppressed, which is
180
- # also the case when enqueue instrumentation is disabled. Same check as
181
- # the single-job path above.
171
+ # When enqueue instrumentation is disabled, record nothing, the same as
172
+ # the single-job path.
173
+ return super if Appsignal.config && !Appsignal.config[:enable_job_enqueue_instrumentation]
174
+
175
+ # Another enqueue integration is already recording this enqueue, so
176
+ # don't record it a second time.
182
177
  if Appsignal::Transaction.current? &&
183
178
  Appsignal::Transaction.current.job_enqueue_events_suppressed?
184
179
  return super
185
180
  end
186
181
 
187
- Appsignal.instrument("enqueue_all.active_job", bulk_enqueue_title(jobs)) do
182
+ Appsignal.instrument(
183
+ "enqueue_all.active_job",
184
+ bulk_enqueue_title(jobs),
185
+ :opentelemetry_kind => :producer,
186
+ :opentelemetry_scope => ["appsignal-ruby/active_job", Appsignal::VERSION]
187
+ ) do
188
+ Appsignal::Transaction.current.add_opentelemetry_attributes(
189
+ Appsignal::OpenTelemetry::Messaging.enqueue_attributes(
190
+ "active_job",
191
+ :destination => bulk_enqueue_destination(jobs),
192
+ :batch_size => jobs.size
193
+ )
194
+ )
195
+ inject_context_into(jobs)
188
196
  # A bulk enqueue does not go through `ActiveJob::Base#enqueue`, so
189
197
  # nothing has suppressed the adapter (Sidekiq, Resque, ...) yet, and
190
198
  # its own enqueue instrumentation would record an event for every job
@@ -198,6 +206,21 @@ module Appsignal
198
206
  end
199
207
  end
200
208
 
209
+ # Writes this producer span's context onto every job in the batch, and
210
+ # marks each one as part of a batch, so the jobs that perform later link
211
+ # back to this span. A bulk enqueue never goes through
212
+ # `ActiveJob::Base#enqueue`, so the injection the single-job path does
213
+ # never runs for these jobs. The adapter serializes each job after this,
214
+ # and the `serialize` patch carries the headers to the wire from there.
215
+ # A no-op outside collector mode.
216
+ def inject_context_into(jobs)
217
+ jobs.each do |job|
218
+ headers = job.__otel_headers
219
+ Appsignal::OpenTelemetry.inject_context(headers)
220
+ Appsignal::OpenTelemetry.mark_active_job_batch(headers)
221
+ end
222
+ end
223
+
201
224
  # The batch's job class, when every job in it has the same one. Active
202
225
  # Job groups the jobs it enqueues by queue adapter rather than by class,
203
226
  # so a batch can mix classes, and then there is no one class to name.
@@ -208,6 +231,13 @@ module Appsignal
208
231
  "bulk enqueue #{job_class} jobs"
209
232
  end
210
233
 
234
+ # The queue the batch went to, when every job in it is on the same one.
235
+ # Grouping is by queue adapter and not by queue, so a batch can span
236
+ # queues, and then there is no one queue to name as the destination.
237
+ def bulk_enqueue_destination(jobs)
238
+ shared_across(jobs, &:queue_name)
239
+ end
240
+
211
241
  # The one value every job in the batch shares, or nil when they differ
212
242
  # or the batch is empty. Stops at the first job that disagrees, because
213
243
  # a batch is as large as the caller made it and a single mismatch is
@@ -241,12 +271,40 @@ module Appsignal
241
271
  # We don't have a separate integration for this QueueAdapter like
242
272
  # we do for Sidekiq.
243
273
  #
274
+ # Read the trace context off the job so the transaction links back
275
+ # to the enqueuer (no-op outside collector mode). Only here, in the
276
+ # standalone branch: when a wrapper integration (e.g. Sidekiq)
277
+ # created the transaction, it already extracted, so we must not
278
+ # extract a second time.
279
+ #
244
280
  # Prefer job_id from provider, instead of ActiveJob's internal ID.
245
- Appsignal::Transaction.create(Appsignal::Transaction::BACKGROUND_JOB)
281
+ Appsignal::Transaction.create(
282
+ Appsignal::Transaction::BACKGROUND_JOB,
283
+ :opentelemetry_context => Appsignal::OpenTelemetry.extract_job_context(job),
284
+ :opentelemetry_scope => ["appsignal-ruby/active_job", Appsignal::VERSION],
285
+ :opentelemetry_kind => :consumer,
286
+ :opentelemetry_relationship =>
287
+ Appsignal::OpenTelemetry.active_job_relationship(job)
288
+ )
246
289
  end
247
290
 
291
+ unless has_wrapper_transaction
292
+ # Describes this span as a job being performed. The messaging
293
+ # system is what the trace timeline reads to recognize background
294
+ # job work, and `active_job` is the value OpenTelemetry's own Active
295
+ # Job instrumentation uses.
296
+ #
297
+ # Only set when this hook created the transaction. When an adapter
298
+ # integration created it, that adapter already named itself, and its
299
+ # answer is the more specific one.
300
+ transaction.add_opentelemetry_attributes(
301
+ Appsignal::OpenTelemetry::Messaging
302
+ .perform_attributes("active_job", :destination => job["queue_name"])
303
+ )
304
+ end
305
+
248
306
  begin
249
- transaction.add_params_if_nil(job["arguments"])
307
+ transaction.add_function_parameters_if_nil(job["arguments"])
250
308
 
251
309
  transaction_tags = ActiveJobHelpers.transaction_tags_for(job)
252
310
  transaction.add_tags(transaction_tags)
@@ -302,6 +360,85 @@ module Appsignal
302
360
  end
303
361
  end
304
362
 
363
+ # Reads and writes W3C trace context on the ActiveJob enqueue/perform
364
+ # boundary, wire-compatible with OpenTelemetry's ActiveJob instrumentation.
365
+ # All of this no-ops outside collector mode.
366
+ #
367
+ # Context rides on the job under `__otel_headers`, the same carrier OTel
368
+ # uses. Stock `serialize`/`deserialize` only carry a fixed key set, so --
369
+ # like OTel -- we patch both plus an accessor to round-trip it. The on-wire
370
+ # value is run through ActiveJob's argument serializer (an array of
371
+ # `[key, value]` pairs), matching OTel byte-for-byte so an AppSignal- and an
372
+ # OTel-instrumented service read each other's jobs.
373
+ module ActiveJobTraceContext
374
+ # Inject on enqueue from inside a producer event, so the job carries this
375
+ # transaction's context and the perform later links back. Mirrors the
376
+ # Sidekiq client middleware: an AppSignal event (a producer span in
377
+ # collector mode), not a direct SDK span. `Appsignal.instrument` is a
378
+ # transparent pass-through when there's no active transaction, and
379
+ # `inject_context` no-ops outside collector mode.
380
+ def enqueue(*, **)
381
+ # When enqueue instrumentation is disabled, drop the trace context
382
+ # along with the event. Without an enqueue event there is no producer
383
+ # span, so the context we would write is that of whatever span is
384
+ # current, such as the surrounding web request. The job that performs
385
+ # later would then link back to a span that is not a producer.
386
+ return super if Appsignal.config && !Appsignal.config[:enable_job_enqueue_instrumentation]
387
+
388
+ # Another enqueue integration is already recording this enqueue, so
389
+ # don't record it a second time.
390
+ if Appsignal::Transaction.current? &&
391
+ Appsignal::Transaction.current.job_enqueue_events_suppressed?
392
+ return super
393
+ end
394
+
395
+ Appsignal.instrument(
396
+ "enqueue.active_job",
397
+ "enqueue #{self.class.name} job",
398
+ :opentelemetry_kind => :producer,
399
+ :opentelemetry_scope => ["appsignal-ruby/active_job", Appsignal::VERSION]
400
+ ) do
401
+ Appsignal::Transaction.current.add_opentelemetry_attributes(
402
+ Appsignal::OpenTelemetry::Messaging
403
+ .enqueue_attributes("active_job", :destination => queue_name)
404
+ )
405
+ Appsignal::OpenTelemetry.inject_context(__otel_headers)
406
+ # Active Job enqueues through an adapter (Sidekiq, Resque, ...) that
407
+ # has its own enqueue instrumentation. Suppress it so the enqueue is
408
+ # recorded once, as this event, rather than as nested Active Job +
409
+ # adapter events.
410
+ if Appsignal::Transaction.current?
411
+ Appsignal::Transaction.current.suppress_job_enqueue_events { super }
412
+ else
413
+ super
414
+ end
415
+ end
416
+ end
417
+
418
+ def serialize
419
+ super.tap do |data|
420
+ Appsignal::OpenTelemetry.if_started do
421
+ next if __otel_headers.empty?
422
+
423
+ data["__otel_headers"] = ::ActiveJob::Arguments.serialize(__otel_headers)
424
+ end
425
+ end
426
+ end
427
+
428
+ def deserialize(job_data)
429
+ super
430
+ serialized = job_data["__otel_headers"]
431
+ @__otel_headers =
432
+ serialized ? ::ActiveJob::Arguments.deserialize(serialized).to_h : {}
433
+ end
434
+
435
+ def __otel_headers
436
+ @__otel_headers ||= {}
437
+ end
438
+
439
+ attr_writer :__otel_headers
440
+ end
441
+
305
442
  module ActiveJobHelpers
306
443
  ACTION_MAILER_CLASSES = [
307
444
  "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,29 +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` and `enqueue_all.active_job`.
13
- # It records its own event for a single enqueue, and one event for a
14
- # whole batch, with Rails' native notification nested inside. The
15
- # Faraday integration owns `request.faraday`.
16
- SUPPRESSED_EVENT_NAMES = [
17
- "enqueue.active_job",
18
- "enqueue_all.active_job",
19
- "request.faraday"
20
- ].freeze
21
-
22
10
  def start_event(name)
23
11
  return unless record_event?(name)
24
12
 
25
- 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]
26
42
  end
27
43
 
28
44
  def finish_event(name, payload = {})
29
45
  return unless record_event?(name)
30
46
 
31
47
  title, body, body_format = Appsignal::EventFormatter.format(name, payload)
32
- 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(
33
56
  name.to_s,
34
57
  title,
35
58
  body,
@@ -37,12 +60,29 @@ module Appsignal
37
60
  )
38
61
  end
39
62
 
40
- # Events starting with a bang are internal to Rails; suppressed events
41
- # are recorded by a dedicated integration instead. Both `start_event`
42
- # 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.
43
84
  def record_event?(name)
44
- name = name.to_s
45
- name[0] != BANG && !SUPPRESSED_EVENT_NAMES.include?(name)
85
+ name.to_s[0] != BANG && Appsignal::EventFormatter.record?(name)
46
86
  end
47
87
  end
48
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