diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b142fe1b..bf07aeda9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/sentry-rails/spec/sentry/rails/tracing/action_view_subscriber_spec.rb b/sentry-rails/spec/sentry/rails/tracing/action_view_subscriber_spec.rb index dba6c9546..b847089b6 100644 --- a/sentry-rails/spec/sentry/rails/tracing/action_view_subscriber_spec.rb +++ b/sentry-rails/spec/sentry/rails/tracing/action_view_subscriber_spec.rb @@ -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" diff --git a/sentry-rails/spec/sentry/rails/tracing/active_record_subscriber_spec.rb b/sentry-rails/spec/sentry/rails/tracing/active_record_subscriber_spec.rb index 026b2121d..ef688b2b0 100644 --- a/sentry-rails/spec/sentry/rails/tracing/active_record_subscriber_spec.rb +++ b/sentry-rails/spec/sentry/rails/tracing/active_record_subscriber_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/sentry-rails/spec/sentry/rails/tracing/active_support_subscriber_spec.rb b/sentry-rails/spec/sentry/rails/tracing/active_support_subscriber_spec.rb index 0d77fd193..0efaa33d5 100644 --- a/sentry-rails/spec/sentry/rails/tracing/active_support_subscriber_spec.rb +++ b/sentry-rails/spec/sentry/rails/tracing/active_support_subscriber_spec.rb @@ -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") @@ -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") @@ -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") @@ -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") @@ -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") @@ -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) @@ -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") @@ -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") diff --git a/sentry-rails/spec/sentry/rails/tracing_spec.rb b/sentry-rails/spec/sentry/rails/tracing_spec.rb index f21d8cded..aee2c38f9 100644 --- a/sentry-rails/spec/sentry/rails/tracing_spec.rb +++ b/sentry-rails/spec/sentry/rails/tracing_spec.rb @@ -275,7 +275,6 @@ status: "ok", sampled: true, name: "a/path", - hub: Sentry.get_current_hub ) end diff --git a/sentry-ruby/lib/sentry/hub.rb b/sentry-ruby/lib/sentry/hub.rb index cb82939a8..907e844f6 100644 --- a/sentry-ruby/lib/sentry/hub.rb +++ b/sentry-ruby/lib/sentry/hub.rb @@ -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, @@ -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, diff --git a/sentry-ruby/lib/sentry/transaction.rb b/sentry-ruby/lib/sentry/transaction.rb index b2f26a493..75f425dd2 100644 --- a/sentry-ruby/lib/sentry/transaction.rb +++ b/sentry-ruby/lib/sentry/transaction.rb @@ -54,7 +54,6 @@ class Transaction < Span attr_reader :sample_rand def initialize( - hub:, name: nil, source: :custom, parent_sampled: nil, @@ -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) @@ -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 @@ -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 @@ -207,7 +196,10 @@ 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 @@ -215,16 +207,16 @@ def finish(end_timestamp: nil) 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 @@ -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 @@ -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? @@ -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 diff --git a/sentry-ruby/lib/sentry/utils/logging_helper.rb b/sentry-ruby/lib/sentry/utils/logging_helper.rb index e7bf25c13..e383a66d4 100644 --- a/sentry-ruby/lib/sentry/utils/logging_helper.rb +++ b/sentry-ruby/lib/sentry/utils/logging_helper.rb @@ -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 + @sdk_logger ||= Sentry.sdk_logger end end end diff --git a/sentry-ruby/spec/sentry/client/event_sending_spec.rb b/sentry-ruby/spec/sentry/client/event_sending_spec.rb index 08d9a35ec..95d489d35 100644 --- a/sentry-ruby/spec/sentry/client/event_sending_spec.rb +++ b/sentry-ruby/spec/sentry/client/event_sending_spec.rb @@ -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) } @@ -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 diff --git a/sentry-ruby/spec/sentry/client_spec.rb b/sentry-ruby/spec/sentry/client_spec.rb index 1a75f3182..56bab2e9e 100644 --- a/sentry-ruby/spec/sentry/client_spec.rb +++ b/sentry-ruby/spec/sentry/client_spec.rb @@ -17,19 +17,19 @@ def sentry_context end subject { Sentry::Client.new(configuration) } + let(:hub) do + Sentry::Hub.new(subject, Sentry::Scope.new) + end + let(:transaction) do - hub = Sentry::Hub.new(subject, Sentry::Scope.new) - Sentry::Transaction.new( - name: "test transaction", - hub: hub, - sampled: true - ) + hub.start_transaction(name: "test transaction") end let(:fake_time) { Time.now } before do allow(Time).to receive(:now).and_return fake_time + allow(Sentry).to receive(:configuration).and_return configuration end describe "#transport" do @@ -215,14 +215,6 @@ def sentry_context end describe "#event_from_transaction" do - let(:hub) do - Sentry::Hub.new(subject, Sentry::Scope.new) - end - - let(:transaction) do - hub.start_transaction(name: "test transaction") - end - before do configuration.traces_sample_rate = 1.0 @@ -254,7 +246,7 @@ def sentry_context "other-vendor-value-2=foo;bar;" ) - transaction = Sentry::Transaction.new(name: "test transaction", hub: hub, baggage: baggage, sampled: true) + transaction = hub.start_transaction(name: "test transaction", baggage: baggage, sampled: true) event = subject.event_from_transaction(transaction) expect(event.dynamic_sampling_context).to eq({ diff --git a/sentry-ruby/spec/sentry/hub_spec.rb b/sentry-ruby/spec/sentry/hub_spec.rb index b25609fef..737cb7edb 100644 --- a/sentry-ruby/spec/sentry/hub_spec.rb +++ b/sentry-ruby/spec/sentry/hub_spec.rb @@ -358,11 +358,15 @@ end context "when event is a transaction" do + before do + configuration.traces_sample_rate = 1.0 + end + it "transaction.set_context merges and takes precedence over scope.set_context" do scope.set_context(:foo, { val: 42 }) scope.set_context(:bar, { val: 43 }) - transaction = Sentry::Transaction.new(hub: subject, name: 'test') + transaction = subject.start_transaction(name: 'test') transaction.set_context(:foo, { val: 44 }) transaction.set_context(:baz, { val: 45 }) @@ -581,21 +585,27 @@ expect(subject.last_event_id).to eq(event.event_id) end - it 'only records last_event_id for error events' do - exception = ZeroDivisionError.new("divided by 0") - transaction = Sentry::Transaction.new(name: "test transaction", op: "rack.request", hub: subject) + context "with tracing" do + before do + configuration.traces_sample_rate = 1.0 + end + + it 'only records last_event_id for error events' do + exception = ZeroDivisionError.new("divided by 0") + transaction = subject.start_transaction(name: "test transaction", op: "rack.request") - error_event = client.event_from_exception(exception) - transaction_event = client.event_from_transaction(transaction) - check_in_event = client.event_from_check_in("test_slug", :ok) + error_event = client.event_from_exception(exception) + transaction_event = client.event_from_transaction(transaction) + check_in_event = client.event_from_check_in("test_slug", :ok) - subject.capture_event(error_event) - subject.capture_event(transaction_event) - subject.capture_event(check_in_event) + subject.capture_event(error_event) + subject.capture_event(transaction_event) + subject.capture_event(check_in_event) - expect(subject.last_event_id).to eq(error_event.event_id) - expect(subject.last_event_id).not_to eq(transaction_event.event_id) - expect(subject.last_event_id).not_to eq(check_in_event.event_id) + expect(subject.last_event_id).to eq(error_event.event_id) + expect(subject.last_event_id).not_to eq(transaction_event.event_id) + expect(subject.last_event_id).not_to eq(check_in_event.event_id) + end end end diff --git a/sentry-ruby/spec/sentry/rack/capture_exceptions_spec.rb b/sentry-ruby/spec/sentry/rack/capture_exceptions_spec.rb index 840545557..28a2f2736 100644 --- a/sentry-ruby/spec/sentry/rack/capture_exceptions_spec.rb +++ b/sentry-ruby/spec/sentry/rack/capture_exceptions_spec.rb @@ -240,12 +240,11 @@ def inspect context "when sentry-trace header is sent" do let(:external_transaction) do - Sentry::Transaction.new( + Sentry.start_transaction( op: "pageload", status: "ok", sampled: true, name: "a/path", - hub: Sentry.get_current_hub ) end @@ -560,7 +559,6 @@ def will_be_sampled_by_sdk status: "ok", sampled: true, name: "a/path", - hub: Sentry.get_current_hub ) end diff --git a/sentry-ruby/spec/sentry/scope/setters_spec.rb b/sentry-ruby/spec/sentry/scope/setters_spec.rb index a688e5c68..15354a254 100644 --- a/sentry-ruby/spec/sentry/scope/setters_spec.rb +++ b/sentry-ruby/spec/sentry/scope/setters_spec.rb @@ -24,10 +24,14 @@ end describe "#set_span" do + before do + perform_basic_setup do |config| + config.traces_sample_rate = 1.0 + end + end + let(:transaction) do - client = Sentry::Client.new(Sentry::Configuration.new) - hub = Sentry::Hub.new(client, subject) - Sentry::Transaction.new(name: "test transaction", hub: hub) + Sentry.start_transaction(name: "test transaction") end let(:span) { Sentry::Span.new(op: "foo", transaction: transaction) } diff --git a/sentry-ruby/spec/sentry/scope_spec.rb b/sentry-ruby/spec/sentry/scope_spec.rb index e67c1e51c..5b24dbcd0 100644 --- a/sentry-ruby/spec/sentry/scope_spec.rb +++ b/sentry-ruby/spec/sentry/scope_spec.rb @@ -60,7 +60,7 @@ it "deep-copies span as well" do perform_basic_setup - span = Sentry::Transaction.new(sampled: true, hub: hub) + span = Sentry::Transaction.new(sampled: true) subject.set_span(span) copy = subject.dup @@ -128,7 +128,7 @@ subject.set_extras({ additional_info: "hello" }) subject.set_user({ id: 1 }) subject.set_transaction_name("WelcomeController#index") - subject.set_span(Sentry::Transaction.new(op: "foo", hub: hub)) + subject.set_span(Sentry::Transaction.new(op: "foo")) subject.set_fingerprint(["foo"]) scope_id = subject.object_id @@ -155,7 +155,7 @@ end let(:transaction) do - Sentry::Transaction.new(op: "parent", hub: hub) + Sentry::Transaction.new(op: "parent") end context "with span in the scope" do @@ -321,7 +321,7 @@ end it "sets trace context and dynamic_sampling_context from span if there's a span" do - transaction = Sentry::Transaction.new(op: "foo", hub: hub) + transaction = Sentry::Transaction.new(op: "foo") subject.set_span(transaction) subject.apply_to_event(event) diff --git a/sentry-ruby/spec/sentry/span_spec.rb b/sentry-ruby/spec/sentry/span_spec.rb index 8d65aeb9d..f4c533d39 100644 --- a/sentry-ruby/spec/sentry/span_spec.rb +++ b/sentry-ruby/spec/sentry/span_spec.rb @@ -7,9 +7,8 @@ end let(:transaction) do - Sentry::Transaction.new( + hub.start_transaction( name: "test transaction", - hub: hub, sampled: true ) end @@ -100,7 +99,9 @@ describe "#to_baggage" do before do # because initializing transactions requires an active hub - perform_basic_setup + perform_basic_setup do |config| + config.traces_sample_rate = 1.0 + end end subject do @@ -113,7 +114,7 @@ "other-vendor-value-2=foo;bar;" ) - Sentry::Transaction.new(hub: Sentry.get_current_hub, baggage: baggage).start_child + Sentry.start_transaction(baggage: baggage).start_child end it "propagates sentry baggage values" do @@ -129,7 +130,9 @@ describe "#get_dynamic_sampling_context" do before do # because initializing transactions requires an active hub - perform_basic_setup + perform_basic_setup do |config| + config.traces_sample_rate = 1.0 + end end subject do @@ -142,7 +145,7 @@ "other-vendor-value-2=foo;bar;" ) - Sentry::Transaction.new(hub: Sentry.get_current_hub, baggage: baggage).start_child + Sentry.start_transaction(baggage: baggage).start_child end it "propagates sentry dynamic_sampling_context" do @@ -158,7 +161,9 @@ describe "#start_child" do before do # because initializing transactions requires an active hub - perform_basic_setup + perform_basic_setup do |config| + config.traces_sample_rate = 1.0 + end end it "initializes a new child Span" do @@ -198,7 +203,7 @@ context "when the parent span has a span_recorder" do subject do # inherits the span recorder from the transaction - Sentry::Transaction.new(hub: Sentry.get_current_hub).start_child + Sentry.start_transaction(name: "test").start_child end it "gives the child span its span_recorder" do diff --git a/sentry-ruby/spec/sentry/transaction_spec.rb b/sentry-ruby/spec/sentry/transaction_spec.rb index 1a82fcb34..4b2b61572 100644 --- a/sentry-ruby/spec/sentry/transaction_spec.rb +++ b/sentry-ruby/spec/sentry/transaction_spec.rb @@ -14,7 +14,6 @@ parent_sampled: true, name: "foo", source: :view, - hub: Sentry.get_current_hub ) end @@ -127,14 +126,14 @@ it "sets @sampled to false and return" do allow(Sentry.configuration).to receive(:tracing_enabled?).and_return(false) - transaction = described_class.new(sampled: true, hub: Sentry.get_current_hub) + transaction = described_class.new(sampled: true) transaction.set_initial_sample_decision(sampling_context: {}) expect(transaction.sampled).to eq(false) end end context "when tracing is enabled" do - let(:subject) { described_class.new(op: "rack.request", hub: Sentry.get_current_hub) } + let(:subject) { described_class.new(op: "rack.request") } before do allow(Sentry.configuration).to receive(:tracing_enabled?).and_return(true) @@ -142,12 +141,12 @@ context "when the transaction already has a decision" do it "doesn't change it" do - transaction = described_class.new(sampled: true, hub: Sentry.get_current_hub) + transaction = described_class.new(sampled: true) transaction.set_initial_sample_decision(sampling_context: {}) expect(transaction.sampled).to eq(true) expect(transaction.effective_sample_rate).to eq(1.0) - transaction = described_class.new(sampled: false, hub: Sentry.get_current_hub) + transaction = described_class.new(sampled: false) transaction.set_initial_sample_decision(sampling_context: {}) expect(transaction.sampled).to eq(false) expect(transaction.effective_sample_rate).to eq(0.0) @@ -169,7 +168,7 @@ it "uses traces_sample_rate for sampling (positive result)" do # Create transaction with sample_rand < sample_rate (0.5) to ensure sampled - transaction = described_class.new(op: "rack.request", hub: Sentry.get_current_hub, sample_rand: 0.4) + transaction = described_class.new(op: "rack.request", sample_rand: 0.4) transaction.set_initial_sample_decision(sampling_context: {}) expect(transaction.sampled).to eq(true) @@ -181,7 +180,7 @@ it "uses traces_sample_rate for sampling (negative result)" do # Create transaction with sample_rand > sample_rate (0.5) to ensure not sampled - transaction = described_class.new(op: "rack.request", hub: Sentry.get_current_hub, sample_rand: 0.6) + transaction = described_class.new(op: "rack.request", sample_rand: 0.6) transaction.set_initial_sample_decision(sampling_context: {}) expect(transaction.sampled).to eq(false) @@ -241,19 +240,19 @@ expect(Sentry.configuration.sdk_logger).to receive(:debug).exactly(3).and_call_original Sentry.configuration.traces_sampler = ->(_) { true } - subject = described_class.new(hub: Sentry.get_current_hub) + subject = described_class.new subject.set_initial_sample_decision(sampling_context: {}) expect(subject.sampled).to eq(true) expect(subject.effective_sample_rate).to eq(1.0) Sentry.configuration.traces_sampler = ->(_) { 1.0 } - subject = described_class.new(hub: Sentry.get_current_hub) + subject = described_class.new subject.set_initial_sample_decision(sampling_context: {}) expect(subject.sampled).to eq(true) expect(subject.effective_sample_rate).to eq(1.0) Sentry.configuration.traces_sampler = ->(_) { 1 } - subject = described_class.new(hub: Sentry.get_current_hub) + subject = described_class.new subject.set_initial_sample_decision(sampling_context: {}) expect(subject.sampled).to eq(true) expect(subject.effective_sample_rate).to eq(1.0) @@ -267,13 +266,13 @@ expect(Sentry.configuration.sdk_logger).to receive(:debug).exactly(2).and_call_original Sentry.configuration.traces_sampler = ->(_) { false } - subject = described_class.new(hub: Sentry.get_current_hub) + subject = described_class.new subject.set_initial_sample_decision(sampling_context: {}) expect(subject.sampled).to eq(false) expect(subject.effective_sample_rate).to eq(0.0) Sentry.configuration.traces_sampler = ->(_) { 0.0 } - subject = described_class.new(hub: Sentry.get_current_hub) + subject = described_class.new subject.set_initial_sample_decision(sampling_context: {}) expect(subject.sampled).to eq(false) expect(subject.effective_sample_rate).to eq(0.0) @@ -324,10 +323,6 @@ Sentry.get_current_client.transport.events end - let(:another_hub) do - Sentry.get_current_hub.clone - end - it "finishes the transaction, converts it into an Event and send it" do subject.finish @@ -372,35 +367,15 @@ it "only follows sampling decision" do Sentry.configuration.traces_sampler = proc { false } - subject = described_class.new(parent_sampled: true, hub: Sentry.get_current_hub) + subject = described_class.new(parent_sampled: true) subject.finish expect(events.count).to eq(0) end - describe "hub selection" do - it "submits the event with the transaction's hub by default" do - # Create transaction with the specific hub from the beginning - transaction = described_class.new( - op: "sql.query", - description: "SELECT * FROM users;", - status: "ok", - sampled: true, - parent_sampled: true, - name: "foo", - source: :view, - hub: another_hub - ) - - expect(another_hub).to receive(:capture_event) - - transaction.finish - end - end - context "if the transaction is not sampled" do - subject { described_class.new(sampled: false, hub: Sentry.get_current_hub) } + subject { described_class.new(sampled: false) } it "doesn't send it" do subject.finish @@ -423,8 +398,6 @@ end end - subject { described_class.new(hub: Sentry.get_current_hub) } - it "records lost event with reason backpressure" do expect(Sentry.get_current_client.transport).to receive(:any_rate_limited?).and_return(true) Sentry.backpressure_monitor.run @@ -432,7 +405,7 @@ # Create transaction with sample_rand that will be rejected after backpressure downsampling # With traces_sample_rate = 1.0 and downsample_factor = 1, effective rate becomes 0.5 # So sample_rand = 0.6 > 0.5 will be rejected - transaction = described_class.new(hub: Sentry.get_current_hub, sample_rand: 0.6) + transaction = described_class.new(sample_rand: 0.6) transaction.finish expect(Sentry.get_current_client.transport).to have_recorded_lost_event(:backpressure, 'transaction') @@ -441,7 +414,7 @@ end context "if the transaction doesn't have a name" do - subject { described_class.new(sampled: true, hub: Sentry.get_current_hub) } + subject { described_class.new(sampled: true) } it "adds a default name" do subject.finish @@ -523,7 +496,6 @@ parent_sampled: true, name: "foo", source: source, - hub: Sentry.get_current_hub, baggage: incoming_baggage, sample_rand: 0.123456 # Use a known value for predictable testing ) diff --git a/sentry-ruby/spec/sentry/transactions/trace_propagation_spec.rb b/sentry-ruby/spec/sentry/transactions/trace_propagation_spec.rb index 1bab090f6..4b175bfe3 100644 --- a/sentry-ruby/spec/sentry/transactions/trace_propagation_spec.rb +++ b/sentry-ruby/spec/sentry/transactions/trace_propagation_spec.rb @@ -104,15 +104,14 @@ trace_id = "771a43a4192642f0b136d5159a501700" results = 5.times.map do - transaction = Sentry::Transaction.new(trace_id: trace_id, hub: Sentry.get_current_hub) - Sentry.start_transaction(transaction: transaction) + transaction = Sentry.start_transaction(trace_id: trace_id) transaction.sampled end expect(results.uniq.length).to eq(1) sample_rands = 5.times.map do - transaction = Sentry::Transaction.new(trace_id: trace_id, hub: Sentry.get_current_hub) + transaction = Sentry.start_transaction(trace_id: trace_id) baggage = transaction.get_baggage baggage.items["sample_rand"] end @@ -135,8 +134,7 @@ baggage = Sentry::Baggage.new({ "sample_rate" => "0.75" }) - parent_transaction = Sentry::Transaction.new( - hub: Sentry.get_current_hub, + parent_transaction = Sentry.start_transaction( baggage: baggage, sample_rand: 0.6 ) diff --git a/sentry-ruby/spec/sentry/transport_spec.rb b/sentry-ruby/spec/sentry/transport_spec.rb index 227f27409..49285704e 100644 --- a/sentry-ruby/spec/sentry/transport_spec.rb +++ b/sentry-ruby/spec/sentry/transport_spec.rb @@ -11,9 +11,6 @@ end let(:client) { Sentry::Client.new(configuration) } - let(:hub) do - Sentry::Hub.new(client, Sentry::Scope.new) - end let(:dynamic_sampling_context) do { @@ -60,7 +57,7 @@ context "transaction event" do let(:transaction) do - Sentry::Transaction.new(name: "test transaction", op: "rack.request", hub: hub) + Sentry::Transaction.new(name: "test transaction", op: "rack.request") end let(:event) do @@ -348,7 +345,7 @@ context "transaction event" do let(:transaction) do - Sentry::Transaction.new(name: "test transaction", op: "rack.request", hub: hub) + Sentry::Transaction.new(name: "test transaction", op: "rack.request") end let(:event) { client.event_from_transaction(transaction) } let(:envelope) { subject.envelope_from_event(event) } diff --git a/sentry-ruby/spec/sentry_spec.rb b/sentry-ruby/spec/sentry_spec.rb index ecb7dedc2..7a4419ecc 100644 --- a/sentry-ruby/spec/sentry_spec.rb +++ b/sentry-ruby/spec/sentry_spec.rb @@ -503,50 +503,34 @@ end it "gives /payment 0.5 of rate" do - transaction = Sentry::Transaction.new(hub: Sentry.get_current_hub, op: "rack.request", name: "/payment", sample_rand: 0.4) - - described_class.start_transaction(transaction: transaction) + transaction = described_class.start_transaction(op: "rack.request", name: "/payment", sample_rand: 0.4) expect(transaction.sampled).to eq(true) - transaction = Sentry::Transaction.new(hub: Sentry.get_current_hub, op: "rack.request", name: "/payment", sample_rand: 0.6) - - described_class.start_transaction(transaction: transaction) + transaction = described_class.start_transaction(op: "rack.request", name: "/payment", sample_rand: 0.6) expect(transaction.sampled).to eq(false) end it "gives /api 0.2 of rate" do - transaction = Sentry::Transaction.new(hub: Sentry.get_current_hub, op: "rack.request", name: "/api", sample_rand: 0.1) - - described_class.start_transaction(transaction: transaction) + transaction = described_class.start_transaction(op: "rack.request", name: "/api", sample_rand: 0.1) expect(transaction.sampled).to eq(true) - transaction = Sentry::Transaction.new(hub: Sentry.get_current_hub, op: "rack.request", name: "/api", sample_rand: 0.3) - - described_class.start_transaction(transaction: transaction) + transaction = described_class.start_transaction(op: "rack.request", name: "/api", sample_rand: 0.3) expect(transaction.sampled).to eq(false) end it "gives other paths 0.1 of rate" do - transaction = Sentry::Transaction.new(hub: Sentry.get_current_hub, op: "rack.request", name: "/orders", sample_rand: 0.05) - - described_class.start_transaction(transaction: transaction) + transaction = described_class.start_transaction(op: "rack.request", name: "/orders", sample_rand: 0.05) expect(transaction.sampled).to eq(true) - transaction = Sentry::Transaction.new(hub: Sentry.get_current_hub, op: "rack.request", name: "/orders", sample_rand: 0.2) - - described_class.start_transaction(transaction: transaction) + transaction = described_class.start_transaction(op: "rack.request", name: "/orders", sample_rand: 0.2) expect(transaction.sampled).to eq(false) end it "gives sidekiq ops 0.01 of rate" do - transaction = Sentry::Transaction.new(hub: Sentry.get_current_hub, op: "sidekiq", sample_rand: 0.005) - - described_class.start_transaction(transaction: transaction) + transaction = described_class.start_transaction(op: "sidekiq", sample_rand: 0.005) expect(transaction.sampled).to eq(true) - transaction = Sentry::Transaction.new(hub: Sentry.get_current_hub, op: "sidekiq", sample_rand: 0.02) - - described_class.start_transaction(transaction: transaction) + transaction = described_class.start_transaction(op: "sidekiq", sample_rand: 0.02) expect(transaction.sampled).to eq(false) end end @@ -564,10 +548,8 @@ context "when given an transaction object" do it "adds sample decision to it" do - transaction = Sentry::Transaction.new(hub: Sentry.get_current_hub) - - described_class.start_transaction(transaction: transaction) - + transaction = Sentry::Transaction.new(name: "test") + transaction = described_class.start_transaction(transaction: transaction) expect(transaction.sampled).to eq(true) end @@ -577,9 +559,7 @@ context = sampling_context end - transaction = Sentry::Transaction.new(op: "foo", hub: Sentry.get_current_hub) - - described_class.start_transaction(transaction: transaction) + described_class.start_transaction(op: "foo") expect(context[:parent_sampled]).to be_nil expect(context[:transaction_context][:op]).to eq("foo") @@ -591,10 +571,7 @@ context = sampling_context end - transaction = Sentry::Transaction.new(parent_sampled: true, hub: Sentry.get_current_hub) - - described_class.start_transaction(transaction: transaction) - + described_class.start_transaction(parent_sampled: true) expect(context[:parent_sampled]).to eq(true) end end @@ -681,7 +658,7 @@ context "when the current span is present" do let(:parent_span) do - transaction = Sentry::Transaction.new(op: "foo", hub: Sentry.get_current_hub) + transaction = described_class.start_transaction(op: "foo") Sentry::Span.new(op: "parent", transaction: transaction) end @@ -884,15 +861,23 @@ expect(traceparent).to eq("#{propagation_context.trace_id}-#{propagation_context.span_id}") end - it "returns a valid traceparent header from scope current span" do - transaction = Sentry::Transaction.new(op: "foo", hub: Sentry.get_current_hub, sampled: true) - span = transaction.start_child(op: "parent") - described_class.get_current_scope.set_span(span) + context "with tracing" do + before do + perform_basic_setup do |config| + config.traces_sample_rate = 1.0 + end + end + + it "returns a valid traceparent header from scope current span" do + transaction = described_class.start_transaction(op: "foo", sampled: true) + span = transaction.start_child(op: "parent") + described_class.get_current_scope.set_span(span) - traceparent = described_class.get_traceparent + traceparent = described_class.get_traceparent - expect(traceparent).to match(Sentry::PropagationContext::SENTRY_TRACE_REGEXP) - expect(traceparent).to eq("#{span.trace_id}-#{span.span_id}-1") + expect(traceparent).to match(Sentry::PropagationContext::SENTRY_TRACE_REGEXP) + expect(traceparent).to eq("#{span.trace_id}-#{span.span_id}-1") + end end end @@ -904,14 +889,22 @@ expect(baggage).to eq("sentry-trace_id=#{propagation_context.trace_id},sentry-sample_rand=#{Sentry::Utils::SampleRand.format(propagation_context.sample_rand)},sentry-environment=development,sentry-public_key=12345") end - it "returns a valid baggage header from scope current span" do - transaction = Sentry::Transaction.new(op: "foo", hub: Sentry.get_current_hub, sampled: true) - span = transaction.start_child(op: "parent") - described_class.get_current_scope.set_span(span) + context "with tracing" do + before do + perform_basic_setup do |config| + config.traces_sample_rate = 1.0 + end + end - baggage = described_class.get_baggage + it "returns a valid baggage header from scope current span" do + transaction = described_class.start_transaction(op: "foo", sampled: true) + span = transaction.start_child(op: "parent") + described_class.get_current_scope.set_span(span) - expect(baggage).to eq("sentry-trace_id=#{span.trace_id},sentry-sample_rand=#{Sentry::Utils::SampleRand.format(transaction.sample_rand)},sentry-sampled=true,sentry-environment=development,sentry-public_key=12345") + baggage = described_class.get_baggage + + expect(baggage).to eq("sentry-trace_id=#{span.trace_id},sentry-sample_rate=1.0,sentry-sample_rand=#{Sentry::Utils::SampleRand.format(transaction.sample_rand)},sentry-sampled=true,sentry-environment=development,sentry-public_key=12345") + end end end