From c686a62e7e60e4d9d06e6bcda00bbfe0d8478660 Mon Sep 17 00:00:00 2001 From: st0012 Date: Fri, 20 Nov 2020 22:01:26 +0800 Subject: [PATCH] Change APIs --- sentry-ruby/lib/sentry-ruby.rb | 8 ------ sentry-ruby/lib/sentry/hub.rb | 12 -------- sentry-ruby/lib/sentry/scope.rb | 14 +++++++--- sentry-ruby/spec/sentry/hub_spec.rb | 40 --------------------------- sentry-ruby/spec/sentry/scope_spec.rb | 36 ++++++++++++++++++++++++ sentry-ruby/spec/sentry_spec.rb | 14 ---------- 6 files changed, 46 insertions(+), 78 deletions(-) diff --git a/sentry-ruby/lib/sentry-ruby.rb b/sentry-ruby/lib/sentry-ruby.rb index 15b0e802b..f24f42f8c 100644 --- a/sentry-ruby/lib/sentry-ruby.rb +++ b/sentry-ruby/lib/sentry-ruby.rb @@ -63,10 +63,6 @@ def get_current_hub Thread.current[THREAD_LOCAL] || clone_hub_to_current_thread end - def get_transaction - get_current_hub.get_transaction - end - def clone_hub_to_current_thread Thread.current[THREAD_LOCAL] = get_main_hub.clone end @@ -99,10 +95,6 @@ def capture_message(message, **options, &block) get_current_hub.capture_message(message, **options, &block) end - def start_span(**options) - get_current_hub.start_span(**options) - end - def start_transaction(**options) get_current_hub.start_transaction(**options) end diff --git a/sentry-ruby/lib/sentry/hub.rb b/sentry-ruby/lib/sentry/hub.rb index 58ae7ce4d..c6b69e75a 100644 --- a/sentry-ruby/lib/sentry/hub.rb +++ b/sentry-ruby/lib/sentry/hub.rb @@ -67,18 +67,6 @@ def pop_scope @stack.pop end - def get_transaction - current_scope.span - end - - def start_span(**options) - if span = current_scope.span - span.start_child(**options) - else - Span.new(**options) - end - end - def start_transaction(transaction: nil, **options) transaction ||= Transaction.new(**options, sampled: true) # TODO: Add sampling logic diff --git a/sentry-ruby/lib/sentry/scope.rb b/sentry-ruby/lib/sentry/scope.rb index 16285502a..5fdca3bbb 100644 --- a/sentry-ruby/lib/sentry/scope.rb +++ b/sentry-ruby/lib/sentry/scope.rb @@ -98,10 +98,6 @@ def set_span(span) @span = span end - def get_span - @span - end - def set_user(user_hash) check_argument_type!(user_hash, Hash) @user = user_hash @@ -146,6 +142,15 @@ def transaction_name @transaction_names.last end + def get_transaction + # transaction will always be the first in the span_recorder + span.span_recorder.spans.first if span + end + + def get_span + span + end + def set_fingerprint(fingerprint) check_argument_type!(fingerprint, Array) @@ -180,6 +185,7 @@ def set_default_value @transaction_names = [] @event_processors = [] @rack_env = {} + @span = nil end class << self diff --git a/sentry-ruby/spec/sentry/hub_spec.rb b/sentry-ruby/spec/sentry/hub_spec.rb index 5b5ee50f4..856ce67e6 100644 --- a/sentry-ruby/spec/sentry/hub_spec.rb +++ b/sentry-ruby/spec/sentry/hub_spec.rb @@ -137,46 +137,6 @@ end end - describe "#start_span" do - it "initializes a new span object" do - span = subject.start_span(op: "foo") - - expect(span).to be_a(Sentry::Span) - expect(span.op).to eq("foo") - end - - context "already has a span in scope" do - let(:span) do - subject.start_span(op: "foo") - end - - before do - subject.current_scope.set_span(span) - end - - it "starts a child span of the current span" do - child_span = subject.start_span(op: "bar") - - expect(child_span.op).to eq("bar") - expect(child_span.parent_span_id).to eq(span.span_id) - end - end - end - - describe "#get_transaction" do - let(:span) do - subject.start_span(op: "foo") - end - - before do - subject.current_scope.set_span(span) - end - - it "gets the transaction/span stored in the current scope" do - expect(subject.get_transaction).to eq(span) - end - end - describe "#with_scope" do it "builds a temporary scope" do inner_event = nil diff --git a/sentry-ruby/spec/sentry/scope_spec.rb b/sentry-ruby/spec/sentry/scope_spec.rb index 7f773f08b..ef1533b8f 100644 --- a/sentry-ruby/spec/sentry/scope_spec.rb +++ b/sentry-ruby/spec/sentry/scope_spec.rb @@ -84,6 +84,7 @@ scope.set_extras({additional_info: "hello"}) scope.set_user({id: 1}) scope.set_transaction_name("WelcomeController#index") + scope.set_span(Sentry::Span.new) scope.set_fingerprint(["foo"]) scope end @@ -102,6 +103,41 @@ expect(subject.user).to eq({}) expect(subject.fingerprint).to eq([]) expect(subject.transaction_names).to eq([]) + expect(subject.span).to eq(nil) + end + end + + describe "#get_transaction & #get_span" do + let(:transaction) do + Sentry::Transaction.new(op: "parent") + end + + context "with span in the scope" do + let(:span) do + transaction.start_child(op: "child") + end + + before do + subject.set_span(span) + end + + it "gets the span from the scope" do + expect(subject.get_span).to eq(span) + end + + it "gets the transaction from the span recorder" do + expect(subject.get_transaction).to eq(transaction) + end + end + + context "without span in the scope" do + it "returns nil" do + expect(subject.get_transaction).to eq(nil) + end + + it "returns nil" do + expect(subject.get_span).to eq(nil) + end end end diff --git a/sentry-ruby/spec/sentry_spec.rb b/sentry-ruby/spec/sentry_spec.rb index 0b988f9ec..f6e39dcc1 100644 --- a/sentry-ruby/spec/sentry_spec.rb +++ b/sentry-ruby/spec/sentry_spec.rb @@ -81,20 +81,6 @@ end end - describe ".get_transaction" do - let(:transaction) { described_class.start_transaction } - - before do - described_class.configure_scope do |scope| - scope.set_span(transaction) - end - end - - it "gets the current transaction" do - expect(described_class.get_transaction).to eq(transaction) - end - end - describe ".capture_message" do it "sends the message via current hub" do expect(described_class.get_current_hub).to receive(:capture_message).with("Test", tags: { foo: "baz" })