Skip to content

Commit 73a59f5

Browse files
authored
Let the instrumenter log exceptions too (#746)
1 parent ed8bd1f commit 73a59f5

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/http/features/instrumentation.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ module Features
1919
# and `finish` so the duration of the request can be calculated.
2020
#
2121
class Instrumentation < Feature
22-
attr_reader :instrumenter, :name
22+
attr_reader :instrumenter, :name, :error_name
2323

2424
def initialize(instrumenter: NullInstrumenter.new, namespace: "http")
2525
@instrumenter = instrumenter
2626
@name = "request.#{namespace}"
27+
@error_name = "error.#{namespace}"
2728
end
2829

2930
def wrap_request(request)
@@ -39,6 +40,10 @@ def wrap_response(response)
3940
response
4041
end
4142

43+
def on_error(request, error)
44+
instrumenter.instrument(error_name, :request => request, :error => error)
45+
end
46+
4247
HTTP::Options.register_feature(:instrumentation, self)
4348

4449
class NullInstrumenter

spec/lib/http/features/instrumentation_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,23 @@ def finish(_name, payload)
5959
expect(instrumenter.output[:finish]).to eq(:response => response)
6060
end
6161
end
62+
63+
describe "logging errors" do
64+
let(:request) do
65+
HTTP::Request.new(
66+
:verb => :post,
67+
:uri => "https://example.com/",
68+
:headers => {:accept => "application/json"},
69+
:body => '{"hello": "world!"}'
70+
)
71+
end
72+
73+
let(:error) { HTTP::TimeoutError.new }
74+
75+
it "should log the error" do
76+
feature.on_error(request, error)
77+
78+
expect(instrumenter.output[:finish]).to eq(:request => request, :error => error)
79+
end
80+
end
6281
end

0 commit comments

Comments
 (0)