sentry-ruby 5.5.0 → 5.6.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2ad98e145a972608f9b15a669f771074f1fac592b8819fe1e1e2ac70c6b919a0
4
- data.tar.gz: 5058a18d24be77f5879c4455ab0a437010ad526cb5fa0ec869161c7fa5174d4f
3
+ metadata.gz: 292dc37d7e5703be7d5d2e33a84f02b492759f0c8ef656e3ae660a7196301fe4
4
+ data.tar.gz: 1b76f778b06e4928eb32cabe31fc208a4aa92bf8b62f447abf9dd204c97e26cb
5
5
  SHA512:
6
- metadata.gz: 69c776cddf21fb89754a1750632f2dc3a28d3dc4cd5e20a0c2a175985533b2f1d6774b4196c3437186776b2e60683356b141675606762338bfbb4e4d2fc9f764
7
- data.tar.gz: 3684c877d6370e5f72fb09151e5f01803661e177dfdce326b789e26ed59c06671bde2db641957f6bdc423f576cfe9e97657d5ac7e8fb81a61e8760ac31039ab8
6
+ metadata.gz: ae4dbbb4e60e1dbad0e0e0e5a1fedad72e383b0709453ab4f9f6dc8cc367625e3663848885b893f28f47a3758aed7aec68fc3aa32de3974fdc9e807aa0f5d4bf
7
+ data.tar.gz: 2194f3cb7e4d604d8ddeaf3761406133a049a5e5870ac44e54e8d7ca90d4a41e489b39681b53c912d97c4710bd1813716863364f9de6f233e345c353b31d133f
data/lib/sentry/hub.rb CHANGED
@@ -92,6 +92,24 @@ module Sentry
92
92
  transaction
93
93
  end
94
94
 
95
+ def with_child_span(**attributes, &block)
96
+ current_span = current_scope.get_span
97
+ return yield(nil) unless current_span
98
+
99
+ result = nil
100
+
101
+ begin
102
+ current_span.with_child_span(**attributes) do |child_span|
103
+ current_scope.set_span(child_span)
104
+ result = yield(child_span)
105
+ end
106
+ ensure
107
+ current_scope.set_span(current_span)
108
+ end
109
+
110
+ result
111
+ end
112
+
95
113
  def capture_exception(exception, **options, &block)
96
114
  check_argument_type!(exception, ::Exception)
97
115
 
@@ -73,7 +73,7 @@ module Sentry
73
73
  request.POST
74
74
  elsif request.body # JSON requests, etc
75
75
  data = request.body.read(MAX_BODY_LIMIT)
76
- data = encode_to_utf_8(data.to_s)
76
+ data = Utils::EncodingHelper.encode_to_utf_8(data.to_s)
77
77
  request.body.rewind
78
78
  data
79
79
  end
@@ -94,7 +94,7 @@ module Sentry
94
94
  key = key.sub(/^HTTP_/, "")
95
95
  key = key.split('_').map(&:capitalize).join('-')
96
96
 
97
- memo[key] = encode_to_utf_8(value.to_s)
97
+ memo[key] = Utils::EncodingHelper.encode_to_utf_8(value.to_s)
98
98
  rescue StandardError => e
99
99
  # Rails adds objects to the Rack env that can sometimes raise exceptions
100
100
  # when `to_s` is called.
@@ -105,18 +105,6 @@ module Sentry
105
105
  end
106
106
  end
107
107
 
108
- def encode_to_utf_8(value)
109
- if value.encoding != Encoding::UTF_8 && value.respond_to?(:force_encoding)
110
- value = value.dup.force_encoding(Encoding::UTF_8)
111
- end
112
-
113
- if !value.valid_encoding?
114
- value = value.scrub
115
- end
116
-
117
- value
118
- end
119
-
120
108
  def is_skippable_header?(key)
121
109
  key.upcase != key || # lower-case envs aren't real http headers
122
110
  key == "HTTP_COOKIE" || # Cookies don't go here, they go somewhere else
@@ -15,7 +15,15 @@ module Sentry
15
15
 
16
16
  def initialize(exception:, stacktrace: nil)
17
17
  @type = exception.class.to_s
18
- @value = (exception.message || "").byteslice(0..Event::MAX_MESSAGE_SIZE_IN_BYTES)
18
+ exception_message =
19
+ if exception.respond_to?(:detailed_message)
20
+ exception.detailed_message(highlight: false)
21
+ else
22
+ exception.message || ""
23
+ end
24
+
25
+ @value = exception_message.byteslice(0..Event::MAX_MESSAGE_SIZE_IN_BYTES)
26
+
19
27
  @module = exception.class.to_s.split('::')[0...-1].join('::')
20
28
  @thread_id = Thread.current.object_id
21
29
  @stacktrace = stacktrace
@@ -26,14 +26,21 @@ module Sentry
26
26
  #
27
27
  # So we're only instrumenting request when `Net::HTTP` is already started
28
28
  def request(req, body = nil, &block)
29
- return super unless started?
29
+ return super unless started? && Sentry.initialized?
30
+ return super if from_sentry_sdk?
30
31
 
