Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement close API that cleans up state and reverses init logic #1844

Merged
merged 8 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
9 changes: 9 additions & 0 deletions sentry-ruby/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

Individual gem's changelog has been deprecated. Please check the [project changelog](https://github.com/getsentry/sentry-ruby/blob/master/CHANGELOG.md).

## Unreleased

### Features
sl0thentr0py marked this conversation as resolved.
Show resolved Hide resolved

- Expose `:values` in `ExceptionInterface`, so that it can be accessed in `before_send` under `event.exception.values` [#1843](https://github.com/getsentry/sentry-ruby/pull/1843)
- Add top level `Sentry.close` API [#1844](https://github.com/getsentry/sentry-ruby/pull/1844)
- Cleans up SDK state and sets it to uninitialized
- No-ops all SDK APIs and also disables the transport layer, so nothing will be sent to Sentry after closing the SDK

## 4.4.2

- Fix NoMethodError when SDK's dsn is nil [#1433](https://github.com/getsentry/sentry-ruby/pull/1433)
Expand Down
28 changes: 26 additions & 2 deletions sentry-ruby/lib/sentry-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ def exception_locals_tp
end

# @!attribute [rw] background_worker
# @return [BackgroundWorker]
# @return [BackgroundWorker, nil]
attr_accessor :background_worker

# @!attribute [r] session_flusher
# @return [SessionFlusher]
# @return [SessionFlusher, nil]
attr_reader :session_flusher

##### Patch Registration #####
Expand Down Expand Up @@ -215,8 +215,31 @@ def init(&block)

at_exit do
@session_flusher&.kill
sl0thentr0py marked this conversation as resolved.
Show resolved Hide resolved
@background_worker&.shutdown
end
end

# Flushes pending events and cleans up SDK state.
# SDK will stop sending events and all top-level APIs will be no-ops after this.
#
# @return [void]
def close
if @background_worker
@background_worker.shutdown
@background_worker = nil
end

if @session_flusher
@session_flusher.kill
@session_flusher = nil
end

if configuration.capture_exception_frame_locals
exception_locals_tp.disable
end

@main_hub = nil
Thread.current.thread_variable_set(THREAD_LOCAL, nil)
end

# Returns true if the SDK is initialized.
Expand Down Expand Up @@ -287,6 +310,7 @@ def get_current_scope
#
# @return [void]
def clone_hub_to_current_thread
return unless initialized?
Thread.current.thread_variable_set(THREAD_LOCAL, get_main_hub.clone)
end

Expand Down
67 changes: 67 additions & 0 deletions sentry-ruby/spec/sentry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -784,4 +784,71 @@
end
end

describe ".close" do
context "when closing initialized SDK" do
it "not initialized?" do
expect(described_class.initialized?).to eq(true)
described_class.close
expect(described_class.initialized?).to eq(false)
end

it "removes main hub" do
expect(described_class.get_main_hub).to be_a(Sentry::Hub)
described_class.close
expect(described_class.get_main_hub).to eq(nil)
end

it "removes thread local" do
expect(Thread.current.thread_variable_get(described_class::THREAD_LOCAL)).to be_a(Sentry::Hub)
described_class.close
expect(Thread.current.thread_variable_get(described_class::THREAD_LOCAL)).to eq(nil)

end

it "calls background worker shutdown" do
expect(described_class.background_worker).to receive(:shutdown)
described_class.close
expect(described_class.background_worker).to eq(nil)
end

it "kills session flusher" do
expect(described_class.session_flusher).to receive(:kill)
described_class.close
expect(described_class.session_flusher).to eq(nil)
end

it "disables Tracepoint" do
perform_basic_setup do |config|
config.capture_exception_frame_locals = true
end

expect(described_class.exception_locals_tp).to receive(:disable).and_call_original
described_class.close
end
end

it "can reinitialize closed SDK" do
perform_basic_setup

expect do
described_class.capture_event(event)
end.to change { transport.events.count }.by(1)

described_class.close

expect do
described_class.capture_event(event)
end.to change { transport.events.count }.by(0)

perform_basic_setup

expect(described_class.initialized?).to eq(true)

new_transport = described_class.get_current_client.transport

expect do
described_class.capture_event(event)
end.to change { new_transport.events.count }.by(1)
end
end
end