sentry-ruby-core 7.2.0 → 7.3.0

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 (46) hide show
  1. checksums.yaml +4 -4
  2. data/gemfiles/ruby-2.7_rack-2_redis-4.gemfile.lock +13 -13
  3. data/gemfiles/ruby-2.7_rack-3.1_redis-4.gemfile.lock +13 -13
  4. data/gemfiles/ruby-2.7_rack-3_redis-4.gemfile.lock +13 -13
  5. data/gemfiles/ruby-3.0_rack-2_redis-4.gemfile.lock +14 -14
  6. data/gemfiles/ruby-3.0_rack-3.1_redis-4.gemfile.lock +14 -14
  7. data/gemfiles/ruby-3.0_rack-3_redis-4.gemfile.lock +14 -14
  8. data/gemfiles/ruby-3.1_rack-2_redis-4.gemfile.lock +28 -28
  9. data/gemfiles/ruby-3.1_rack-3.1_redis-4.gemfile.lock +28 -28
  10. data/gemfiles/ruby-3.1_rack-3_redis-4.gemfile.lock +28 -28
  11. data/gemfiles/ruby-3.2_rack-0_redis-5.gemfile.lock +28 -28
  12. data/gemfiles/ruby-3.2_rack-2_redis-4.gemfile.lock +30 -30
  13. data/gemfiles/ruby-3.2_rack-2_redis-5.gemfile.lock +30 -30
  14. data/gemfiles/ruby-3.2_rack-3.1_redis-4.gemfile.lock +30 -30
  15. data/gemfiles/ruby-3.2_rack-3_redis-4.gemfile.lock +30 -30
  16. data/gemfiles/ruby-3.2_rack-3_redis-5.gemfile.lock +30 -30
  17. data/gemfiles/ruby-3.3_rack-2_redis-4.gemfile.lock +32 -33
  18. data/gemfiles/ruby-3.3_rack-3.1_redis-4.gemfile.lock +32 -33
  19. data/gemfiles/ruby-3.3_rack-3.1_redis-5.3.gemfile.lock +32 -33
  20. data/gemfiles/ruby-3.3_rack-3_redis-4.gemfile.lock +32 -33
  21. data/gemfiles/ruby-3.4_rack-2_redis-4.gemfile.lock +32 -33
  22. data/gemfiles/ruby-3.4_rack-3.1_redis-4.gemfile.lock +32 -33
  23. data/gemfiles/ruby-3.4_rack-3.1_redis-5.3.gemfile.lock +32 -33
  24. data/gemfiles/ruby-3.4_rack-3_redis-4.gemfile.lock +32 -33
  25. data/gemfiles/ruby-4.0_rack-2_redis-4.gemfile.lock +32 -33
  26. data/gemfiles/ruby-4.0_rack-3.1_redis-4.gemfile.lock +32 -33
  27. data/gemfiles/ruby-4.0_rack-3_redis-4.gemfile.lock +32 -33
  28. data/gemfiles/ruby-jruby-9.4.14.0_rack-2_redis-4.gemfile.lock +32 -32
  29. data/gemfiles/ruby-jruby-9.4.14.0_rack-3.1_redis-4.gemfile.lock +32 -32
  30. data/gemfiles/ruby-jruby-9.4.14.0_rack-3_redis-4.gemfile.lock +32 -32
  31. data/lib/sentry/configuration.rb +2 -26
  32. data/lib/sentry/debug_structured_logger.rb +18 -21
  33. data/lib/sentry/job_extensions/extension.rb +348 -0
  34. data/lib/sentry/propagation_context.rb +2 -0
  35. data/lib/sentry/redis.rb +7 -1
  36. data/lib/sentry/rufus_scheduler.rb +10 -5
  37. data/lib/sentry/scope.rb +39 -1
  38. data/lib/sentry/session_flusher.rb +5 -2
  39. data/lib/sentry/span.rb +25 -2
  40. data/lib/sentry/structured_logger.rb +16 -13
  41. data/lib/sentry/transport/debug_transport.rb +70 -19
  42. data/lib/sentry/utils/latency.rb +18 -0
  43. data/lib/sentry/utils/ring_buffer.rb +38 -0
  44. data/lib/sentry/version.rb +1 -1
  45. data/lib/sentry-ruby.rb +12 -0
  46. metadata +6 -3
@@ -0,0 +1,348 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sentry
4
+ module JobExtensions
5
+ # Base class for extensions that let a background-job backend enrich Sentry's
6
+ # job integrations. A backend gem subclasses it and overrides the hooks it can
7
+ # supply; jobs are opaque to Sentry and only interpreted by the extension.
8
+ class Extension
9
+ include LoggingHelper
10
+
11
+ PUBLISH_OP = "queue.publish"
12
+ PROCESS_OP = "queue.process"
13
+
14
+ MESSAGING_DATA_ATTRIBUTES = {
15
+ system: Span::DataConventions::MESSAGING_SYSTEM,
16
+ message_id: Span::DataConventions::MESSAGING_MESSAGE_ID,
17
+ destination: Span::DataConventions::MESSAGING_DESTINATION_NAME,
18
+ receive_latency: Span::DataConventions::MESSAGING_MESSAGE_RECEIVE_LATENCY,
19
+ retry_count: Span::DataConventions::MESSAGING_MESSAGE_RETRY_COUNT,
20
+ body_size: Span::DataConventions::MESSAGING_MESSAGE_BODY_SIZE
21
+ }.freeze
22
+
23
+ EMPTY_HASH = {}.freeze
24
+ private_constant :EMPTY_HASH
25
+
26
+ EMPTY_ERROR_DATA = { contexts: {}.freeze, tags: {}.freeze, extra: {}.freeze }.freeze
27
+ private_constant :EMPTY_ERROR_DATA
28
+
29
+ # The extension's name, used in SDK log messages.
30
+ # @return [Symbol]
31
+ attr_reader :name
32
+
33
+ # Creates an extension. Raises ArgumentError if the name is not a Symbol.
34
+ #
35
+ # @param name [Symbol]
36
+ # @param integration [Module] the integration that captures the job's exceptions
37
+ # @param origin [String] the origin of the job's spans
38
+ def initialize(name:, integration:, origin:)
39
+ raise ArgumentError, "job extension name must be a Symbol, got #{name.inspect}" unless name.is_a?(Symbol)
40
+
41
+ @name = name
42
+ @integration = integration
43
+ @origin = origin
44
+ end
45
+
46
+ # Wraps enqueueing a job in a `queue.publish` span.
47
+ #
48
+ # @param job [Object]
49
+ # @yieldreturn [Object] the result of enqueueing the job
50
+ # @return [Object] the block's result
51
+ def instrument_enqueue(job:, **context, &enqueue)
52
+ return enqueue.call unless safely("decide whether to record the producer span", fallback: true) { record_publish_span?(job: job, **context) }
53
+
54
+ Sentry.with_child_span(op: PUBLISH_OP) do |span|
55
+ if span
56
+ span.set_description(safely("describe the producer span") { publish_description(job: job, **context) })
57
+ span.set_origin(safely("set the producer span origin", fallback: @origin) { publish_span_origin(job: job, **context) })
58
+ set_messaging_data(span, hook_hash("collect the producer messaging data") { publish_messaging_data(job: job, **context) })
59
+ end
60
+
61
+ enqueue.call
62
+ end
63
+ end
64
+
65
+ # Prepares the scope for performing the job and starts its `queue.process`
66
+ # transaction, continuing the trace the job was enqueued in.
67
+ #
68
+ # @param scope [Scope]
69
+ # @param job [Object]
70
+ # @return [Transaction, nil]
71
+ def start_job(scope:, job:, **context)
72
+ data = hook_hash("collect the scope data") { scope_data(job: job, **context) }
73
+ safely("apply the scope data") { apply_scope_data(scope, data) }
74
+
75
+ job_name = safely("name the transaction") { transaction_name(job: job, **context) }
76
+ scope.set_transaction_name(job_name, source: :task) if job_name
77
+
78
+ trace_headers = hook_hash("extract trace headers") { extract_trace_headers(job: job, **context) }
79
+ origin = safely("set the transaction origin", fallback: @origin) { span_origin(job: job, **context) }
80
+ transaction = start_transaction(scope, trace_headers, origin)
81
+ return unless transaction
82
+
83
+ scope.set_span(transaction)
84
+ set_messaging_data(transaction, hook_hash("collect the messaging data") { messaging_data(job: job, **context) })
85
+ transaction
86
+ end
87
+
88
+ # Finishes the job's transaction with the given status.
89
+ #
90
+ # @param transaction [Transaction, nil]
91
+ # @param status [Integer]
92
+ # @return [void]
93
+ def finish_job(transaction:, status:)
94
+ return if !transaction || transaction.timestamp
95
+
96
+ transaction.set_http_status(status)
97
+ transaction.finish
98
+ end
99
+
100
+ # Captures an exception raised by the job, enriched with the extension's error data.
101
+ #
102
+ # @param job [Object]
103
+ # @param exception [Exception]
104
+ # @return [Event, nil]
105
+ def capture_exception(job:, exception:, contexts: {}, tags: {}, extra: {}, **context)
106
+ return unless Sentry.initialized?
107
+
108
+ data = safely("collect the error data", fallback: EMPTY_ERROR_DATA) { normalized_error_data(error_data(job: job, exception: exception, **context)) }
109
+ capturing_integration = safely("pick the capturing integration", fallback: @integration) { integration(job: job, **context) }
110
+
111
+ capturing_integration.capture_exception(exception, **merged_error_data(data, contexts: contexts, tags: tags, extra: extra))
112
+ end
113
+
114
+ # Runs a hook, logging its failure and returning `fallback` instead of raising.
115
+ #
116
+ # @param context [String] what the hook does, used in the log message
117
+ # @return [Object]
118
+ def safely(context, fallback: nil)
119
+ yield
120
+ rescue StandardError => e
121
+ log_failure(context, e)
122
+ fallback
123
+ end
124
+
125
+ ##### Hooks #####
126
+ #
127
+ # Called by job integrations with the job and any extra keyword context.
128
+ # Hooks must not mutate the objects they are given.
129
+
130
+ # Returns whether to record a `queue.publish` span when the job is enqueued.
131
+ #
132
+ # @param job [Object]
133
+ # @return [Boolean]
134
+ def record_publish_span?(job:, **)
135
+ true
136
+ end
137
+
138
+ # Returns data to apply to the scope the job is performed in.
139
+ #
140
+ # @param job [Object]
141
+ # @return [Hash] with optional `:contexts`, `:tags`, `:extra` and `:user`
142
+ def scope_data(job:, **)
143
+ {}
144
+ end
145
+
146
+ # Returns data to add to the event captured for a failed job. Data already on
147
+ # the scope or passed explicitly by the job integration takes precedence.
148
+ #
149
+ # @param job [Object]
150
+ # @param exception [Exception]
151
+ # @return [Hash] with optional `:contexts`, `:tags` and `:extra`
152
+ def error_data(job:, exception:, **)
153
+ {}
154
+ end
155
+
156
+ # Returns whether the backend will retry the failed job.
157
+ #
158
+ # @param job [Object]
159
+ # @return [Boolean]
160
+ def will_retry?(job:, **)
161
+ false
162
+ end
163
+
164
+ # Returns whether to capture the job's exception. By default only when the
165
+ # backend won't retry the job.
166
+ #
167
+ # @param job [Object]
168
+ # @param exception [Exception]
169
+ # @return [Boolean]
170
+ def report_exception?(job:, exception:, **context)
171
+ !will_retry?(job: job, exception: exception, **context)
172
+ end
173
+
174
+ # Returns whether to capture an exception that the job integration's own
175
+ # retry mechanism retries, so the backend never sees it.
176
+ #
177
+ # @param job [Object]
178
+ # @param exception [Exception]
179
+ # @return [Boolean]
180
+ def report_retried_exception?(job:, exception:, **)
181
+ false
182
+ end
183
+
184
+ # Returns the backend's job payload carrying the trace propagation headers.
185
+ #
186
+ # @param job [Object]
187
+ # @param payload [Object]
188
+ # @param headers [Hash{String => String}]
189
+ # @return [Object] a payload of the same class as the one given
190
+ def inject_trace_headers(job:, payload:, headers:, **)
191
+ payload
192
+ end
193
+
194
+ # Returns the trace propagation headers the job carries, so that performing
195
+ # it continues the trace it was enqueued in.
196
+ #
197
+ # @param job [Object]
198
+ # @return [Hash{String => String}]
199
+ def extract_trace_headers(job:, **)
200
+ {}
201
+ end
202
+
203
+ # Returns messaging attributes for the job's spans.
204
+ #
205
+ # @param job [Object]
206
+ # @return [Hash] with optional `:system`, `:message_id`, `:destination`,
207
+ # `:receive_latency`, `:retry_count` and `:body_size`
208
+ def messaging_data(job:, **)
209
+ {}
210
+ end
211
+
212
+ # Returns messaging attributes for the `queue.publish` span. Defaults to
213
+ # {#messaging_data}.
214
+ #
215
+ # @param job [Object]
216
+ # @return [Hash]
217
+ def publish_messaging_data(job:, **context)
218
+ messaging_data(job: job, **context)
219
+ end
220
+
221
+ # Returns the name of the job's transaction. Defaults to the job's class name.
222
+ #
223
+ # @param job [Object]
224
+ # @return [String, nil]
225
+ def transaction_name(job:, **)
226
+ job.class.name
227
+ end
228
+
229
+ # Returns the description of the `queue.publish` span. Defaults to the job's class name.
230
+ #
231
+ # @param job [Object]
232
+ # @return [String, nil]
233
+ def publish_description(job:, **)
234
+ job.class.name
235
+ end
236
+
237
+ # Returns the origin of the job's transaction. Defaults to the extension's origin.
238
+ #
239
+ # @param job [Object]
240
+ # @return [String]
241
+ def span_origin(job:, **)
242
+ @origin
243
+ end
244
+
245
+ # Returns the origin of the `queue.publish` span. Defaults to {#span_origin}.
246
+ #
247
+ # @param job [Object]
248
+ # @return [String]
249
+ def publish_span_origin(job:, **context)
250
+ span_origin(job: job, **context)
251
+ end
252
+
253
+ # Returns the integration that captures the job's exceptions. Defaults to
254
+ # the extension's integration.
255
+ #
256
+ # @param job [Object]
257
+ # @return [Module]
258
+ def integration(job:, **)
259
+ @integration
260
+ end
261
+
262
+ private
263
+
264
+ def hook_hash(description)
265
+ safely(description, fallback: EMPTY_HASH) do
266
+ value = yield
267
+ value.nil? ? EMPTY_HASH : Hash(value)
268
+ end
269
+ end
270
+
271
+ def start_transaction(scope, trace_headers, origin)
272
+ options = {
273
+ name: scope.transaction_name,
274
+ source: scope.transaction_source,
275
+ op: PROCESS_OP,
276
+ origin: origin
277
+ }
278
+
279
+ if trace_headers.empty?
280
+ Sentry.start_transaction(**options)
281
+ else
282
+ Sentry.start_transaction(transaction: Sentry.continue_trace(trace_headers, **options), **options)
283
+ end
284
+ end
285
+
286
+ def apply_scope_data(scope, data)
287
+ scope.set_contexts(data[:contexts]) if data[:contexts]
288
+ scope.set_tags(data[:tags]) if data[:tags]
289
+ scope.set_extras(data[:extra]) if data[:extra]
290
+ scope.set_user(data[:user]) if data[:user]
291
+ end
292
+
293
+ def set_messaging_data(span, data)
294
+ MESSAGING_DATA_ATTRIBUTES.each do |key, attribute|
295
+ span.set_data(attribute, data[key]) if data.key?(key)
296
+ end
297
+ end
298
+
299
+ def normalized_error_data(data)
300
+ data = Hash(data)
301
+
302
+ {
303
+ contexts: Hash(data[:contexts]).transform_values { |context| Hash(context) },
304
+ tags: Hash(data[:tags]),
305
+ extra: Hash(data[:extra])
306
+ }
307
+ end
308
+
309
+ # Builds the capture options so that the caller's contexts, tags and extra
310
+ # take precedence over the scope's, and the scope's over the extension's.
311
+ #
312
+ # Every value passed to capture_exception becomes event data, and
313
+ # Scope#apply_to_event lets event data beat scope data. It also merges
314
+ # contexts by name, so an event context replaces the scope context of the
315
+ # same name entirely. The extension's tags and extra therefore leave out
316
+ # keys the scope already has, and each extension context absorbs the scope
317
+ # context of the same name before the caller's values are merged on top.
318
+ def merged_error_data(data, contexts:, tags:, extra:)
319
+ scope = Sentry.get_current_scope
320
+
321
+ merged_contexts = contexts_with_scope_values(data[:contexts], scope.contexts).merge(contexts)
322
+ options = {
323
+ extra: without_scope_keys(data[:extra], scope.extra).merge(extra),
324
+ tags: without_scope_keys(data[:tags], scope.tags).merge(tags),
325
+ hint: { background: false }
326
+ }
327
+ options[:contexts] = merged_contexts unless merged_contexts.empty?
328
+ options
329
+ end
330
+
331
+ def without_scope_keys(values, scope_values)
332
+ values.reject { |key, _value| scope_values.key?(key) }
333
+ end
334
+
335
+ def contexts_with_scope_values(contexts, scope_contexts)
336
+ contexts.to_h { |name, context| [name, context.merge(scope_contexts.fetch(name, {}))] }
337
+ end
338
+
339
+ def log_failure(context, error)
340
+ log_error("Job extension #{name.inspect} failed to #{context}", error)
341
+ end
342
+
343
+ def sdk_logger
344
+ Sentry.sdk_logger if Sentry.initialized?
345
+ end
346
+ end
347
+ end
348
+ end
@@ -48,6 +48,8 @@ module Sentry
48
48
  return if match.nil?
49
49
 
50
50
  trace_id, parent_span_id, sampled_flag = match[1..3]
51
+ return if trace_id.nil?
52
+
51
53
  parent_sampled = sampled_flag.nil? ? nil : sampled_flag != "0"
52
54
 
53
55
  [trace_id, parent_span_id, parent_sampled]
data/lib/sentry/redis.rb CHANGED
@@ -6,6 +6,10 @@ module Sentry
6
6
  OP_NAME = "db.redis"
7
7
  SPAN_ORIGIN = "auto.db.redis"
8
8
  LOGGER_NAME = :redis_logger
9
+ # Commands that carry credentials from their first argument on.
10
+ SENSITIVE_COMMANDS = %w[AUTH HELLO].freeze
11
+ # Commands whose later arguments can carry credentials. Arguments are never recorded.
12
+ ARGUMENTLESS_COMMANDS = %w[ACL CONFIG MIGRATE SENTINEL].freeze
9
13
 
10
14
  def initialize(commands, host, port, db)
11
15
  @commands, @host, @port, @db = commands, host, port, db
@@ -60,9 +64,11 @@ module Sentry
60
64
  commands.map do |statement|
61
65
  command, key, *arguments = statement
62
66
  command_set = { command: command.to_s.upcase }
67
+ next command_set if SENSITIVE_COMMANDS.include?(command_set[:command])
68
+
63
69
  command_set[:key] = key if Utils::EncodingHelper.valid_utf_8?(key)
64
70
 
65
- if Sentry.configuration.data_collection.database_query_data
71
+ if Sentry.configuration.data_collection.database_query_data && !ARGUMENTLESS_COMMANDS.include?(command_set[:command])
66
72
  command_set[:arguments] = arguments
67
73
  .select { |a| Utils::EncodingHelper.valid_utf_8?(a) }
68
74
  .join(" ")
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sentry
4
- # Sends Crons check-ins for rufus-scheduler `cron`, `every` and `interval` jobs.
4
+ # Sends Crons check-ins for rufus-scheduler `cron` and `every` jobs.
5
5
  # Enable with `config.enabled_patches << :rufus_scheduler`.
6
6
  module RufusScheduler
7
7
  module Job
@@ -46,7 +46,7 @@ module Sentry
46
46
  end
47
47
 
48
48
  class << self
49
- # Returns [slug, monitor_config], or nil for jobs that aren't monitored (like `at`/`in`).
49
+ # Returns [slug, monitor_config], or nil for jobs that aren't monitored (like `at`/`in`, or unsupported schedules).
50
50
  def monitor_for(job)
51
51
  return unless job.is_a?(::Rufus::Scheduler::RepeatJob)
52
52
  return if job.opts[:sentry_monitor] == false
@@ -61,7 +61,13 @@ module Sentry
61
61
  return
62
62
  end
63
63
 
64
- [slug, monitor_config_for(job)]
64
+ monitor_config = monitor_config_for(job)
65
+ unless monitor_config
66
+ Sentry.sdk_logger.debug(Sentry::LOGGER_PROGNAME) { "Not monitoring rufus-scheduler job #{slug}: its schedule can't be expressed as a Crons schedule." }
67
+ return
68
+ end
69
+
70
+ [slug, monitor_config]
65
71
  end
66
72
 
67
73
  private
@@ -98,8 +104,7 @@ module Sentry
98
104
  when ::Rufus::Scheduler::EveryJob
99
105
  Sentry::Cron::FugitSchedule.from_seconds(job.frequency)
100
106
  end
101
- # IntervalJob waits `interval` after each run ends, so it has no fixed
102
- # schedule to monitor against.
107
+ # IntervalJob waits `interval` after each run ends, so no fixed schedule matches it.
103
108
  end
104
109
  end
105
110
  end
data/lib/sentry/scope.rb CHANGED
@@ -4,6 +4,7 @@ require "set"
4
4
  require "sentry/breadcrumb_buffer"
5
5
  require "sentry/propagation_context"
6
6
  require "sentry/attachment"
7
+ require "sentry/utils/ring_buffer"
7
8
  require "etc"
8
9
 
9
10
  module Sentry
@@ -27,9 +28,13 @@ module Sentry
27
28
  :session,
28
29
  :attachments,
29
30
  :propagation_context,
30
- :attributes
31
+ :attributes,
32
+ :flags
31
33
  ]
32
34
 
35
+ # Maximum number of feature flag evaluations kept on a scope.
36
+ MAX_FLAGS = 100
37
+
33
38
  attr_reader(*ATTRIBUTES)
34
39
 
35
40
  # @param max_breadcrumbs [Integer] the maximum number of breadcrumbs to be stored in the scope.
@@ -61,6 +66,7 @@ module Sentry
61
66
  event.breadcrumbs = breadcrumbs
62
67
  event.rack_env = rack_env if rack_env
63
68
  event.attachments = attachments
69
+ apply_flags_to_event(event)
64
70
  end
65
71
 
66
72
  trace_context = get_trace_context
@@ -151,6 +157,7 @@ module Sentry
151
157
  copy.attachments = attachments.dup
152
158
  copy.event_processors = event_processors.dup
153
159
  copy.attributes = attributes.deep_dup
160
+ copy.flags = flags.dup
154
161
  copy
155
162
  end
156
163
 
@@ -170,6 +177,7 @@ module Sentry
170
177
  self.propagation_context = scope.propagation_context
171
178
  self.attachments = scope.attachments
172
179
  self.attributes = scope.attributes
180
+ self.flags = scope.flags.dup
173
181
  end
174
182
 
175
183
  # Updates the scope's data from the given options.
@@ -298,6 +306,22 @@ module Sentry
298
306
  @attributes.delete(key.to_s)
299
307
  end
300
308
 
309
+ # Records a feature flag evaluation on the scope (and the active span, if any).
310
+ # Only boolean results are supported; other values are ignored.
311
+ # The scope keeps the most recent {MAX_FLAGS} distinct flags (oldest evicted first).
312
+ # @param name [String, Symbol] the flag name
313
+ # @param result [Boolean] the evaluation result
314
+ # @return [void]
315
+ def add_feature_flag(name, result)
316
+ return unless result == true || result == false
317
+
318
+ name = name.to_s
319
+ flags.set(name, result)
320
+
321
+ add_feature_flag_to_span(name, result)
322
+ nil
323
+ end
324
+
301
325
  # Sets the scope's level attribute.
302
326
  # @param level [String, Symbol]
303
327
  # @return [void]
@@ -389,6 +413,19 @@ module Sentry
389
413
 
390
414
  private
391
415
 
416
+ def add_feature_flag_to_span(name, result)
417
+ return unless span
418
+
419
+ span.set_flag(name, result)
420
+ end
421
+
422
+ def apply_flags_to_event(event)
423
+ return unless event.is_a?(ErrorEvent) && !flags.empty?
424
+
425
+ values = flags.values.map { |flag, result| { flag: flag, result: result } }
426
+ event.contexts[:flags] = { values: values }
427
+ end
428
+
392
429
  def set_default_value