31
- sentry_span = start_sentry_span
32
- set_sentry_trace_header(req, sentry_span)
32
+ Sentry.with_child_span(op: OP_NAME, start_timestamp: Sentry.utc_now.to_f) do |sentry_span|
33
+ set_sentry_trace_header(req, sentry_span)
33
34
 
34
- super.tap do |res|
35
- record_sentry_breadcrumb(req, res)
36
- record_sentry_span(req, res, sentry_span)
35
+ super.tap do |res|
36
+ record_sentry_breadcrumb(req, res)
37
+
38
+ if sentry_span
39
+ request_info = extract_request_info(req)
40
+ sentry_span.set_description("#{request_info[:method]} #{request_info[:url]}")
41
+ sentry_span.set_data(:status, res.code.to_i)
42
+ end
43
+ end
37
44
  end
38
45
  end
39
46
 
@@ -53,7 +60,6 @@ module Sentry
53
60
 
54
61
  def record_sentry_breadcrumb(req, res)
55
62
  return unless Sentry.initialized? && Sentry.configuration.breadcrumbs_logger.include?(:http_logger)
56
- return if from_sentry_sdk?
57
63
 
58
64
  request_info = extract_request_info(req)
59
65
 
@@ -69,29 +75,6 @@ module Sentry
69
75
  Sentry.add_breadcrumb(crumb)
70
76
  end
71
77
 
72
- def record_sentry_span(req, res, sentry_span)
73
- return unless Sentry.initialized? && sentry_span
74
-
75
- request_info = extract_request_info(req)
76
- sentry_span.set_description("#{request_info[:method]} #{request_info[:url]}")
77
- sentry_span.set_data(:status, res.code.to_i)
78
- finish_sentry_span(sentry_span)
79
- end
80
-
81
- def start_sentry_span
82
- return unless Sentry.initialized? && span = Sentry.get_current_scope.get_span
83
- return if from_sentry_sdk?
84
- return if span.sampled == false
85
-
86
- span.start_child(op: OP_NAME, start_timestamp: Sentry.utc_now.to_f)
87
- end
88
-
89
- def finish_sentry_span(sentry_span)
90
- return unless Sentry.initialized? && sentry_span
91
-
92
- sentry_span.set_timestamp(Sentry.utc_now.to_f)
93
- end
94
-
95
78
  def from_sentry_sdk?
96
79
  dsn = Sentry.configuration.dsn
97
80
  dsn && dsn.host == self.address
@@ -52,7 +52,7 @@ module Sentry
52
52
  end
53
53
 
54
54
  def transaction_op
55
- "rack.request".freeze
55
+ "http.server".freeze
56
56
  end
57
57
 
58
58
  def capture_exception(exception, env)
data/lib/sentry/redis.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  module Sentry
4
4
  # @api private
5
5
  class Redis
6
- OP_NAME = "db.redis.command"
6
+ OP_NAME = "db.redis"
7
7
  LOGGER_NAME = :redis_logger
8
8
 
9
9
  def initialize(commands, host, port, db)
@@ -13,9 +13,14 @@ module Sentry
13
13
  def instrument
14
14
  return yield unless Sentry.initialized?
15
15
 
16
- record_span do
16
+ Sentry.with_child_span(op: OP_NAME, start_timestamp: Sentry.utc_now.to_f) do |span|
17
17
  yield.tap do
18
18
  record_breadcrumb
19
+
20
+ if span
21
+ span.set_description(commands_description)
22
+ span.set_data(:server, server_description)
23
+ end
19
24
  end
20
25
  end
21
26
  end
@@ -24,18 +29,6 @@ module Sentry
24
29
 
25
30
  attr_reader :commands, :host, :port, :db
26
31
 
27
- def record_span
28
- return yield unless (transaction = Sentry.get_current_scope.get_transaction) && transaction.sampled
29
-
30
- sentry_span = transaction.start_child(op: OP_NAME, start_timestamp: Sentry.utc_now.to_f)
31
-
32
- yield.tap do
33
- sentry_span.set_description(commands_description)
34
- sentry_span.set_data(:server, server_description)
35
- sentry_span.set_timestamp(Sentry.utc_now.to_f)
36
- end
37
- end
38
-
39
32
  def record_breadcrumb
40
33
  return unless Sentry.configuration.breadcrumbs_logger.include?(LOGGER_NAME)
41
34
 
@@ -61,10 +54,15 @@ module Sentry
61
54
  def parsed_commands
62
55
  commands.map do |statement|
63
56
  command, key, *arguments = statement
57
+ command_set = { command: command.to_s.upcase, key: key }
64
58
 
65
- { command: command.to_s.upcase, key: key }.tap do |command_set|
66
- command_set[:arguments] = arguments.join(" ") if Sentry.configuration.send_default_pii
59
+ if Sentry.configuration.send_default_pii
60
+ command_set[:arguments] = arguments
61
+ .select { |a| Utils::EncodingHelper.valid_utf_8?(a) }
62
+ .join(" ")
67
63
  end
64
+
65
+ command_set
68
66
  end
69
67
  end
70
68
 
data/lib/sentry/span.rb CHANGED
@@ -60,9 +60,10 @@ module Sentry
60
60
  # The Transaction object the Span belongs to.
61
61
  # Every span needs to be attached to a Transaction and their child spans will also inherit the same transaction.
62
62
  # @return [Transaction]
63
- attr_accessor :transaction
63
+ attr_reader :transaction
64
64
 
65
65
  def initialize(
66
+ transaction:,
66
67
  description: nil,
67
68
  op: nil,
68
69
  status: nil,
@@ -79,6 +80,7 @@ module Sentry
79
80
  @start_timestamp = start_timestamp || Sentry.utc_now.to_f
80
81
  @timestamp = timestamp
81
82
  @description = description
83
+ @transaction = transaction
82
84
  @op = op
83
85
  @status = status
84
86
  @data = {}
@@ -105,10 +107,10 @@ module Sentry
105
107
  end
106
108
 
107
109
  # Generates a W3C Baggage header string for distributed tracing
108
- # from the incoming baggage stored on the transation.
110
+ # from the incoming baggage stored on the transaction.
109
111
  # @return [String, nil]
110
112
  def to_baggage
111
- transaction&.get_baggage&.serialize
113
+ transaction.get_baggage&.serialize
112
114
  end
113
115
 
114
116
  # @return [Hash]
@@ -143,9 +145,8 @@ module Sentry
143
145
  # Starts a child span with given attributes.
144
146
  # @param attributes [Hash] the attributes for the child span.
145
147
  def start_child(**attributes)
146
- attributes = attributes.dup.merge(trace_id: @trace_id, parent_span_id: @span_id, sampled: @sampled)
148
+ attributes = attributes.dup.merge(transaction: @transaction, trace_id: @trace_id, parent_span_id: @span_id, sampled: @sampled)
147
149
  new_span = Span.new(**attributes)
148
- new_span.transaction = transaction
149
150
  new_span.span_recorder = span_recorder
150
151
 
151
152
  if span_recorder
@@ -58,12 +58,11 @@ module Sentry
58
58
  baggage: nil,
59
59
  **options
60
60
  )
61
- super(**options)
61
+ super(transaction: self, **options)
62
62
 
63
63
  @name = name
64
64
  @source = SOURCES.include?(source) ? source.to_sym : :custom
65
65
  @parent_sampled = parent_sampled
66
- @transaction = self
67
66
  @hub = hub
68
67
  @baggage = baggage
69
68
  @configuration = hub.configuration # to be removed
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sentry
4
+ module Utils
5
+ module EncodingHelper
6
+ def self.encode_to_utf_8(value)
7
+ if value.encoding != Encoding::UTF_8 && value.respond_to?(:force_encoding)
8
+ value = value.dup.force_encoding(Encoding::UTF_8)
9
+ end
10
+
11
+ value = value.scrub unless value.valid_encoding?
12
+ value
13
+ end
14
+
15
+ def self.valid_utf_8?(value)
16
+ return true unless value.respond_to?(:force_encoding)
17
+
18
+ value.dup.force_encoding(Encoding::UTF_8).valid_encoding?
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sentry
4
- VERSION = "5.5.0"
4
+ VERSION = "5.6.0"
5
5
  end
data/lib/sentry-ruby.rb CHANGED
@@ -8,6 +8,7 @@ require "sentry/version"
8
8
  require "sentry/exceptions"
9
9
  require "sentry/core_ext/object/deep_dup"
10
10
  require "sentry/utils/argument_checking_helper"
11
+ require "sentry/utils/encoding_helper"
11
12
  require "sentry/utils/logging_helper"
12
13
  require "sentry/configuration"
13
14
  require "sentry/logger"
@@ -441,22 +442,8 @@ module Sentry
441
442
  # end
442
443
  #
443
444
  def with_child_span(**attributes, &block)
444
- if Sentry.initialized? && current_span = get_current_scope.get_span
445
- result = nil
446
-
447
- begin
448
- current_span.with_child_span(**attributes) do |child_span|
449
- get_current_scope.set_span(child_span)
450
- result = yield(child_span)
451
- end
452
- ensure
453
- get_current_scope.set_span(current_span)
454
- end
455
-
456
- result
457
- else
458
- yield(nil)
459
- end
445
+ return yield(nil) unless Sentry.initialized?
446
+ get_current_hub.with_child_span(**attributes, &block)
460
447
  end
461
448
 
462
449
  # Returns the id of the lastly reported Sentry::Event.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sentry-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.5.0
4
+ version: 5.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sentry Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-03 00:00:00.000000000 Z
11
+ date: 2022-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -96,6 +96,7 @@ files:
96
96
  - lib/sentry/transport/http_transport.rb
97
97
  - lib/sentry/utils/argument_checking_helper.rb
98
98
  - lib/sentry/utils/custom_inspection.rb
99
+ - lib/sentry/utils/encoding_helper.rb
99
100
  - lib/sentry/utils/exception_cause_chain.rb
100
101
  - lib/sentry/utils/logging_helper.rb
101
102
  - lib/sentry/utils/real_ip.rb