Skip to content

Commit

Permalink
Add tags and namespace options to listen_for_error (#357)
Browse files Browse the repository at this point in the history
`Appsignal.listen_for_error` is the only helper that doesn't support
custom tags and a custom namespace.
This change adds support for the tags and namespace and passes the
values along to `Appsignal.send_error`.

Did not change this spec to use `Appsignal::Transaction#to_h`, because
the change would be a lot more involved as we can't fetch the current
transaction from the current thread. To record a transaction we'd need
to be able to capture the transaction as it's being created.
  • Loading branch information
tombruijn committed Oct 25, 2017
1 parent 84df16c commit bc19d56
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
31 changes: 29 additions & 2 deletions lib/appsignal.rb
Expand Up @@ -217,10 +217,37 @@ def monitor_single_transaction(name, env = {}, &block)
stop("monitor_single_transaction")
end

def listen_for_error
# Listen for an error to occur and send it to AppSignal.
#
# Uses {.send_error} to directly send the error in a separate transaction.
# Does not add the error to the current transaction.
#
# Make sure that AppSignal is integrated in your application beforehand.
# AppSignal won't record errors unless {Config#active?} is `true`.
#
# @example
# # my_app.rb
# # setup AppSignal beforehand
#
# Appsignal.listen_for_error do
# # my code
# raise "foo"
# end
#
# @see Transaction.set_tags
# @see Transaction.set_namespace
# @see .send_error
# @see https://docs.appsignal.com/ruby/instrumentation/integrating-appsignal.html
# AppSignal integration guide
#
# @param tags [Hash, nil]
# @param namespace [String] the namespace for this error.
# @yield yields the given block.
# @return [Object] returns the return value of the given block.
def listen_for_error(tags = nil, namespace = Appsignal::Transaction::HTTP_REQUEST)
yield
rescue Exception => error # rubocop:disable Lint/RescueException
send_error(error)
send_error(error, tags, namespace)
raise error
end
alias :listen_for_exception :listen_for_error
Expand Down
44 changes: 39 additions & 5 deletions spec/lib/appsignal_spec.rb
Expand Up @@ -257,7 +257,7 @@
end

describe ".listen_for_error" do
it "should do nothing" do
it "does not record anyhing" do
error = RuntimeError.new("specific error")
expect do
Appsignal.listen_for_error do
Expand Down Expand Up @@ -779,13 +779,47 @@
end

describe ".listen_for_error" do
it "should call send_error and re-raise" do
expect(Appsignal).to receive(:send_error).with(kind_of(Exception))
it "records the error and re-raise it" do
expect(Appsignal).to receive(:send_error).with(
kind_of(ExampleException),
nil,
Appsignal::Transaction::HTTP_REQUEST
)
expect do
Appsignal.listen_for_error do
raise "I am an exception"
raise ExampleException, "I am an exception"
end
end.to raise_error(RuntimeError, "I am an exception")
end.to raise_error(ExampleException, "I am an exception")
end

context "with tags" do
it "adds tags to the transaction" do
expect(Appsignal).to receive(:send_error).with(
kind_of(ExampleException),
{ "foo" => "bar" },
Appsignal::Transaction::HTTP_REQUEST
)
expect do
Appsignal.listen_for_error("foo" => "bar") do
raise ExampleException, "I am an exception"
end
end.to raise_error(ExampleException, "I am an exception")
end
end

context "with a custom namespace" do
it "adds the namespace to the transaction" do
expect(Appsignal).to receive(:send_error).with(
kind_of(ExampleException),
nil,
"custom_namespace"
)
expect do
Appsignal.listen_for_error(nil, "custom_namespace") do
raise ExampleException, "I am an exception"
end
end.to raise_error(ExampleException, "I am an exception")
end
end
end

Expand Down

0 comments on commit bc19d56

Please sign in to comment.