sentry-ruby 5.16.1 → 5.20.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +3 -0
  3. data/README.md +20 -10
  4. data/Rakefile +1 -1
  5. data/bin/console +1 -0
  6. data/lib/sentry/attachment.rb +40 -0
  7. data/lib/sentry/background_worker.rb +1 -1
  8. data/lib/sentry/backpressure_monitor.rb +2 -32
  9. data/lib/sentry/backtrace.rb +8 -6
  10. data/lib/sentry/baggage.rb +6 -6
  11. data/lib/sentry/breadcrumb/sentry_logger.rb +6 -6
  12. data/lib/sentry/check_in_event.rb +5 -5
  13. data/lib/sentry/client.rb +61 -11
  14. data/lib/sentry/configuration.rb +53 -25
  15. data/lib/sentry/core_ext/object/deep_dup.rb +1 -1
  16. data/lib/sentry/cron/monitor_check_ins.rb +1 -1
  17. data/lib/sentry/cron/monitor_config.rb +1 -1
  18. data/lib/sentry/cron/monitor_schedule.rb +1 -1
  19. data/lib/sentry/dsn.rb +4 -4
  20. data/lib/sentry/envelope.rb +19 -2
  21. data/lib/sentry/error_event.rb +2 -2
  22. data/lib/sentry/event.rb +20 -18
  23. data/lib/sentry/faraday.rb +77 -0
  24. data/lib/sentry/graphql.rb +9 -0
  25. data/lib/sentry/hub.rb +23 -3
  26. data/lib/sentry/integrable.rb +4 -0
  27. data/lib/sentry/interface.rb +1 -0
  28. data/lib/sentry/interfaces/exception.rb +5 -3
  29. data/lib/sentry/interfaces/mechanism.rb +20 -0
  30. data/lib/sentry/interfaces/request.rb +7 -7
  31. data/lib/sentry/interfaces/single_exception.rb +7 -5
  32. data/lib/sentry/interfaces/stacktrace.rb +3 -1
  33. data/lib/sentry/interfaces/stacktrace_builder.rb +23 -2
  34. data/lib/sentry/logger.rb +1 -1
  35. data/lib/sentry/metrics/aggregator.rb +248 -0
  36. data/lib/sentry/metrics/configuration.rb +47 -0
  37. data/lib/sentry/metrics/counter_metric.rb +25 -0
  38. data/lib/sentry/metrics/distribution_metric.rb +25 -0
  39. data/lib/sentry/metrics/gauge_metric.rb +35 -0
  40. data/lib/sentry/metrics/local_aggregator.rb +53 -0
  41. data/lib/sentry/metrics/metric.rb +19 -0
  42. data/lib/sentry/metrics/set_metric.rb +28 -0
  43. data/lib/sentry/metrics/timing.rb +43 -0
  44. data/lib/sentry/metrics.rb +56 -0
  45. data/lib/sentry/net/http.rb +18 -39
  46. data/lib/sentry/profiler.rb +19 -20
  47. data/lib/sentry/propagation_context.rb +10 -9
  48. data/lib/sentry/puma.rb +1 -1
  49. data/lib/sentry/rack/capture_exceptions.rb +15 -3
  50. data/lib/sentry/rack.rb +2 -2
  51. data/lib/sentry/rake.rb +4 -2
  52. data/lib/sentry/redis.rb +2 -1
  53. data/lib/sentry/release_detector.rb +4 -4
  54. data/lib/sentry/scope.rb +36 -26
  55. data/lib/sentry/session.rb +2 -2
  56. data/lib/sentry/session_flusher.rb +7 -39
  57. data/lib/sentry/span.rb +46 -5
  58. data/lib/sentry/test_helper.rb +3 -2
  59. data/lib/sentry/threaded_periodic_worker.rb +39 -0
  60. data/lib/sentry/transaction.rb +17 -15
  61. data/lib/sentry/transaction_event.rb +6 -1
  62. data/lib/sentry/transport/configuration.rb +0 -1
  63. data/lib/sentry/transport/http_transport.rb +12 -12
  64. data/lib/sentry/transport.rb +18 -26
  65. data/lib/sentry/utils/argument_checking_helper.rb +6 -0
  66. data/lib/sentry/utils/env_helper.rb +21 -0
  67. data/lib/sentry/utils/http_tracing.rb +41 -0
  68. data/lib/sentry/utils/logging_helper.rb +0 -4
  69. data/lib/sentry/utils/real_ip.rb +2 -2
  70. data/lib/sentry/utils/request_id.rb +1 -1
  71. data/lib/sentry/version.rb +1 -1
  72. data/lib/sentry-ruby.rb +34 -3
  73. data/sentry-ruby-core.gemspec +1 -1
  74. data/sentry-ruby.gemspec +13 -6
  75. metadata +40 -7
@@ -11,10 +11,10 @@ module Sentry
11
11
  OMISSION_MARK = "...".freeze
12
12
  MAX_LOCAL_BYTES = 1024
13
13
 
14
- attr_reader :type, :module, :thread_id, :stacktrace
14
+ attr_reader :type, :module, :thread_id, :stacktrace, :mechanism
15
15
  attr_accessor :value
16
16
 
17
- def initialize(exception:, stacktrace: nil)
17
+ def initialize(exception:, mechanism:, stacktrace: nil)
18
18
  @type = exception.class.to_s
19
19
  exception_message =
20
20
  if exception.respond_to?(:detailed_message)
@@ -26,20 +26,22 @@ module Sentry
26
26
 
27
27
  @value = Utils::EncodingHelper.encode_to_utf_8(exception_message.byteslice(0..Event::MAX_MESSAGE_SIZE_IN_BYTES))
28
28
 
29
- @module = exception.class.to_s.split('::')[0...-1].join('::')
29
+ @module = exception.class.to_s.split("::")[0...-1].join("::")
30
30
  @thread_id = Thread.current.object_id
31
31
  @stacktrace = stacktrace
32
+ @mechanism = mechanism
32
33
  end
33
34
 
34
35
  def to_hash
35
36
  data = super
36
37
  data[:stacktrace] = data[:stacktrace].to_hash if data[:stacktrace]
38
+ data[:mechanism] = data[:mechanism].to_hash
37
39
  data
38
40
  end
39
41
 
40
42
  # patch this method if you want to change an exception's stacktrace frames
41
43
  # also see `StacktraceBuilder.build`.
42
- def self.build_with_stacktrace(exception:, stacktrace_builder:)
44
+ def self.build_with_stacktrace(exception:, stacktrace_builder:, mechanism:)
43
45
  stacktrace = stacktrace_builder.build(backtrace: exception.backtrace)
44
46
 
45
47
  if locals = exception.instance_variable_get(:@sentry_locals)
@@ -61,7 +63,7 @@ module Sentry
61
63
  stacktrace.frames.last.vars = locals
62
64
  end
63
65
 
64
- new(exception: exception, stacktrace: stacktrace)
66
+ new(exception: exception, stacktrace: stacktrace, mechanism: mechanism)
65
67
  end
66
68
  end
67
69
  end
@@ -27,8 +27,9 @@ module Sentry
27
27
  attr_accessor :abs_path, :context_line, :function, :in_app, :filename,
28
28
  :lineno, :module, :pre_context, :post_context, :vars
29
29
 
30
- def initialize(project_root, line)
30
+ def initialize(project_root, line, strip_backtrace_load_path = true)
31
31
  @project_root = project_root
32
+ @strip_backtrace_load_path = strip_backtrace_load_path
32
33
 
33
34
  @abs_path = line.file
34
35
  @function = line.method if line.method
@@ -44,6 +45,7 @@ module Sentry
44
45
 
45
46
  def compute_filename
46
47
  return if abs_path.nil?
48
+ return abs_path unless @strip_backtrace_load_path
47
49
 
48
50
  prefix =
49
51
  if under_project_root? && in_app
@@ -17,22 +17,35 @@ module Sentry
17
17
  # @return [Proc, nil]
18
18
  attr_reader :backtrace_cleanup_callback
19
19
 
20
+ # @return [Boolean]
21
+ attr_reader :strip_backtrace_load_path
22
+
20
23
  # @param project_root [String]
21
24
  # @param app_dirs_pattern [Regexp, nil]
22
25
  # @param linecache [LineCache]
23
26
  # @param context_lines [Integer, nil]
24
27
  # @param backtrace_cleanup_callback [Proc, nil]
28
+ # @param strip_backtrace_load_path [Boolean]
25
29
  # @see Configuration#project_root
26
30
  # @see Configuration#app_dirs_pattern
27
31
  # @see Configuration#linecache
28
32
  # @see Configuration#context_lines
29
33
  # @see Configuration#backtrace_cleanup_callback
30
- def initialize(project_root:, app_dirs_pattern:, linecache:, context_lines:, backtrace_cleanup_callback: nil)
34
+ # @see Configuration#strip_backtrace_load_path
35
+ def initialize(
36
+ project_root:,
37
+ app_dirs_pattern:,
38
+ linecache:,
39
+ context_lines:,
40
+ backtrace_cleanup_callback: nil,
41
+ strip_backtrace_load_path: true
42
+ )
31
43
  @project_root = project_root
32
44
  @app_dirs_pattern = app_dirs_pattern
33
45
  @linecache = linecache
34
46
  @context_lines = context_lines
35
47
  @backtrace_cleanup_callback = backtrace_cleanup_callback
48
+ @strip_backtrace_load_path = strip_backtrace_load_path
36
49
  end
37
50
 
38
51
  # Generates a StacktraceInterface with the given backtrace.
@@ -62,10 +75,18 @@ module Sentry
62
75
  StacktraceInterface.new(frames: frames)
63
76
  end
64
77
 
78
+ # Get the code location hash for a single line for where metrics where added.
79
+ # @return [Hash]
80
+ def metrics_code_location(unparsed_line)
81
+ parsed_line = Backtrace::Line.parse(unparsed_line)
82
+ frame = convert_parsed_line_into_frame(parsed_line)
83
+ frame.to_hash.reject { |k, _| %i[project_root in_app].include?(k) }
84
+ end
85
+
65
86
  private
66
87
 
67
88
  def convert_parsed_line_into_frame(line)
68
- frame = StacktraceInterface::Frame.new(project_root, line)
89
+ frame = StacktraceInterface::Frame.new(project_root, line, strip_backtrace_load_path)
69
90
  frame.set_context(linecache, context_lines) if context_lines
70
91
  frame
71
92
  end
data/lib/sentry/logger.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'logger'
3
+ require "logger"
4
4
 
5
5
  module Sentry
6
6
  class Logger < ::Logger
