Created
December 10, 2019 15:05
-
-
Save NooNameR/ccf937bbf0f40d18c6898414cc42cb80 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| public class MongoEventListener | |
| { | |
| private const string MongoDbPrefix = "MongoDB."; | |
| private readonly ConcurrentDictionary<int, Activity> _activities = new ConcurrentDictionary<int, Activity>(); | |
| private readonly DiagnosticSource _diagnosticSource; | |
| private readonly EventFilter _eventFilter; | |
| public MongoEventListener(DiagnosticSource diagnosticSource) | |
| { | |
| _diagnosticSource = diagnosticSource; | |
| _eventFilter = new EventFilter(_diagnosticSource); | |
| } | |
| public void Handle(CommandStartedEvent @event) | |
| { | |
| if (!_eventFilter.IsApproved(@event.CommandName)) | |
| return; | |
| var activity = new Activity($"{MongoDbPrefix}{@event.CommandName}") | |
| .AddTag(Headers.RequestId, @event.RequestId.ToString()) | |
| .AddTag(Headers.ConnectionId, @event.ConnectionId.ToString()) | |
| .AddTag(Headers.Command, @event.Command.ToString()) | |
| .AddTag(Headers.DbInstance, @event.DatabaseNamespace.ToString()) | |
| .AddBaggage(Headers.OperationId, @event.OperationId.ToString()) | |
| .SetStartTime(DateTime.UtcNow); | |
| _activities.TryAdd(@event.RequestId, activity); | |
| } | |
| public void Handle(CommandSucceededEvent @event) | |
| { | |
| if (!_eventFilter.IsApproved(@event.CommandName) || !_activities.TryRemove(@event.RequestId, out var activity)) | |
| return; | |
| _diagnosticSource.StartActivity(activity, @event); | |
| activity.AddTag(Headers.Reply, @event.Reply.ToString()) | |
| .AddTag(Headers.Duration, @event.Duration.ToString()) | |
| .SetEndTime(DateTime.UtcNow); | |
| _diagnosticSource.StopActivity(activity, @event); | |
| } | |
| public void Handle(CommandFailedEvent @event) | |
| { | |
| if (!_eventFilter.IsApproved(@event.CommandName) || !_activities.TryRemove(@event.RequestId, out var activity)) | |
| return; | |
| _diagnosticSource.StartActivity(activity, @event); | |
| activity.AddTag(Headers.ErrorMessage, @event.Failure.Message) | |
| .AddTag(Headers.ErrorType, @event.Failure.GetType().FullName) | |
| .AddTag(Headers.StackTrace, @event.Failure.StackTrace) | |
| .SetEndTime(_clock.UtcNow); | |
| _diagnosticSource.StopActivity(activity, @event); | |
| } | |
| private class Headers | |
| { | |
| public const string RequestId = "Request-Id"; | |
| public const string ConnectionId = "Connection-Id"; | |
| public const string DbInstance = "DB-Instance"; | |
| public const string Command = "Command"; | |
| public const string OperationId = "Operation-Id"; | |
| public const string Duration = "Duration"; | |
| public const string Reply = "Reply"; | |
| public const string ErrorMessage = "Exception-Message"; | |
| public const string StackTrace = "Exception-StackTrace"; | |
| public const string ErrorType = "Exception-Type"; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment