sentry-ruby 5.10.0 → 5.17.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +2 -13
  3. data/README.md +10 -10
  4. data/Rakefile +1 -1
  5. data/lib/sentry/background_worker.rb +9 -2
  6. data/lib/sentry/backpressure_monitor.rb +75 -0
  7. data/lib/sentry/backtrace.rb +7 -3
  8. data/lib/sentry/breadcrumb.rb +8 -2
  9. data/lib/sentry/check_in_event.rb +60 -0
  10. data/lib/sentry/client.rb +88 -17
  11. data/lib/sentry/configuration.rb +66 -12
  12. data/lib/sentry/cron/configuration.rb +23 -0
  13. data/lib/sentry/cron/monitor_check_ins.rb +75 -0
  14. data/lib/sentry/cron/monitor_config.rb +53 -0
  15. data/lib/sentry/cron/monitor_schedule.rb +42 -0
  16. data/lib/sentry/dsn.rb +1 -1
  17. data/lib/sentry/envelope.rb +19 -2
  18. data/lib/sentry/error_event.rb +2 -2
  19. data/lib/sentry/event.rb +14 -36
  20. data/lib/sentry/hub.rb +70 -2
  21. data/lib/sentry/integrable.rb +10 -0
  22. data/lib/sentry/interface.rb +1 -0
  23. data/lib/sentry/interfaces/exception.rb +5 -3
  24. data/lib/sentry/interfaces/mechanism.rb +20 -0
  25. data/lib/sentry/interfaces/request.rb +2 -2
  26. data/lib/sentry/interfaces/single_exception.rb +10 -6
  27. data/lib/sentry/interfaces/stacktrace_builder.rb +8 -0
  28. data/lib/sentry/metrics/aggregator.rb +276 -0
  29. data/lib/sentry/metrics/configuration.rb +47 -0
  30. data/lib/sentry/metrics/counter_metric.rb +25 -0
  31. data/lib/sentry/metrics/distribution_metric.rb +25 -0
  32. data/lib/sentry/metrics/gauge_metric.rb +35 -0
  33. data/lib/sentry/metrics/local_aggregator.rb +53 -0
  34. data/lib/sentry/metrics/metric.rb +19 -0
  35. data/lib/sentry/metrics/set_metric.rb +28 -0
  36. data/lib/sentry/metrics/timing.rb +43 -0
  37. data/lib/sentry/metrics.rb +55 -0
  38. data/lib/sentry/net/http.rb +25 -22
  39. data/lib/sentry/profiler.rb +18 -7
  40. data/lib/sentry/propagation_context.rb +135 -0
  41. data/lib/sentry/puma.rb +12 -5
  42. data/lib/sentry/rack/capture_exceptions.rb +7 -5
  43. data/lib/sentry/rake.rb +3 -14
  44. data/lib/sentry/redis.rb +8 -3
  45. data/lib/sentry/release_detector.rb +1 -1
  46. data/lib/sentry/scope.rb +36 -15
  47. data/lib/sentry/session.rb +2 -2
  48. data/lib/sentry/session_flusher.rb +1 -6
  49. data/lib/sentry/span.rb +54 -3
  50. data/lib/sentry/test_helper.rb +18 -12
  51. data/lib/sentry/transaction.rb +33 -33
  52. data/lib/sentry/transaction_event.rb +5 -3
  53. data/lib/sentry/transport/configuration.rb +73 -1
  54. data/lib/sentry/transport/http_transport.rb +68 -37
  55. data/lib/sentry/transport/spotlight_transport.rb +50 -0
  56. data/lib/sentry/transport.rb +27 -37
  57. data/lib/sentry/utils/argument_checking_helper.rb +12 -0
  58. data/lib/sentry/utils/real_ip.rb +1 -1
  59. data/lib/sentry/utils/request_id.rb +1 -1
  60. data/lib/sentry/version.rb +1 -1
  61. data/lib/sentry-ruby.rb +96 -26
  62. data/sentry-ruby.gemspec +1 -0
  63. metadata +35 -2
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "set"
3
4
 
4
5
  module Sentry
@@ -23,17 +24,18 @@ module Sentry
23
24
  # @param stacktrace_builder [StacktraceBuilder]
24
25
  # @see SingleExceptionInterface#build_with_stacktrace
25
26
  # @see SingleExceptionInterface#initialize
27
+ # @param mechanism [Mechanism]
26
28
  # @return [ExceptionInterface]
