Closed jdfrens closed 3 years ago
I was able to confirm that the missing fields from the Airbrake.Payload
struct is the problem when deriving the encoders. It was easiest for me to tweak the encoder, but I suspect the better solution is to add the fields to the struct.
I ran some experiments with an app that uses airbrake_client
version 0.8.0. I ended up with this hacked encoder:
impl Jason.Encoder, for: Airbrake.Payload do
def encode(value, opts) do
Jason.Encode.map(Map.take(value, [:apiKey, :notifier, :errors, :context, :params, :session, :environment]), opts)
end
end
From the iex
console, I generated this report:
Airbrake.report(
[type: "TestingError", message: "Testing Airbrake.report/2"],
context: %{
foo: "bar",
now: "America/Chicago" |> DateTime.now!() |> inspect()
},
params: %{foo: 6},
session: %{foo: 9},
env: %{foo: 10}
)
Now I see the extended options in Airbrake.io:
If you use
jason
instead ofpoison
for JSON encoding, you have to derive a serialization forAirbrake.Payload
. This is not enough:This is because
Airbrake.Payload
defines only a few fields in its struct:https://github.com/CityBaseInc/airbrake_client/blob/master/lib/airbrake/payload.ex#L10
However, the "enhanced options" for
Airbrake.report/2
include optional fields:context
,:params
,:session
, and:env
. Since they are not part of theAirbrake.Payload
struct, derivingJason.Encoder
ignores those fields, and you get anemic reports in Airbrake.io.Here's how I verified the problem in an
iex
session:Adding these extra fields to the struct seems like a good idea, but I suspect then the fields will always be sent to Airbrake.io, set to
nil
if a field hasn't been assigned a value. This seems like it could lead to a lot of noise in the reports. So just adding the fields to the struct will not be sufficient. (This might be a very easy problem to solve with options provided byjason
andpoison
.)Somewhat separately, it would be great if
airbrake_client
derived an encoding forAirbrake.Payload
itself. This was a good idea to begin with (rather than forcing anyone using the library to come up with their own encoded). But if the encoding is more involved than just callingProtocol.derive/2
, then for easy-of-use this library must define the encoding, and it might as well be done fixing the missing struct fields.