Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- Remove deprecated attribute `Sentry::Transaction.configuration`
- Remove deprecated attribute `Sentry::Transaction.hub`
- Remove deprecated argument `hub` to `Sentry::Transaction.finish`
- Remove deprecated argument `hub` to `Sentry::Transaction#initialize` ([#2739](https://github.com/getsentry/sentry-ruby/pull/2739))
- Remove `:monotonic_active_support_logger` from `config.breadcrumbs_logger` ([#2717](https://github.com/getsentry/sentry-ruby/pull/2717))
- Migrate from to_hash to to_h ([#2351](https://github.com/getsentry/sentry-ruby/pull/2351))
- Add `before_send_check_in` for applying to `CheckInEvent` ([#2703](https://github.com/getsentry/sentry-ruby/pull/2703))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
end

it "doesn't record spans" do
transaction = Sentry::Transaction.new(sampled: false, hub: Sentry.get_current_hub)
transaction = Sentry::Transaction.new(sampled: false)
Sentry.get_current_scope.set_span(transaction)

get "/view"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
end

it "records database query events" do
transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub)
transaction = Sentry.start_transaction(sampled: true)
Sentry.get_current_scope.set_span(transaction)

Post.all.to_a
Expand Down Expand Up @@ -54,7 +54,7 @@ def foo
rspec_class = self.name # RSpec::ExampleGroups::[....]

before do
transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub)
transaction = Sentry.start_transaction(sampled: true)
Sentry.get_current_scope.set_span(transaction)

foo
Expand Down Expand Up @@ -121,7 +121,7 @@ def foo
end

it "records database cached query events", skip: Rails.version.to_f < 5.1 do
transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub)
transaction = Sentry.start_transaction(sampled: true)
Sentry.get_current_scope.set_span(transaction)

ActiveRecord::Base.connection.cache do
Expand Down Expand Up @@ -155,7 +155,7 @@ def foo
end

it "doesn't record spans" do
transaction = Sentry::Transaction.new(sampled: false, hub: Sentry.get_current_hub)
transaction = Sentry::Transaction.new(sampled: false)
Sentry.get_current_scope.set_span(transaction)

Post.all.to_a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
end

it "tracks cache write" do
transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub)
transaction = Sentry.start_transaction(sampled: true)
Sentry.get_current_scope.set_span(transaction)

Rails.cache.write("my_cache_key", "my_cache_value")
Expand All @@ -37,7 +37,7 @@

Rails.cache.write("my_cache_key", 0)

transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub)
transaction = Sentry.start_transaction(sampled: true)
Sentry.get_current_scope.set_span(transaction)
Rails.cache.increment("my_cache_key")

Expand All @@ -57,7 +57,7 @@

Rails.cache.write("my_cache_key", 0)

transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub)
transaction = Sentry.start_transaction(sampled: true)
Sentry.get_current_scope.set_span(transaction)
Rails.cache.decrement("my_cache_key")

Expand All @@ -72,7 +72,7 @@
end

it "tracks cache read" do
transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub)
transaction = Sentry.start_transaction(sampled: true)
Sentry.get_current_scope.set_span(transaction)
Rails.cache.read("my_cache_key")

Expand All @@ -87,7 +87,7 @@
end

it "tracks cache delete" do
transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub)
transaction = Sentry.start_transaction(sampled: true)
Sentry.get_current_scope.set_span(transaction)

Rails.cache.read("my_cache_key")
Expand All @@ -102,7 +102,7 @@
expect(cache_transaction[:spans][0][:origin]).to eq("auto.cache.rails")
end
it "tracks cache prune" do
transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub)
transaction = Sentry.start_transaction(sampled: true)
Sentry.get_current_scope.set_span(transaction)

Rails.cache.write("my_cache_key", 123, expires_in: 0.seconds)
Expand All @@ -123,7 +123,7 @@
skip("cache.hit is unset on Rails 6.0.x.") if Rails.version.to_i == 6

Rails.cache.write("my_cache_key", "my_cache_value")
transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub)
transaction = Sentry.start_transaction(sampled: true)
Sentry.get_current_scope.set_span(transaction)
Rails.cache.read("my_cache_key")
Rails.cache.read("my_cache_key_non_existing")
Expand All @@ -146,7 +146,7 @@

it "tracks cache delete" do
Rails.cache.write("my_cache_key", "my_cache_value")
transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub)
transaction = Sentry.start_transaction(sampled: true)
Sentry.get_current_scope.set_span(transaction)
Rails.cache.delete("my_cache_key")

Expand Down
1 change: 0 additions & 1 deletion sentry-rails/spec/sentry/rails/tracing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@
status: "ok",
sampled: true,
name: "a/path",
hub: Sentry.get_current_hub
)
end

Expand Down
3 changes: 1 addition & 2 deletions sentry-ruby/lib/sentry/hub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def start_transaction(transaction: nil, custom_sampling_context: {}, instrumente
return unless configuration.tracing_enabled?
return unless instrumenter == configuration.instrumenter

transaction ||= Transaction.new(**options.merge(hub: self))
transaction ||= Transaction.new(**options)

sampling_context = {
transaction_context: transaction.to_h,
Expand Down Expand Up @@ -353,7 +353,6 @@ def continue_trace(env, **options)
return nil unless propagation_context.incoming_trace

Transaction.new(
hub: self,
trace_id: propagation_context.trace_id,
parent_span_id: propagation_context.parent_span_id,
parent_sampled: propagation_context.parent_sampled,
Expand Down
64 changes: 34 additions & 30 deletions sentry-ruby/lib/sentry/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class Transaction < Span
attr_reader :sample_rand

def initialize(
hub:,
name: nil,
source: :custom,
parent_sampled: nil,
Expand All @@ -66,26 +65,14 @@ def initialize(

set_name(name, source: source)
@parent_sampled = parent_sampled
@hub = hub
@baggage = baggage
@tracing_enabled = hub.configuration.tracing_enabled?
@traces_sampler = hub.configuration.traces_sampler
@traces_sample_rate = hub.configuration.traces_sample_rate
@trace_ignore_status_codes = hub.configuration.trace_ignore_status_codes
@sdk_logger = hub.configuration.sdk_logger
@release = hub.configuration.release
@environment = hub.configuration.environment
@dsn = hub.configuration.dsn
@effective_sample_rate = nil
@contexts = {}
@measurements = {}
@sample_rand = sample_rand

unless hub.profiler_running?
@profiler = hub.configuration.profiler_class.new(hub.configuration)
end

init_span_recorder
init_profiler

unless @sample_rand
generator = Utils::SampleRand.new(trace_id: @trace_id)
Expand Down Expand Up @@ -141,7 +128,9 @@ def set_measurement(name, value, unit = "")
# @param sampling_context [Hash] a context Hash that'll be passed to `traces_sampler` (if provided).
# @return [void]
def set_initial_sample_decision(sampling_context:)
unless @tracing_enabled
configuration = Sentry.configuration

unless configuration && configuration.tracing_enabled?
@sampled = false
return
end
Expand All @@ -152,12 +141,12 @@ def set_initial_sample_decision(sampling_context:)
end

sample_rate =
if @traces_sampler.is_a?(Proc)
@traces_sampler.call(sampling_context)
if configuration.traces_sampler.is_a?(Proc)
configuration.traces_sampler.call(sampling_context)
elsif !sampling_context[:parent_sampled].nil?
sampling_context[:parent_sampled]
else
@traces_sample_rate
configuration.traces_sample_rate
end

transaction_description = generate_transaction_description
Expand Down Expand Up @@ -207,24 +196,27 @@ def finish(end_timestamp: nil)
@name = UNLABELD_NAME
end

@hub.stop_profiler!(self)
hub = Sentry.get_current_hub
return unless hub

hub.stop_profiler!(self)

if @sampled && ignore_status_code?
@sampled = false

status_code = get_http_status_code
log_debug("#{MESSAGE_PREFIX} Discarding #{generate_transaction_description} due to ignored HTTP status code: #{status_code}")

@hub.current_client.transport.record_lost_event(:event_processor, "transaction")
@hub.current_client.transport.record_lost_event(:event_processor, "span")
hub.current_client.transport.record_lost_event(:event_processor, "transaction")
hub.current_client.transport.record_lost_event(:event_processor, "span")
elsif @sampled
event = @hub.current_client.event_from_transaction(self)
@hub.capture_event(event)
event = hub.current_client.event_from_transaction(self)
hub.capture_event(event)
else
is_backpressure = Sentry.backpressure_monitor&.downsample_factor&.positive?
reason = is_backpressure ? :backpressure : :sample_rate
@hub.current_client.transport.record_lost_event(reason, "transaction")
@hub.current_client.transport.record_lost_event(reason, "span")
hub.current_client.transport.record_lost_event(reason, "transaction")
hub.current_client.transport.record_lost_event(reason, "span")
end
end

Expand Down Expand Up @@ -275,6 +267,15 @@ def init_span_recorder(limit = 1000)
@span_recorder.add(self)
end

def init_profiler
hub = Sentry.get_current_hub
return unless hub

unless hub.profiler_running?
@profiler = hub.configuration.profiler_class.new(hub.configuration)
end
end

private

def generate_transaction_description
Expand All @@ -285,14 +286,16 @@ def generate_transaction_description
end

def populate_head_baggage
configuration = Sentry.configuration

items = {
"trace_id" => trace_id,
"sample_rate" => effective_sample_rate&.to_s,
"sample_rand" => Utils::SampleRand.format(@sample_rand),
"sampled" => sampled&.to_s,
"environment" => @environment,
"release" => @release,
"public_key" => @dsn&.public_key
"environment" => configuration&.environment,
"release" => configuration&.release,
"public_key" => configuration&.dsn&.public_key
}

items["transaction"] = name unless source_low_quality?
Expand All @@ -302,12 +305,13 @@ def populate_head_baggage
end

def ignore_status_code?
return false unless @trace_ignore_status_codes
trace_ignore_status_codes = Sentry.configuration&.trace_ignore_status_codes
return false unless trace_ignore_status_codes

status_code = get_http_status_code
return false unless status_code

@trace_ignore_status_codes.any? do |ignored|
trace_ignore_status_codes.any? do |ignored|
ignored.is_a?(Range) ? ignored.include?(status_code) : status_code == ignored
end
end
Expand Down
14 changes: 8 additions & 6 deletions sentry-ruby/lib/sentry/utils/logging_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,29 @@
module Sentry
# @private
module LoggingHelper
# @!visibility private
attr_reader :sdk_logger

# @!visibility private
def log_error(message, exception, debug: false)
message = "#{message}: #{exception.message}"
message += "\n#{exception.backtrace.join("\n")}" if debug

sdk_logger.error(LOGGER_PROGNAME) do
sdk_logger&.error(LOGGER_PROGNAME) do
message
end
end

# @!visibility private
def log_debug(message)
sdk_logger.debug(LOGGER_PROGNAME) { message }
sdk_logger&.debug(LOGGER_PROGNAME) { message }
end

# @!visibility private
def log_warn(message)
sdk_logger.warn(LOGGER_PROGNAME) { message }
sdk_logger&.warn(LOGGER_PROGNAME) { message }
end

# @!visibility private
def sdk_logger
Copy link
Member Author

@sl0thentr0py sl0thentr0py Oct 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generalized this to fallback to the global logger

@sdk_logger ||= Sentry.sdk_logger
end
end
end
5 changes: 4 additions & 1 deletion sentry-ruby/spec/sentry/client/event_sending_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

before do
stub_request(:post, Sentry::TestHelper::DUMMY_DSN)
allow(Sentry).to receive(:configuration).and_return configuration
end

subject(:client) { Sentry::Client.new(configuration) }
Expand All @@ -20,7 +21,9 @@
end

let(:transaction) do
transaction = Sentry::Transaction.new(name: "test transaction", op: "rack.request", hub: hub)
configuration.traces_sample_rate = 1.0

transaction = hub.start_transaction(name: "test transaction", op: "rack.request")
5.times { |i| transaction.with_child_span(description: "span_#{i}") { } }
transaction
end
Expand Down
Loading
Loading