27
- def self.build(exception:, stacktrace_builder:)
29
+ def self.build(exception:, stacktrace_builder:, mechanism:)
28
30
  exceptions = Sentry::Utils::ExceptionCauseChain.exception_to_array(exception).reverse
29
31
  processed_backtrace_ids = Set.new
30
32
 
31
33
  exceptions = exceptions.map do |e|
32
34
  if e.backtrace && !processed_backtrace_ids.include?(e.backtrace.object_id)
33
35
  processed_backtrace_ids << e.backtrace.object_id
34
- SingleExceptionInterface.build_with_stacktrace(exception: e, stacktrace_builder: stacktrace_builder)
36
+ SingleExceptionInterface.build_with_stacktrace(exception: e, stacktrace_builder: stacktrace_builder, mechanism: mechanism)
35
37
  else
36
- SingleExceptionInterface.new(exception: exception)
38
+ SingleExceptionInterface.new(exception: exception, mechanism: mechanism)
37
39
  end
38
40
  end
39
41
 
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sentry
4
+ class Mechanism < Interface
5
+ # Generic identifier, mostly the source integration for this exception.
6
+ # @return [String]
7
+ attr_accessor :type
8
+
9
+ # A manually captured exception has handled set to true,
10
+ # false if coming from an integration where we intercept an uncaught exception.
11
+ # Defaults to true here and will be set to false explicitly in integrations.
12
+ # @return [Boolean]
13
+ attr_accessor :handled
14
+
15
+ def initialize(type: 'generic', handled: true)
16
+ @type = type
17
+ @handled = handled
18
+ end
19
+ end
20
+ end
@@ -2,8 +2,8 @@
2
2
 
3
3
  module Sentry
4
4
  class RequestInterface < Interface
