Skip to content

Commit fa4d878

Browse files
authored
feat: add request_id to notices (#619)
Closes #616
1 parent 753f995 commit fa4d878

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

lib/honeybadger/agent.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ def notify(exception_or_opts = nil, opts = {}, **kwargs)
149149
opts[:rack_env] ||= context_manager.get_rack_env
150150
opts[:global_context] ||= context_manager.get_context
151151
opts[:breadcrumbs] ||= breadcrumbs.dup
152+
opts[:request_id] ||= context_manager.get_request_id
152153

153154
notice = Notice.new(config, opts)
154155

lib/honeybadger/notice.rb

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ def tags=(tags)
144144
# Custom details data
145145
attr_accessor :details
146146

147+
# The ID of the request which caused this notice.
148+
attr_accessor :request_id
149+
147150
# The parsed exception backtrace. Lines in this backtrace that are from installed gems
148151
# have the base path for gem installs replaced by "[GEM_ROOT]", while those in the project
149152
# have "[PROJECT_ROOT]".
@@ -213,13 +216,14 @@ def initialize(config, opts = {})
213216
self.api_key = opts[:api_key] || config[:api_key]
214217
self.tags = construct_tags(opts[:tags]) | construct_tags(context[:tags])
215218

216-
self.url = opts[:url] || request_hash[:url] || nil
217-
self.action = opts[:action] || request_hash[:action] || nil
218-
self.component = opts[:controller] || opts[:component] || request_hash[:component] || nil
219-
self.params = opts[:parameters] || opts[:params] || request_hash[:params] || {}
220-
self.session = opts[:session] || request_hash[:session] || {}
221-
self.cgi_data = opts[:cgi_data] || request_hash[:cgi_data] || {}
222-
self.details = opts[:details] || {}
219+
self.url = opts[:url] || request_hash[:url] || nil
220+
self.action = opts[:action] || request_hash[:action] || nil
221+
self.component = opts[:controller] || opts[:component] || request_hash[:component] || nil
222+
self.params = opts[:parameters] || opts[:params] || request_hash[:params] || {}
223+
self.session = opts[:session] || request_hash[:session] || {}
224+
self.cgi_data = opts[:cgi_data] || request_hash[:cgi_data] || {}
225+
self.details = opts[:details] || {}
226+
self.request_id = opts[:request_id] || nil
223227

224228
self.session = opts[:session][:data] if opts[:session] && opts[:session][:data]
225229

@@ -261,6 +265,9 @@ def as_json(*args)
261265
stats: stats,
262266
time: now,
263267
pid: pid
268+
},
269+
correlation_context: {
270+
request_id: s(request_id)
264271
}
265272
}
266273
end

spec/unit/honeybadger/notice_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ def assert_array_starts_with(expected, actual)
9393
expect(notice.url).to eq url
9494
end
9595

96+
it "accepts a request_id" do
97+
expect(build_notice(request_id: 'abc').request_id).to eq 'abc'
98+
end
99+
96100
it "sets the error class from an exception or hash" do
97101
assert_accepts_exception_attribute :error_class do |exception|
98102
exception.class.name
@@ -634,6 +638,10 @@ def exception.cause
634638
expect(metadata[:password]).to eq "[FILTERED]"
635639
expect(metadata[:deep]).to eq "[DEPTH]"
636640
end
641+
642+
it "includes the request_id" do
643+
expect(build_notice(request_id: "abc").as_json[:correlation_context][:request_id]).to eq "abc"
644+
end
637645
end
638646

639647
describe 'public attributes' do

0 commit comments

Comments
 (0)