Way to know in behavior if message is a failed / poison message?

We have written a custom pipeline behavior to plug into a telemetry platform (DataDog). Currently every message that has an exception marks the span as failed. But what we really want to do is only mark the span as failed if its about to go to the error queue. Is there a way to do that?

Here is the gist of what the behavior does

public override async Task Invoke(ITransportReceiveContext context, Func<Task> next)
{
    using (var scope = Tracer.Instance.StartActive("nservicebus.receive_message")
    {
        scope.Span.ResourceName = GetEnclosedMessageType(context);
        scope.Span.Type = "nservicebus";

        try
        {
            await next().ConfigureAwait(false);

        }
        catch (Exception ex)
        {
            scope.Span.SetException(ex); // we only want to do this if it is failed
            throw;
        }
    }
}

Hi Joe,

Just to make sure I understand, the code you have now will mark the span as failed when the message fails (i.e. which I believe is before the recoverability process kicks in as per Customizing Error Handling • NServiceBus • Particular Docs). But you’d like to mark it as failed after all the recoverability attempts have failed just as it’s about to enter the error queue?

Correct. If possible I don’t want to mark retries as failed. Only the last retry that is now going to the error queue. By marking every retries as failed it makes things like error rates and reporting artificially high

I’m not aware of any direct way of detecting whether a message is going to the error queue, at least in NServiceBus 7. NServiceBus 8 (still in beta) exposes an IRecoverabilityContext which indicates the action being performed, such as MoveToErrorQueue. Barring that, you have access to the NServiceBus.Retries header which tells you how many delayed retries have happened but I don’t think there’s a way to compare that to the number of delayed retries configured for the endpoint.

Joe,

This may be an option as well: Error notifications • NServiceBus • Particular Docs