5
- REQUEST_ID_HEADERS = %w(action_dispatch.request_id HTTP_X_REQUEST_ID).freeze
6
- CONTENT_HEADERS = %w(CONTENT_TYPE CONTENT_LENGTH).freeze
5
+ REQUEST_ID_HEADERS = %w[action_dispatch.request_id HTTP_X_REQUEST_ID].freeze
6
+ CONTENT_HEADERS = %w[CONTENT_TYPE CONTENT_LENGTH].freeze
7
7
  IP_HEADERS = [
8
8
  "REMOTE_ADDR",
9
9
  "HTTP_CLIENT_IP",
@@ -11,9 +11,10 @@ module Sentry
11
11
  OMISSION_MARK = "...".freeze
12
12
  MAX_LOCAL_BYTES = 1024
13
13
 
14
- attr_reader :type, :value, :module, :thread_id, :stacktrace
14
+ attr_reader :type, :module, :thread_id, :stacktrace, :mechanism
15
+ attr_accessor :value
15
16
 
16
- def initialize(exception:, stacktrace: nil)
17
+ def initialize(exception:, mechanism:, stacktrace: nil)
17
18
  @type = exception.class.to_s
18
19
  exception_message =
19
20
  if exception.respond_to?(:detailed_message)
@@ -21,23 +22,26 @@ module Sentry
21
22
  else
22
23
  exception.message || ""
23
24
  end
25
+ exception_message = exception_message.inspect unless exception_message.is_a?(String)
24
26
 
25
- @value = exception_message.byteslice(0..Event::MAX_MESSAGE_SIZE_IN_BYTES)
27
+ @value = Utils::EncodingHelper.encode_to_utf_8(exception_message.byteslice(0..Event::MAX_MESSAGE_SIZE_IN_BYTES))
26
28
 
27
29
  @module = exception.class.to_s.split('::')[0...-1].join('::')
28
30
  @thread_id = Thread.current.object_id
29
31
  @stacktrace = stacktrace
32
+ @mechanism = mechanism
30
33
  end
31
34
 
32
35
  def to_hash
33
36
  data = super
34
37
  data[:stacktrace] = data[:stacktrace].to_hash if data[:stacktrace]
38
+ data[:mechanism] = data[:mechanism].to_hash
35
39
  data
36
40
  end
37
41
 
38
42
  # patch this method if you want to change an exception's stacktrace frames
39
43
  # also see `StacktraceBuilder.build`.
40
- def self.build_with_stacktrace(exception:, stacktrace_builder:)
44
+ def self.build_with_stacktrace(exception:, stacktrace_builder:, mechanism:)
41
45
  stacktrace = stacktrace_builder.build(backtrace: exception.backtrace)
42
46
 
43
47
  if locals = exception.instance_variable_get(:@sentry_locals)
@@ -50,7 +54,7 @@ module Sentry
50
54
  v = v.byteslice(0..MAX_LOCAL_BYTES - 1) + OMISSION_MARK
51
55
  end
52
56
 
53
- v
57
+ Utils::EncodingHelper.encode_to_utf_8(v)
54
58
  rescue StandardError
55
59
  PROBLEMATIC_LOCAL_VALUE_REPLACEMENT
56
60
  end
@@ -59,7 +63,7 @@ module Sentry
59
63
  stacktrace.frames.last.vars = locals
60
64
  end
61
65
 
62
- new(exception: exception, stacktrace: stacktrace)
66
+ new(exception: exception, stacktrace: stacktrace, mechanism: mechanism)
63
67
  end
64
68
  end
65
69
  end
@@ -62,6 +62,14 @@ module Sentry
62
62
  StacktraceInterface.new(frames: frames)
63
63
  end
64
64
 
65
+ # Get the code location hash for a single line for where metrics where added.
66
+ # @return [Hash]
67
+ def metrics_code_location(unparsed_line)
68
+ parsed_line = Backtrace::Line.parse(unparsed_line)
69
+ frame = convert_parsed_line_into_frame(parsed_line)
70
+ frame.to_hash.reject { |k, _| %i[project_root in_app].include?(k) }
71
+ end
72
+
65
73
  private
66
74
 
67
75
  def convert_parsed_line_into_frame(line)
@@ -0,0 +1,276 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sentry
4
+ module Metrics
5
+ class Aggregator
6
+ include LoggingHelper
7
+
8
+ FLUSH_INTERVAL = 5
9
+ ROLLUP_IN_SECONDS = 10
10
+
11
+ # this is how far removed from user code in the backtrace we are
12
+ # when we record code locations
13
+ DEFAULT_STACKLEVEL = 4
14
+
15
+ KEY_SANITIZATION_REGEX = /[^a-zA-Z0-9_\-.]+/
16
+ UNIT_SANITIZATION_REGEX = /[^a-zA-Z0-9_]+/
17
+ TAG_KEY_SANITIZATION_REGEX = /[^a-zA-Z0-9_\-.\/]+/
18
+
19
+ TAG_VALUE_SANITIZATION_MAP = {
20
+ "\n" => "\\n",
21
+ "\r" => "\\r",
22
+ "\t" => "\\t",
23
+ "\\" => "\\\\",
24
+ "|" => "\\u{7c}",
25
+ "," => "\\u{2c}"
26
+ }
27
+
28
+ METRIC_TYPES = {
29
+ c: CounterMetric,
30
+ d: DistributionMetric,
31
+ g: GaugeMetric,
32
+ s: SetMetric
33
+ }
34
+
35
+ # exposed only for testing
36
+ attr_reader :client, :thread, :buckets, :flush_shift, :code_locations
37
+
38
+ def initialize(configuration, client)
39
+ @client = client
40
+ @logger = configuration.logger
41
+ @before_emit = configuration.metrics.before_emit
42
+ @enable_code_locations = configuration.metrics.enable_code_locations
43
+ @stacktrace_builder = configuration.stacktrace_builder
44
+
45
+ @default_tags = {}
46
+ @default_tags['release'] = configuration.release if configuration.release
47
+ @default_tags['environment'] = configuration.environment if configuration.environment
48
+
49
+ @thread = nil
50
+ @exited = false
51
+ @mutex = Mutex.new
52
+
53
+ # a nested hash of timestamp -> bucket keys -> Metric instance
54
+ @buckets = {}
55
+
56
+ # the flush interval needs to be shifted once per startup to create jittering
57
+ @flush_shift = Random.rand * ROLLUP_IN_SECONDS
58
+
59
+ # a nested hash of timestamp (start of day) -> meta keys -> frame
60
+ @code_locations = {}
61
+ end
62
+
63
+ def add(type,
64
+ key,
65
+ value,
66
+ unit: 'none',
67
+ tags: {},
68
+ timestamp: nil,
69
+ stacklevel: nil)
70
+ return unless ensure_thread
71
+ return unless METRIC_TYPES.keys.include?(type)
72
+
73
+ updated_tags = get_updated_tags(tags)
74
+ return if @before_emit && !@before_emit.call(key, updated_tags)
75
+
76
+ timestamp ||= Sentry.utc_now
77
+
78
+ # this is integer division and thus takes the floor of the division
79
+ # and buckets into 10 second intervals
80
+ bucket_timestamp = (timestamp.to_i / ROLLUP_IN_SECONDS) * ROLLUP_IN_SECONDS
81
+
82
+ serialized_tags = serialize_tags(updated_tags)
83
+ bucket_key = [type, key, unit, serialized_tags]
84
+
85
+ added = @mutex.synchronize do
86
+ record_code_location(type, key, unit, timestamp, stacklevel: stacklevel) if @enable_code_locations
87
+ process_bucket(bucket_timestamp, bucket_key, type, value)
88
+ end
89
+
90
+ # for sets, we pass on if there was a new entry to the local gauge
91
+ local_value = type == :s ? added : value
92
+ process_span_aggregator(bucket_key, local_value)
93
+ end
94
+
95
+ def flush(force: false)
96
+ flushable_buckets = get_flushable_buckets!(force)
97
+ code_locations = get_code_locations!
98
+ return if flushable_buckets.empty? && code_locations.empty?
99
+
100
+ envelope = Envelope.new
101
+
102
+ unless flushable_buckets.empty?
103
+ payload = serialize_buckets(flushable_buckets)
104
+ envelope.add_item(
105
+ { type: 'statsd', length: payload.bytesize },
106
+ payload
107
+ )
108
+ end
109
+
110
+ unless code_locations.empty?
111
+ code_locations.each do |timestamp, locations|
112
+ payload = serialize_locations(timestamp, locations)
113
+ envelope.add_item(
114
+ { type: 'metric_meta', content_type: 'application/json' },
115
+ payload
116
+ )
117
+ end
118
+ end
119
+
120
+ @client.capture_envelope(envelope)
121
+ end
122
+
123
+ def kill
124
+ log_debug('[Metrics::Aggregator] killing thread')
125
+
126
+ @exited = true
127
+ @thread&.kill
128
+ end
129
+
130
+ private
131
+
132
+ def ensure_thread
133
+ return false if @exited
134
+ return true if @thread&.alive?
135
+
136
+ @thread = Thread.new do
137
+ loop do
138
+ # TODO-neel-metrics use event for force flush later
139
+ sleep(FLUSH_INTERVAL)
140
+ flush
141
+ end
142
+ end
143
+
144
+ true
145
+ rescue ThreadError
146
+ log_debug('[Metrics::Aggregator] thread creation failed')
147
+ @exited = true
148
+ false
149
+ end
150
+
151
+ # important to sort for key consistency
152
+ def serialize_tags(tags)
153
+ tags.flat_map do |k, v|
154
+ if v.is_a?(Array)
155
+ v.map { |x| [k.to_s, x.to_s] }
156
+ else
157
+ [[k.to_s, v.to_s]]
158
+ end
159
+ end.sort
160
+ end
161
+
162
+ def get_flushable_buckets!(force)
163
+ @mutex.synchronize do
164
+ flushable_buckets = {}
165
+
166
+ if force
167
+ flushable_buckets = @buckets
168
+ @buckets = {}
169
+ else
170
+ cutoff = Sentry.utc_now.to_i - ROLLUP_IN_SECONDS - @flush_shift
171
+ flushable_buckets = @buckets.select { |k, _| k <= cutoff }
172
+ @buckets.reject! { |k, _| k <= cutoff }
173
+ end
174
+
175
+ flushable_buckets
176
+ end
177
+ end
178
+
179
+ def get_code_locations!
180
+ @mutex.synchronize do
181
+ code_locations = @code_locations
182
+ @code_locations = {}
183
+ code_locations
184
+ end
185
+ end
186
+
187
+ # serialize buckets to statsd format
188
+ def serialize_buckets(buckets)
189
+ buckets.map do |timestamp, timestamp_buckets|
190
+ timestamp_buckets.map do |metric_key, metric|
191
+ type, key, unit, tags = metric_key
192
+ values = metric.serialize.join(':')
193
+ sanitized_tags = tags.map { |k, v| "#{sanitize_tag_key(k)}:#{sanitize_tag_value(v)}" }.join(',')
194
+
195
+ "#{sanitize_key(key)}@#{sanitize_unit(unit)}:#{values}|#{type}|\##{sanitized_tags}|T#{timestamp}"
196
+ end
197
+ end.flatten.join("\n")
198
+ end
199
+
200
+ def serialize_locations(timestamp, locations)
201
+ mapping = locations.map do |meta_key, location|
202
+ type, key, unit = meta_key
203
+ mri = "#{type}:#{sanitize_key(key)}@#{sanitize_unit(unit)}"
204
+
205
+ # note this needs to be an array but it really doesn't serve a purpose right now
206
+ [mri, [location.merge(type: 'location')]]
207
+ end.to_h
208
+
209
+ { timestamp: timestamp, mapping: mapping }
210
+ end
211
+
212
+ def sanitize_key(key)
213
+ key.gsub(KEY_SANITIZATION_REGEX, '_')
214
+ end
215
+
216
+ def sanitize_unit(unit)
217
+ unit.gsub(UNIT_SANITIZATION_REGEX, '')
218
+ end
219
+
220
+ def sanitize_tag_key(key)
221
+ key.gsub(TAG_KEY_SANITIZATION_REGEX, '')
222
+ end
223
+
224
+ def sanitize_tag_value(value)
225
+ value.chars.map { |c| TAG_VALUE_SANITIZATION_MAP[c] || c }.join
226
+ end
227
+
228
+ def get_transaction_name
229
+ scope = Sentry.get_current_scope
230
+ return nil unless scope && scope.transaction_name
231
+ return nil if scope.transaction_source_low_quality?
232
+
233
+ scope.transaction_name
234
+ end
235
+
236
+ def get_updated_tags(tags)
237
+ updated_tags = @default_tags.merge(tags)
238
+
239
+ transaction_name = get_transaction_name
240
+ updated_tags['transaction'] = transaction_name if transaction_name
241
+
242
+ updated_tags
243
+ end
244
+
245
+ def process_span_aggregator(key, value)
246
+ scope = Sentry.get_current_scope
247
+ return nil unless scope && scope.span
248
+ return nil if scope.transaction_source_low_quality?
249
+
250
+ scope.span.metrics_local_aggregator.add(key, value)
251
+ end
252
+
253
+ def process_bucket(timestamp, key, type, value)
254
+ @buckets[timestamp] ||= {}
255
+
256
+ if (metric = @buckets[timestamp][key])
257
+ old_weight = metric.weight
258
+ metric.add(value)
259
+ metric.weight - old_weight
260
+ else
261
+ metric = METRIC_TYPES[type].new(value)
262
+ @buckets[timestamp][key] = metric
263
+ metric.weight
264
+ end
265
+ end
266
+
267
+ def record_code_location(type, key, unit, timestamp, stacklevel: nil)
268
+ meta_key = [type, key, unit]
269
+ start_of_day = Time.utc(timestamp.year, timestamp.month, timestamp.day).to_i
270
+
271
+ @code_locations[start_of_day] ||= {}
272
+ @code_locations[start_of_day][meta_key] ||= @stacktrace_builder.metrics_code_location(caller[stacklevel || DEFAULT_STACKLEVEL])
273
+ end
274
+ end
275
+ end
276
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sentry
4
+ module Metrics
5
+ class Configuration
6
+ include ArgumentCheckingHelper
7
+
8
+ # Enable metrics usage.
9
+ # Starts a new {Sentry::Metrics::Aggregator} instance to aggregate metrics
10
+ # and a thread to aggregate flush every 5 seconds.
11
+ # @return [Boolean]
12
+ attr_accessor :enabled
13
+
14
+ # Enable code location reporting.
15
+ # Will be sent once per day.
16
+ # True by default.
17
+ # @return [Boolean]
18
+ attr_accessor :enable_code_locations
19
+
20
+ # Optional Proc, called before emitting a metric to the aggregator.
21
+ # Use it to filter keys (return false/nil) or update tags.
22
+ # Make sure to return true at the end.
23
+ #
24
+ # @example
25
+ # config.metrics.before_emit = lambda do |key, tags|
26
+ # return nil if key == 'foo'
27
+ # tags[:bar] = 42
28
+ # tags.delete(:baz)
29
+ # true
30
+ # end
31
+ #
32
+ # @return [Proc, nil]
33
+ attr_reader :before_emit
34
+
35
+ def initialize
36
+ @enabled = false
37
+ @enable_code_locations = true
38
+ end
39
+
40
+ def before_emit=(value)
41
+ check_callable!("metrics.before_emit", value)
42
+
43
+ @before_emit = value
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sentry
4
+ module Metrics
5
+ class CounterMetric < Metric
6
+ attr_reader :value
7
+
8
+ def initialize(value)
9
+ @value = value.to_f
10
+ end
11
+
12
+ def add(value)
13
+ @value += value.to_f
14
+ end
15
+
16
+ def serialize
17
+ [value]
18
+ end
19
+
20
+ def weight
21
+ 1
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sentry
4
+ module Metrics
5
+ class DistributionMetric < Metric
6
+ attr_reader :value
7
+
8
+ def initialize(value)
9
+ @value = [value.to_f]
10
+ end
11
+
12
+ def add(value)
13
+ @value << value.to_f
14
+ end
15
+
16
+ def serialize
17
+ value
18
+ end
19
+
20
+ def weight
21
+ value.size
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sentry
4
+ module Metrics
5
+ class GaugeMetric < Metric
6
+ attr_reader :last, :min, :max, :sum, :count
7
+
8
+ def initialize(value)
9
+ value = value.to_f
10
+ @last = value
11
+ @min = value
12
+ @max = value
13
+ @sum = value
14
+ @count = 1
15
+ end
16
+
17
+ def add(value)
18
+ value = value.to_f
19
+ @last = value
20
+ @min = [@min, value].min
21
+ @max = [@max, value].max
22
+ @sum += value
23
+ @count += 1
24
+ end
25
+
26
+ def serialize
27
+ [last, min, max, sum, count]
28
+ end
29
+
30
+ def weight
31
+ 5
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sentry
4
+ module Metrics
5
+ class LocalAggregator
6
+ # exposed only for testing
7
+ attr_reader :buckets
8
+
9
+ def initialize
10
+ @buckets = {}
11
+ end
12
+
13
+ def add(key, value)
14
+ if @buckets[key]
15
+ @buckets[key].add(value)
16
+ else
17
+ @buckets[key] = GaugeMetric.new(value)
18
+ end
19
+ end
20
+
21
+ def to_hash
22
+ return nil if @buckets.empty?
23
+
24
+ @buckets.map do |bucket_key, metric|
25
+ type, key, unit, tags = bucket_key
26
+
27
+ payload_key = "#{type}:#{key}@#{unit}"
28
+ payload_value = {
29
+ tags: deserialize_tags(tags),
30
+ min: metric.min,
31
+ max: metric.max,
32
+ count: metric.count,
33
+ sum: metric.sum
34
+ }
35
+
36
+ [payload_key, payload_value]
37
+ end.to_h
38
+ end
39
+
40
+ private
41
+
42
+ def deserialize_tags(tags)
43
+ tags.inject({}) do |h, tag|
44
+ k, v = tag
45
+ old = h[k]
46
+ # make it an array if key repeats
47
+ h[k] = old ? (old.is_a?(Array) ? old << v : [old, v]) : v
48
+ h
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sentry
4
+ module Metrics
5
+ class Metric
6
+ def add(value)
7
+ raise NotImplementedError
8
+ end
9
+
10
+ def serialize
11
+ raise NotImplementedError
12
+ end
13
+
14
+ def weight
15
+ raise NotImplementedError
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'set'
4
+ require 'zlib'
5
+
6
+ module Sentry
7
+ module Metrics
8
+ class SetMetric < Metric
9
+ attr_reader :value
10
+
11
+ def initialize(value)
12
+ @value = Set[value]
13
+ end
14
+
15
+ def add(value)
16
+ @value << value
17
+ end
18
+
19
+ def serialize
20
+ value.map { |x| x.is_a?(String) ? Zlib.crc32(x) : x.to_i }
21
+ end
22
+
23
+ def weight
24
+ value.size
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sentry
4
+ module Metrics
5
+ module Timing
6
+ class << self
7
+ def nanosecond
8
+ time = Sentry.utc_now
9
+ time.to_i * (10 ** 9) + time.nsec
10
+ end
11
+
12
+ def microsecond
13
+ time = Sentry.utc_now
14
+ time.to_i * (10 ** 6) + time.usec
15
+ end
16
+
17
+ def millisecond
18
+ Sentry.utc_now.to_i * (10 ** 3)
19
+ end
20
+
21
+ def second
22
+ Sentry.utc_now.to_i
23
+ end
24
+
25
+ def minute
26
+ Sentry.utc_now.to_i / 60.0
27
+ end
28
+
29
+ def hour
30
+ Sentry.utc_now.to_i / 3600.0
31
+ end
32
+
33
+ def day
34
+ Sentry.utc_now.to_i / (3600.0 * 24.0)
35
+ end
36
+
37
+ def week
38
+ Sentry.utc_now.to_i / (3600.0 * 24.0 * 7.0)
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end