393
430
  @contexts = { os: self.class.os_context, runtime: self.class.runtime_context }
394
431
  @extra = {}
@@ -404,6 +441,7 @@ module Sentry
404
441
  @session = nil
405
442
  @attachments = []
406
443
  @attributes = {}
444
+ @flags = Utils::RingBuffer.new(MAX_FLAGS)
407
445
  generate_propagation_context
408
446
  set_new_breadcrumb_buffer
409
447
  end
@@ -29,8 +29,11 @@ module Sentry
29
29
  return unless ensure_thread
30
30
 
31
31
  return unless Session::AGGREGATE_STATUSES.include?(session.status)
32
- @pending_aggregates[session.aggregation_key] ||= init_aggregates(session.aggregation_key)
33
- @pending_aggregates[session.aggregation_key][session.status] += 1
32
+
33
+ @mutex.synchronize do
34
+ @pending_aggregates[session.aggregation_key] ||= init_aggregates(session.aggregation_key)
35
+ @pending_aggregates[session.aggregation_key][session.status] += 1
36
+ end
34
37
  end
35
38
 
36
39
  private
data/lib/sentry/span.rb CHANGED
@@ -50,6 +50,7 @@ module Sentry
50
50
  MESSAGING_DESTINATION_NAME = "messaging.destination.name"
51
51
  MESSAGING_MESSAGE_RECEIVE_LATENCY = "messaging.message.receive.latency"
52
52
  MESSAGING_MESSAGE_RETRY_COUNT = "messaging.message.retry.count"
53
+ MESSAGING_MESSAGE_BODY_SIZE = "messaging.message.body.size"
53
54
 
54
55
  # Time in ms the request spent in the server queue before processing began.
55
56
  HTTP_QUEUE_TIME_MS = "http.server.request.time_in_queue"
@@ -70,6 +71,8 @@ module Sentry
70
71
  }
71
72
 
72
73
  DEFAULT_SPAN_ORIGIN = "manual"
74
+ MAX_FLAGS = 10
75
+ SPAN_FLAG_ATTRIBUTE_PREFIX = "flag.evaluation."
73
76
 
74
77
  # An uuid that can be used to identify a trace.
75
78
  # @return [String]
@@ -104,6 +107,9 @@ module Sentry
104
107
  # Span data
105
108
  # @return [Hash]
106
109
  attr_reader :data
110
+ # Feature flag data
111
+ # @return [Hash]
112
+ attr_reader :flags
107
113
  # Span origin that tracks what kind of instrumentation created a span
108
114
  # @return [String]
109
115
  attr_reader :origin
@@ -142,6 +148,7 @@ module Sentry
142
148
  @op = op
143
149
  @status = status
144
150
  @data = {}
151
+ @flags = {}
145
152
  @tags = {}
146
153
  @origin = origin || DEFAULT_SPAN_ORIGIN
147
154
  end
@@ -187,7 +194,7 @@ module Sentry
187
194
  op: @op,
188
195
  status: @status,
189
196
  tags: @tags,
190
- data: @data,
197
+ data: serialized_data,
191
198
  origin: @origin
192
199
  }
193
200
  end
@@ -203,7 +210,7 @@ module Sentry
203
210
  op: @op,
204
211
  status: @status,
205
212
  origin: @origin,
206
- data: @data
213
+ data: serialized_data
207
214
  }
208
215
  end
209
216
 
@@ -293,6 +300,14 @@ module Sentry
293
300
  @data[key] = value
294
301
  end
295
302
 
303
+ # Sets a feature flag on the span.
304
+ # @param name [String] the flag name
305
+ # @param result [Boolean] the evaluation result
306
+ def set_flag(name, result)
307
+ flag = "#{SPAN_FLAG_ATTRIBUTE_PREFIX}#{name}"
308
+ @flags[flag] = result if @flags.size < MAX_FLAGS
309
+ end
310
+
296
311
  # Sets a tag to the span.
297
312
  # @param key [String, Symbol]
298
313
  # @param value [String]
@@ -305,5 +320,13 @@ module Sentry
305
320
  def set_origin(origin)
