Description
Describe the suggested improvement
Is your improvement related to a problem? Please describe.
In our handlers, we typically do not catch exceptions. We let NServiceBus handle them (to retry) and log them via OpenTelemetry.
For some types of exceptions (like SqlException
and ApiException<ProblemDetails>
), we want to log additional details from the exception. Ideally, we would like to add these as tags to the exception that NServiceBus is logging.
Describe the suggested solution
The pattern among other OpenTelemetry instrumentation providers seems to be to provide an Enrich
API, which allows applications to access the objects being logged and enrich the associated Activity
with additional information. The HTTP Instrumentation library is one example.
For NServiceBus, I envision something similar:
endpointConfiguration.EnableOpenTelemetry(options => {
options.EnrichWithException = (activity, exception) =>
{
if (exception is SqlException sqlex)
activity.SetTag("exception.number", sqlex.Number);
};
});
Describe alternatives you've considered
Catch/Log/Rethrow
In our handlers, catch the exceptions we are interested in, log the properties of interest (but not the exception), and rethrow for NServiceBus.
public async Task Handle(MyEvent message, IMessageHandlerContext context)
{
try
{
...
}
catch (SqlException ex)
{
Activity.Current?.SetTag("exception.number", ex.Number);
throw;
}
}
This gives us the desired outcome, but adds a fair amount of boilerplate code to our handlers.
OpenTelemetry Processor
I looked into creating a custom ActivityProcessor. This gives you a hook to modify an Activity
when it starts and when it ends. Unfortunately, at this level we don't have access to the raw exception object - it's already been converted to event tags on the activity.
Additional Context
No response