@@ -0,0 +1,248 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sentry
4
+ module Metrics
5
+ class Aggregator < ThreadedPeriodicWorker
6
+ FLUSH_INTERVAL = 5
7
+ ROLLUP_IN_SECONDS = 10
8
+
9
+ # this is how far removed from user code in the backtrace we are
10
+ # when we record code locations
11
+ DEFAULT_STACKLEVEL = 4
12
+
13
+ KEY_SANITIZATION_REGEX = /[^a-zA-Z0-9_\-.]+/
14
+ UNIT_SANITIZATION_REGEX = /[^a-zA-Z0-9_]+/
15
+ TAG_KEY_SANITIZATION_REGEX = /[^a-zA-Z0-9_\-.\/]+/
16
+
17
+ TAG_VALUE_SANITIZATION_MAP = {
18
+ "\n" => "\\n",
19
+ "\r" => "\\r",
20
+ "\t" => "\\t",
21
+ "\\" => "\\\\",
22
+ "|" => "\\u{7c}",
23
+ "," => "\\u{2c}"
24
+ }
25
+
26
+ METRIC_TYPES = {
27
+ c: CounterMetric,
28
+ d: DistributionMetric,
29
+ g: GaugeMetric,
30
+ s: SetMetric
31
+ }
32
+
33
+ # exposed only for testing
34
+ attr_reader :client, :thread, :buckets, :flush_shift, :code_locations
35
+
36
+ def initialize(configuration, client)
37
+ super(configuration.logger, FLUSH_INTERVAL)
38
+ @client = client
39
+ @before_emit = configuration.metrics.before_emit
40
+ @enable_code_locations = configuration.metrics.enable_code_locations
41
+ @stacktrace_builder = configuration.stacktrace_builder
42
+
43
+ @default_tags = {}
44
+ @default_tags["release"] = configuration.release if configuration.release
45
+ @default_tags["environment"] = configuration.environment if configuration.environment
46
+
47
+ @mutex = Mutex.new
48
+
49
+ # a nested hash of timestamp -> bucket keys -> Metric instance
50
+ @buckets = {}
51
+
52
+ # the flush interval needs to be shifted once per startup to create jittering
53
+ @flush_shift = Random.rand * ROLLUP_IN_SECONDS
54
+
55
+ # a nested hash of timestamp (start of day) -> meta keys -> frame
56
+ @code_locations = {}
57
+ end
58
+
59
+ def add(type,
60
+ key,
61
+ value,
62
+ unit: "none",
63
+ tags: {},
64
+ timestamp: nil,
65
+ stacklevel: nil)
66
+ return unless ensure_thread
67
+ return unless METRIC_TYPES.keys.include?(type)
68
+
69
+ updated_tags = get_updated_tags(tags)
70
+ return if @before_emit && !@before_emit.call(key, updated_tags)
71
+
72
+ timestamp ||= Sentry.utc_now
73
+
74
+ # this is integer division and thus takes the floor of the division
75
+ # and buckets into 10 second intervals
76
+ bucket_timestamp = (timestamp.to_i / ROLLUP_IN_SECONDS) * ROLLUP_IN_SECONDS
77
+
78
+ serialized_tags = serialize_tags(updated_tags)
79
+ bucket_key = [type, key, unit, serialized_tags]
80
+
81
+ added = @mutex.synchronize do
82
+ record_code_location(type, key, unit, timestamp, stacklevel: stacklevel) if @enable_code_locations
83
+ process_bucket(bucket_timestamp, bucket_key, type, value)
84
+ end
85
+
86
+ # for sets, we pass on if there was a new entry to the local gauge
87
+ local_value = type == :s ? added : value
88
+ process_span_aggregator(bucket_key, local_value)
89
+ end
90
+
91
+ def flush(force: false)
92
+ flushable_buckets = get_flushable_buckets!(force)
93
+ code_locations = get_code_locations!
94
+ return if flushable_buckets.empty? && code_locations.empty?
95
+
96
+ envelope = Envelope.new
97
+
98
+ unless flushable_buckets.empty?
99
+ payload = serialize_buckets(flushable_buckets)
100
+ envelope.add_item(
101
+ { type: "statsd", length: payload.bytesize },
102
+ payload
103
+ )
104
+ end
105
+
106
+ unless code_locations.empty?
107
+ code_locations.each do |timestamp, locations|
108
+ payload = serialize_locations(timestamp, locations)
109
+ envelope.add_item(
110
+ { type: "metric_meta", content_type: "application/json" },
111
+ payload
112
+ )
113
+ end
114
+ end
115
+
116
+ @client.capture_envelope(envelope)
117
+ end
118
+
119
+ alias_method :run, :flush
120
+
121
+ private
122
+
123
+ # important to sort for key consistency
124
+ def serialize_tags(tags)
125
+ tags.flat_map do |k, v|
126
+ if v.is_a?(Array)
127
+ v.map { |x| [k.to_s, x.to_s] }
128
+ else
129
+ [[k.to_s, v.to_s]]
130
+ end
131
+ end.sort
132
+ end
133
+
134
+ def get_flushable_buckets!(force)
135
+ @mutex.synchronize do
136
+ flushable_buckets = {}
137
+
138
+ if force
139
+ flushable_buckets = @buckets
140
+ @buckets = {}
141
+ else
142
+ cutoff = Sentry.utc_now.to_i - ROLLUP_IN_SECONDS - @flush_shift
143
+ flushable_buckets = @buckets.select { |k, _| k <= cutoff }
144
+ @buckets.reject! { |k, _| k <= cutoff }
145
+ end
146
+
147
+ flushable_buckets
148
+ end
149
+ end
150
+
151
+ def get_code_locations!
152
+ @mutex.synchronize do
153
+ code_locations = @code_locations
154
+ @code_locations = {}
155
+ code_locations
156
+ end
157
+ end
158
+
159
+ # serialize buckets to statsd format
160
+ def serialize_buckets(buckets)
161
+ buckets.map do |timestamp, timestamp_buckets|
162
+ timestamp_buckets.map do |metric_key, metric|
163
+ type, key, unit, tags = metric_key
164
+ values = metric.serialize.join(":")
165
+ sanitized_tags = tags.map { |k, v| "#{sanitize_tag_key(k)}:#{sanitize_tag_value(v)}" }.join(",")
166
+
167
+ "#{sanitize_key(key)}@#{sanitize_unit(unit)}:#{values}|#{type}|\##{sanitized_tags}|T#{timestamp}"
168
+ end
169
+ end.flatten.join("\n")
170
+ end
171
+
172
+ def serialize_locations(timestamp, locations)
173
+ mapping = locations.map do |meta_key, location|
174
+ type, key, unit = meta_key
175
+ mri = "#{type}:#{sanitize_key(key)}@#{sanitize_unit(unit)}"
176
+
177
+ # note this needs to be an array but it really doesn't serve a purpose right now
178
+ [mri, [location.merge(type: "location")]]
179
+ end.to_h
180
+
181
+ { timestamp: timestamp, mapping: mapping }
182
+ end
183
+
184
+ def sanitize_key(key)
185
+ key.gsub(KEY_SANITIZATION_REGEX, "_")
186
+ end
187
+
188
+ def sanitize_unit(unit)
189
+ unit.gsub(UNIT_SANITIZATION_REGEX, "")
190
+ end
191
+
192
+ def sanitize_tag_key(key)
193
+ key.gsub(TAG_KEY_SANITIZATION_REGEX, "")
194
+ end
195
+
196
+ def sanitize_tag_value(value)
197
+ value.chars.map { |c| TAG_VALUE_SANITIZATION_MAP[c] || c }.join
198
+ end
199
+
200
+ def get_transaction_name
201
+ scope = Sentry.get_current_scope
202
+ return nil unless scope && scope.transaction_name
203
+ return nil if scope.transaction_source_low_quality?
204
+
205
+ scope.transaction_name
206
+ end
207
+
208
+ def get_updated_tags(tags)
209
+ updated_tags = @default_tags.merge(tags)
210
+
211
+ transaction_name = get_transaction_name
212
+ updated_tags["transaction"] = transaction_name if transaction_name
213
+
214
+ updated_tags
215
+ end
216
+
217
+ def process_span_aggregator(key, value)
218
+ scope = Sentry.get_current_scope
219
+ return nil unless scope && scope.span
220
+ return nil if scope.transaction_source_low_quality?
221
+
222
+ scope.span.metrics_local_aggregator.add(key, value)
223
+ end
224
+
225
+ def process_bucket(timestamp, key, type, value)
226
+ @buckets[timestamp] ||= {}
227
+
228
+ if (metric = @buckets[timestamp][key])
229
+ old_weight = metric.weight
230
+ metric.add(value)
231
+ metric.weight - old_weight
232
+ else
233
+ metric = METRIC_TYPES[type].new(value)
234
+ @buckets[timestamp][key] = metric
235
+ metric.weight
236
+ end
237
+ end
238
+
239
+ def record_code_location(type, key, unit, timestamp, stacklevel: nil)
240
+ meta_key = [type, key, unit]
241
+ start_of_day = Time.utc(timestamp.year, timestamp.month, timestamp.day).to_i
242
+
243
+ @code_locations[start_of_day] ||= {}
244
+ @code_locations[start_of_day][meta_key] ||= @stacktrace_builder.metrics_code_location(caller[stacklevel || DEFAULT_STACKLEVEL])
245
+ end
246
+ end
247
+ end
248
+ 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