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

Add ThreadsInterface #1178

Merged
merged 2 commits into from
Jan 15, 2021
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
4 changes: 4 additions & 0 deletions sentry-ruby/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased (4.2.0)

- Add ThreadsInterface [#1178](https://github.com/getsentry/sentry-ruby/pull/1178)

## 4.1.4

- Fix headers serialization for sentry-ruby [#1197](https://github.com/getsentry/sentry-ruby/pull/1197) (by @moofkit)
Expand Down
5 changes: 4 additions & 1 deletion sentry-ruby/lib/sentry/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,15 @@ def event_from_exception(exception, hint = {})

Event.new(configuration: configuration, integration_meta: integration_meta).tap do |event|
event.add_exception_interface(exception)
event.add_threads_interface(crashed: true)
end
end

def event_from_message(message, hint = {})
integration_meta = Sentry.integrations[hint[:integration]]
Event.new(configuration: configuration, integration_meta: integration_meta, message: message)
event = Event.new(configuration: configuration, integration_meta: integration_meta, message: message)
event.add_threads_interface(backtrace: caller)
event
end

def event_from_transaction(transaction)
Expand Down
8 changes: 7 additions & 1 deletion sentry-ruby/lib/sentry/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Event
)

attr_accessor(*ATTRIBUTES)
attr_reader :configuration, :request, :exception, :stacktrace
attr_reader :configuration, :request, :exception, :stacktrace, :threads

def initialize(configuration:, integration_meta: nil, message: nil)
# this needs to go first because some setters rely on configuration
Expand Down Expand Up @@ -100,6 +100,7 @@ def to_hash
data[:stacktrace] = stacktrace.to_hash if stacktrace
data[:request] = request.to_hash if request
data[:exception] = exception.to_hash if exception
data[:threads] = threads.to_hash if threads

data
end
Expand All @@ -112,6 +113,11 @@ def add_request_interface(env)
@request = Sentry::RequestInterface.from_rack(env)
end

def add_threads_interface(backtrace: nil, **options)
@threads = ThreadsInterface.new(**options)
@threads.stacktrace = initialize_stacktrace_interface(backtrace) if backtrace
end

def add_exception_interface(exc)
if exc.respond_to?(:sentry_context)
@extra.merge!(exc.sentry_context)
Expand Down
1 change: 1 addition & 0 deletions sentry-ruby/lib/sentry/interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ def to_hash
require "sentry/interfaces/request"
require "sentry/interfaces/single_exception"
require "sentry/interfaces/stacktrace"
require "sentry/interfaces/threads"
26 changes: 26 additions & 0 deletions sentry-ruby/lib/sentry/interfaces/threads.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Sentry
class ThreadsInterface
attr_accessor :stacktrace

def initialize(crashed: false)
@id = Thread.current.object_id
@name = Thread.current.name
@current = true
@crashed = crashed
end

def to_hash
{
values: [
{
id: @id,
name: @name,
crashed: @crashed,
current: @current,
stacktrace: @stacktrace&.to_hash
}
]
}
end
end
end
36 changes: 36 additions & 0 deletions sentry-ruby/spec/sentry/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,24 @@ def sentry_context
expect(hash[:message]).to eq(message)
expect(hash[:level]).to eq(:error)
end

it "inserts threads interface to the event" do
event = nil

t = Thread.new do
event = subject.event_from_message(message)
end

t.name = "Thread 1"
t.join
hash = event.to_hash

thread = hash[:threads][:values][0]
expect(thread[:id]).to eq(t.object_id)
expect(thread[:name]).to eq("Thread 1")
expect(thread[:crashed]).to eq(false)
expect(thread[:stacktrace]).not_to be_empty
end
end

describe "#event_from_transaction" do
Expand Down Expand Up @@ -296,6 +314,24 @@ def sentry_context
expect(hash[:exception][:values][0][:value]).to eq(message)
end

it "sets threads interface without stacktrace" do
event = nil

t = Thread.new do
event = subject.event_from_exception(exception)
end

t.name = "Thread 1"
t.join

thread = event.to_hash[:threads][:values][0]

expect(thread[:id]).to eq(t.object_id)
expect(thread[:name]).to eq("Thread 1")
expect(thread[:crashed]).to eq(true)
expect(thread[:stacktrace]).to be_nil
end

it 'has level ERROR' do
expect(hash[:level]).to eq(:error)
end
Expand Down