sentry-ruby 5.13.0 → 5.21.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +7 -18
- data/README.md +20 -10
- data/Rakefile +3 -1
- data/bin/console +2 -0
- data/lib/sentry/attachment.rb +40 -0
- data/lib/sentry/background_worker.rb +9 -2
- data/lib/sentry/backpressure_monitor.rb +45 -0
- data/lib/sentry/backtrace.rb +10 -8
- data/lib/sentry/baggage.rb +7 -7
- data/lib/sentry/breadcrumb/sentry_logger.rb +6 -6
- data/lib/sentry/check_in_event.rb +5 -5
- data/lib/sentry/client.rb +71 -18
- data/lib/sentry/configuration.rb +108 -32
- data/lib/sentry/core_ext/object/deep_dup.rb +1 -1
- data/lib/sentry/cron/configuration.rb +23 -0
- data/lib/sentry/cron/monitor_check_ins.rb +42 -26
- data/lib/sentry/cron/monitor_config.rb +1 -1
- data/lib/sentry/cron/monitor_schedule.rb +1 -1
- data/lib/sentry/dsn.rb +4 -4
- data/lib/sentry/envelope/item.rb +88 -0
- data/lib/sentry/envelope.rb +2 -68
- data/lib/sentry/error_event.rb +2 -2
- data/lib/sentry/event.rb +20 -46
- data/lib/sentry/faraday.rb +77 -0
- data/lib/sentry/graphql.rb +9 -0
- data/lib/sentry/hub.rb +25 -5
- data/lib/sentry/integrable.rb +4 -0
- data/lib/sentry/interface.rb +1 -0
- data/lib/sentry/interfaces/exception.rb +5 -3
- data/lib/sentry/interfaces/mechanism.rb +20 -0
- data/lib/sentry/interfaces/request.rb +7 -7
- data/lib/sentry/interfaces/single_exception.rb +10 -7
- data/lib/sentry/interfaces/stacktrace.rb +3 -1
- data/lib/sentry/interfaces/stacktrace_builder.rb +23 -2
- data/lib/sentry/logger.rb +1 -1
- data/lib/sentry/metrics/aggregator.rb +248 -0
- data/lib/sentry/metrics/configuration.rb +47 -0
- data/lib/sentry/metrics/counter_metric.rb +25 -0
- data/lib/sentry/metrics/distribution_metric.rb +25 -0
- data/lib/sentry/metrics/gauge_metric.rb +35 -0
- data/lib/sentry/metrics/local_aggregator.rb +53 -0
- data/lib/sentry/metrics/metric.rb +19 -0
- data/lib/sentry/metrics/set_metric.rb +28 -0
- data/lib/sentry/metrics/timing.rb +43 -0
- data/lib/sentry/metrics.rb +56 -0
- data/lib/sentry/net/http.rb +22 -39
- data/lib/sentry/profiler/helpers.rb +46 -0
- data/lib/sentry/profiler.rb +25 -56
- data/lib/sentry/propagation_context.rb +10 -9
- data/lib/sentry/puma.rb +1 -1
- data/lib/sentry/rack/capture_exceptions.rb +16 -4
- data/lib/sentry/rack.rb +2 -2
- data/lib/sentry/rake.rb +4 -15
- data/lib/sentry/redis.rb +2 -1
- data/lib/sentry/release_detector.rb +5 -5
- data/lib/sentry/scope.rb +48 -37
- data/lib/sentry/session.rb +2 -2
- data/lib/sentry/session_flusher.rb +7 -39
- data/lib/sentry/span.rb +46 -5
- data/lib/sentry/test_helper.rb +5 -2
- data/lib/sentry/threaded_periodic_worker.rb +39 -0
- data/lib/sentry/transaction.rb +27 -18
- data/lib/sentry/transaction_event.rb +6 -2
- data/lib/sentry/transport/configuration.rb +73 -1
- data/lib/sentry/transport/http_transport.rb +72 -41
- data/lib/sentry/transport/spotlight_transport.rb +50 -0
- data/lib/sentry/transport.rb +36 -41
- data/lib/sentry/utils/argument_checking_helper.rb +6 -0
- data/lib/sentry/utils/env_helper.rb +21 -0
- data/lib/sentry/utils/http_tracing.rb +41 -0
- data/lib/sentry/utils/logging_helper.rb +0 -4
- data/lib/sentry/utils/real_ip.rb +2 -2
- data/lib/sentry/utils/request_id.rb +1 -1
- data/lib/sentry/vernier/output.rb +89 -0
- data/lib/sentry/vernier/profiler.rb +125 -0
- data/lib/sentry/version.rb +1 -1
- data/lib/sentry-ruby.rb +61 -27
- data/sentry-ruby-core.gemspec +3 -1
- data/sentry-ruby.gemspec +15 -6
- metadata +47 -7
@@ -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
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "sentry/metrics/metric"
|
4
|
+
require "sentry/metrics/counter_metric"
|
5
|
+
require "sentry/metrics/distribution_metric"
|
6
|
+
require "sentry/metrics/gauge_metric"
|
7
|
+
require "sentry/metrics/set_metric"
|
8
|
+
require "sentry/metrics/timing"
|
9
|
+
require "sentry/metrics/aggregator"
|
10
|
+
|
11
|
+
module Sentry
|
12
|
+
module Metrics
|
13
|
+
DURATION_UNITS = %w[nanosecond microsecond millisecond second minute hour day week]
|
14
|
+
INFORMATION_UNITS = %w[bit byte kilobyte kibibyte megabyte mebibyte gigabyte gibibyte terabyte tebibyte petabyte pebibyte exabyte exbibyte]
|
15
|
+
FRACTIONAL_UNITS = %w[ratio percent]
|
16
|
+
|
17
|
+
OP_NAME = "metric.timing"
|
18
|
+
SPAN_ORIGIN = "auto.metric.timing"
|
19
|
+
|
20
|
+
class << self
|
21
|
+
def increment(key, value = 1.0, unit: "none", tags: {}, timestamp: nil)
|
22
|
+
Sentry.metrics_aggregator&.add(:c, key, value, unit: unit, tags: tags, timestamp: timestamp)
|
23
|
+
end
|
24
|
+
|
25
|
+
def distribution(key, value, unit: "none", tags: {}, timestamp: nil)
|
26
|
+
Sentry.metrics_aggregator&.add(:d, key, value, unit: unit, tags: tags, timestamp: timestamp)
|
27
|
+
end
|
28
|
+
|
29
|
+
def set(key, value, unit: "none", tags: {}, timestamp: nil)
|
30
|
+
Sentry.metrics_aggregator&.add(:s, key, value, unit: unit, tags: tags, timestamp: timestamp)
|
31
|
+
end
|
32
|
+
|
33
|
+
def gauge(key, value, unit: "none", tags: {}, timestamp: nil)
|
34
|
+
Sentry.metrics_aggregator&.add(:g, key, value, unit: unit, tags: tags, timestamp: timestamp)
|
35
|
+
end
|
36
|
+
|
37
|
+
def timing(key, unit: "second", tags: {}, timestamp: nil, &block)
|
38
|
+
return unless block_given?
|
39
|
+
return yield unless DURATION_UNITS.include?(unit)
|
40
|
+
|
41
|
+
result, value = Sentry.with_child_span(op: OP_NAME, description: key, origin: SPAN_ORIGIN) do |span|
|
42
|
+
tags.each { |k, v| span.set_tag(k, v.is_a?(Array) ? v.join(", ") : v.to_s) } if span
|
43
|
+
|
44
|
+
start = Timing.send(unit.to_sym)
|
45
|
+
result = yield
|
46
|
+
value = Timing.send(unit.to_sym) - start
|
47
|
+
|
48
|
+
[result, value]
|
49
|
+
end
|
50
|
+
|
51
|
+
Sentry.metrics_aggregator&.add(:d, key, value, unit: unit, tags: tags, timestamp: timestamp)
|
52
|
+
result
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/lib/sentry/net/http.rb
CHANGED
@@ -1,12 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "net/http"
|
4
|
+
require "resolv"
|
5
|
+
require "sentry/utils/http_tracing"
|
4
6
|
|
5
7
|
module Sentry
|
6
8
|
# @api private
|
7
9
|
module Net
|
8
10
|
module HTTP
|
11
|
+
include Utils::HttpTracing
|
12
|
+
|
9
13
|
OP_NAME = "http.client"
|
14
|
+
SPAN_ORIGIN = "auto.http.net_http"
|
10
15
|
BREADCRUMB_CATEGORY = "net.http"
|
11
16
|
|
12
17
|
# To explain how the entire thing works, we need to know how the original Net::HTTP#request works
|
@@ -19,8 +24,7 @@ module Sentry
|
|
19
24
|
# req['connection'] ||= 'close'
|
20
25
|
# return request(req, body, &block) # <- request will be called for the second time from the first call
|
21
26
|
# }
|
22
|
-
# end
|
23
|
-
# # .....
|
27
|
+
# end # .....
|
24
28
|
# end
|
25
29
|
# ```
|
26
30
|
#
|
@@ -29,47 +33,29 @@ module Sentry
|
|
29
33
|
return super unless started? && Sentry.initialized?
|
30
34
|
return super if from_sentry_sdk?
|
31
35
|
|
32
|
-
Sentry.with_child_span(op: OP_NAME, start_timestamp: Sentry.utc_now.to_f) do |sentry_span|
|
36
|
+
Sentry.with_child_span(op: OP_NAME, start_timestamp: Sentry.utc_now.to_f, origin: SPAN_ORIGIN) do |sentry_span|
|
33
37
|
request_info = extract_request_info(req)
|
34
38
|
|
35
|
-
if propagate_trace?(request_info[:url]
|
39
|
+
if propagate_trace?(request_info[:url])
|
36
40
|
set_propagation_headers(req)
|
37
41
|
end
|
38
42
|
|
39
|
-
|
40
|
-
|
43
|
+
res = super
|
44
|
+
response_status = res.code.to_i
|
41
45
|
|
42
|
-
|
43
|
-
|
44
|
-
sentry_span.set_data(Span::DataConventions::URL, request_info[:url])
|
45
|
-
sentry_span.set_data(Span::DataConventions::HTTP_METHOD, request_info[:method])
|
46
|
-
sentry_span.set_data(Span::DataConventions::HTTP_QUERY, request_info[:query]) if request_info[:query]
|
47
|
-
sentry_span.set_data(Span::DataConventions::HTTP_STATUS_CODE, res.code.to_i)
|
48
|
-
end
|
46
|
+
if record_sentry_breadcrumb?
|
47
|
+
record_sentry_breadcrumb(request_info, response_status)
|
49
48
|
end
|
50
|
-
end
|
51
|
-
end
|
52
49
|
|
53
|
-
|
50
|
+
if sentry_span
|
51
|
+
set_span_info(sentry_span, request_info, response_status)
|
52
|
+
end
|
54
53
|
|
55
|
-
|
56
|
-
|
54
|
+
res
|
55
|
+
end
|
57
56
|
end
|
58
57
|
|
59
|
-
|
60
|
-
return unless Sentry.initialized? && Sentry.configuration.breadcrumbs_logger.include?(:http_logger)
|
61
|
-
|
62
|
-
crumb = Sentry::Breadcrumb.new(
|
63
|
-
level: :info,
|
64
|
-
category: BREADCRUMB_CATEGORY,
|
65
|
-
type: :info,
|
66
|
-
data: {
|
67
|
-
status: res.code.to_i,
|
68
|
-
**request_info
|
69
|
-
}
|
70
|
-
)
|
71
|
-
Sentry.add_breadcrumb(crumb)
|
72
|
-
end
|
58
|
+
private
|
73
59
|
|
74
60
|
def from_sentry_sdk?
|
75
61
|
dsn = Sentry.configuration.dsn
|
@@ -77,7 +63,10 @@ module Sentry
|
|
77
63
|
end
|
78
64
|
|
79
65
|
def extract_request_info(req)
|
80
|
-
|
66
|
+
# IPv6 url could look like '::1/path', and that won't parse without
|
67
|
+
# wrapping it in square brackets.
|
68
|
+
hostname = address =~ Resolv::IPv6::Regex ? "[#{address}]" : address
|
69
|
+
uri = req.uri || URI.parse(URI::DEFAULT_PARSER.escape("#{use_ssl? ? 'https' : 'http'}://#{hostname}#{req.path}"))
|
81
70
|
url = "#{uri.scheme}://#{uri.host}#{uri.path}" rescue uri.to_s
|
82
71
|
|
83
72
|
result = { method: req.method, url: url }
|
@@ -89,12 +78,6 @@ module Sentry
|
|
89
78
|
|
90
79
|
result
|
91
80
|
end
|
92
|
-
|
93
|
-
def propagate_trace?(url, configuration)
|
94
|
-
url &&
|
95
|
-
configuration.propagate_traces &&
|
96
|
-
configuration.trace_propagation_targets.any? { |target| url.match?(target) }
|
97
|
-
end
|
98
81
|
end
|
99
82
|
end
|
100
83
|
end
|