Skip to content

Commit

Permalink
Add request_id to context manager
Browse files Browse the repository at this point in the history
  • Loading branch information
roelbondoc committed May 22, 2024
1 parent 94cd569 commit 200154d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
6 changes: 6 additions & 0 deletions lib/honeybadger/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,10 @@ def event(event_type, payload = {})
merged.merge!(Hash(event_type))
end

if (request_id = context_manager.get_request_id)
merged.merge!(request_id: request_id)
end

merged.merge!(Hash(payload))

events_worker.push(merged)
Expand Down Expand Up @@ -478,9 +482,11 @@ def collect(collector)
# @api private
def with_rack_env(rack_env, &block)
context_manager.set_rack_env(rack_env)
context_manager.set_request_id(rack_env["action_dispatch.request_id"] || SecureRandom.uuid)
yield
ensure
context_manager.set_rack_env(nil)
context_manager.set_request_id(nil)
end

# @api private
Expand Down
11 changes: 10 additions & 1 deletion lib/honeybadger/context_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,24 @@ def get_rack_env
@mutex.synchronize { @rack_env }
end

def set_request_id(request_id)
@mutex.synchronize { @request_id = request_id }
end

def get_request_id
@mutex.synchronize { @request_id }
end

private

attr_accessor :custom, :rack_env
attr_accessor :custom, :rack_env, :request_id

def _initialize
@mutex.synchronize do
@global_context = nil
@local_context = nil
@rack_env = nil
@request_id = nil
end
end
end
Expand Down
4 changes: 1 addition & 3 deletions lib/honeybadger/notification_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ def format_payload(payload)

class ActionControllerSubscriber < NotificationSubscriber
def format_payload(payload)
{
request_id: payload[:request]&.request_id || SecureRandom.uuid,
}.merge(payload.except(:headers, :request, :response))
payload.except(:headers, :request, :response)
end
end

Expand Down

0 comments on commit 200154d

Please sign in to comment.