CityBaseInc / airbrake_client

Airbrake client to report errors and exceptions to Airbrake.io.
Other
0 stars 1 forks source link

Set session from Logger metadata #29

Closed jdfrens closed 5 months ago

jdfrens commented 5 months ago

Description

The basic use case we wanted to solve was to add a request_id in an Airbrake report. This makes the most sense in the session field for the report. We also realized that we're storing the request_id in the Logger metadata, and we're storing a lot of useful information in the Logger metadata. Let's face it: if it's in the Logger metadata, it's good for debugging. So it seems natural to also include it in an Airbrake report.

Adding Logger metadata to the session is optional. You need to set session: :include_logger_metadata in the :airbrake_client config.

All values from Logger.metadata() are added to the session field. The map for :session in the opts passed to a report/? function overrides the Logger metadata.

Testing

I used this code to manually test:

defmodule ReportTest do
  def send do
    Logger.metadata(foo: 5, bar: "yes!")

    Airbrake.report(
      [type: "AirbrakeClientTester", message: "testing Airbrake.report/2 with maximal options"],
      context: %{foo: 5},
      params: %{foo: 55},
      session: %{foo: 555},
      env: %{foo: 5555}
    ) |> IO.inspect()
  end
end

ReportTest.send()

Ran it like this: mix compile && iex -S mix run manual_test.exs

Screenshots:

Screenshot 2024-04-01 at 1 38 06 PM

Screenshot 2024-04-01 at 1 38 37 PM