Created
October 25, 2019 10:45
-
-
Save donaldgray/aa0dafb870801c4a62c99796c5d7c438 to your computer and use it in GitHub Desktop.
Class for handling MassTransit errors and sending faults to NewRelic.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
using MassTransit; | |
using NewRelic.Api.Agent; | |
/// <summary> | |
/// <see cref="IReceiveObserver"/> implementation that sends faults to NewRelic. | |
/// </summary> | |
public class NewRelicReceiveObserver : IReceiveObserver | |
{ | |
public Task PreReceive(ReceiveContext context) => Task.CompletedTask; | |
public Task PostReceive(ReceiveContext context) => Task.CompletedTask; | |
public Task PostConsume<T>(ConsumeContext<T> context, TimeSpan duration, string consumerType) | |
where T : class | |
=> Task.CompletedTask; | |
public Task ConsumeFault<T>(ConsumeContext<T> context, TimeSpan duration, string consumerType, Exception exception) | |
where T : class | |
=> SendErrorToNewRelic(exception, nameof(ConsumeFault), consumerType); | |
public Task ReceiveFault(ReceiveContext context, Exception exception) | |
=> SendErrorToNewRelic(exception, nameof(ReceiveFault)); | |
private static Task SendErrorToNewRelic(Exception exception, string faultType, string consumerType = null) | |
{ | |
var newRelicParams = new Dictionary<string, string> | |
{ | |
["source"] = "MassTransit", | |
["faultType"] = faultType | |
}; | |
if (consumerType != null) | |
{ | |
newRelicParams.Add("consumerType", consumerType); | |
} | |
NewRelic.NoticeError(exception, newRelicParams); | |
return Task.CompletedTask; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment