Skip to content

Commit

Permalink
Add ThreadsInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 committed Jan 8, 2021
1 parent c886fc6 commit bb75304
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 2 deletions.
6 changes: 5 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 All @@ -73,6 +76,7 @@ def event_from_transaction(transaction)
event.contexts.merge!(trace: transaction.get_trace_context)
event.timestamp = transaction.timestamp
event.start_timestamp = transaction.start_timestamp
event.add_threads_interface

finished_spans = transaction.span_recorder.spans.select { |span| span.timestamp && span != transaction }
event.spans = finished_spans.map(&:to_hash)
Expand Down
13 changes: 12 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,16 @@ def add_request_interface(env)
@request = Sentry::RequestInterface.from_rack(env)
end

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

if backtrace
@threads.stacktrace = StacktraceInterface.new.tap do |stacktrace|
stacktrace.frames = stacktrace_interface_from(backtrace)
end
end
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
54 changes: 54 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 All @@ -283,6 +301,24 @@ def sentry_context
expect(event[:spans].count).to eq(1)
expect(event[:spans][0][:op]).to eq("finished child")
end

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

t = Thread.new do
event = subject.event_from_transaction(transaction)
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(false)
expect(thread[:stacktrace]).to be_nil
end
end

describe "#event_from_exception" do
Expand All @@ -296,6 +332,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

0 comments on commit bb75304

Please sign in to comment.