306
321
  @origin = origin
307
322
  end
323
+
324
+ private
325
+
326
+ def serialized_data
327
+ return @data if @flags.empty?
328
+
329
+ @flags.merge(@data)
330
+ end
308
331
  end
309
332
  end
@@ -59,8 +59,8 @@ module Sentry
59
59
  # @param attributes [Hash] Additional attributes to include with the log
60
60
  #
61
61
  # @return [LogEvent, nil] The created log event or nil if logging is disabled
62
- def trace(message, parameters = [], **attributes)
63
- log(__method__, message, parameters: parameters, **attributes)
62
+ def trace(message = nil, parameters = [], **attributes, &block)
63
+ log(__method__, message, parameters: parameters, **attributes, &block)
64
64
  end
65
65
 
66
66
  # Logs a message at DEBUG level
@@ -70,8 +70,8 @@ module Sentry
70
70
  # @param attributes [Hash] Additional attributes to include with the log
71
71
  #
72
72
  # @return [LogEvent, nil] The created log event or nil if logging is disabled
73
- def debug(message, parameters = [], **attributes)
74
- log(__method__, message, parameters: parameters, **attributes)
73
+ def debug(message = nil, parameters = [], **attributes, &block)
74
+ log(__method__, message, parameters: parameters, **attributes, &block)
75
75
  end
76
76
 
77
77
  # Logs a message at INFO level
@@ -81,8 +81,8 @@ module Sentry
81
81
  # @param attributes [Hash] Additional attributes to include with the log
82
82
  #
83
83
  # @return [LogEvent, nil] The created log event or nil if logging is disabled
84
- def info(message, parameters = [], **attributes)
85
- log(__method__, message, parameters: parameters, **attributes)
84
+ def info(message = nil, parameters = [], **attributes, &block)
85
+ log(__method__, message, parameters: parameters, **attributes, &block)
86
86
  end
87
87
 
88
88
  # Logs a message at WARN level
@@ -92,8 +92,8 @@ module Sentry
92
92
  # @param attributes [Hash] Additional attributes to include with the log
93
93
  #
94
94
  # @return [LogEvent, nil] The created log event or nil if logging is disabled
95
- def warn(message, parameters = [], **attributes)
96
- log(__method__, message, parameters: parameters, **attributes)
95
+ def warn(message = nil, parameters = [], **attributes, &block)
96
+ log(__method__, message, parameters: parameters, **attributes, &block)
97
97
  end
98
98
 
99
99
  # Logs a message at ERROR level
@@ -103,8 +103,8 @@ module Sentry
103
103
  # @param attributes [Hash] Additional attributes to include with the log
104
104
  #
105
105
  # @return [LogEvent, nil] The created log event or nil if logging is disabled
106
- def error(message, parameters = [], **attributes)
107
- log(__method__, message, parameters: parameters, **attributes)
106
+ def error(message = nil, parameters = [], **attributes, &block)
107
+ log(__method__, message, parameters: parameters, **attributes, &block)
108
108
  end
109
109
 
110
110
  # Logs a message at FATAL level
@@ -114,8 +114,8 @@ module Sentry
114
114
  # @param attributes [Hash] Additional attributes to include with the log
115
115
  #
116
116
  # @return [LogEvent, nil] The created log event or nil if logging is disabled
117
- def fatal(message, parameters = [], **attributes)
118
- log(__method__, message, parameters: parameters, **attributes)
117
+ def fatal(message = nil, parameters = [], **attributes, &block)
118
+ log(__method__, message, parameters: parameters, **attributes, &block)
119
119
  end
120
120
 
121
121
  # Logs a message at the specified level
@@ -126,7 +126,10 @@ module Sentry
126
126
  # @param attributes [Hash] Additional attributes to include with the log
127
127
  #
128
128
  # @return [LogEvent, nil] The created log event or nil if logging is disabled
129
- def log(level, message, parameters:, **attributes)
129
+ def log(level, message = nil, parameters:, **attributes)
130
+ message = yield if block_given?
131
+ raise ArgumentError, "message or block is required" if message.nil?
132
+
130
133
  case parameters
131
134
  when Array then
132
135
  Sentry.capture_log(message, level: level, severity: LEVELS[level], parameters: parameters, **attributes)