From e5b0c8a6fc1321ff87f558b69c5ad1a95912acc3 Mon Sep 17 00:00:00 2001 From: Christian Weiss Date: Sat, 12 Dec 2020 09:28:49 +0100 Subject: [PATCH] #70 add "level" tag for generic EF Core events --- .../EntityFrameworkCoreDiagnostics.cs | 16 ++++++++-- .../Internal/DiagnosticEventObserver.cs | 4 +-- .../Internal/GenericEventProcessor.cs | 30 ++++++++++++++----- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/OpenTracing.Contrib.NetCore/EntityFrameworkCore/EntityFrameworkCoreDiagnostics.cs b/src/OpenTracing.Contrib.NetCore/EntityFrameworkCore/EntityFrameworkCoreDiagnostics.cs index b524b20..61bb992 100644 --- a/src/OpenTracing.Contrib.NetCore/EntityFrameworkCore/EntityFrameworkCoreDiagnostics.cs +++ b/src/OpenTracing.Contrib.NetCore/EntityFrameworkCore/EntityFrameworkCoreDiagnostics.cs @@ -108,8 +108,20 @@ protected override void HandleEvent(string eventName, object untypedArg) break; default: - HandleUnknownEvent(eventName, untypedArg); - break; + { + Dictionary tags = null; + if (untypedArg is EventData eventArgs) + { + tags = new Dictionary + { + { "level", eventArgs.LogLevel.ToString() }, + }; + } + + HandleUnknownEvent(eventName, untypedArg, tags); + break; + } + } } diff --git a/src/OpenTracing.Contrib.NetCore/Internal/DiagnosticEventObserver.cs b/src/OpenTracing.Contrib.NetCore/Internal/DiagnosticEventObserver.cs index f32cf07..7b240c9 100644 --- a/src/OpenTracing.Contrib.NetCore/Internal/DiagnosticEventObserver.cs +++ b/src/OpenTracing.Contrib.NetCore/Internal/DiagnosticEventObserver.cs @@ -87,9 +87,9 @@ private bool IsEnabled(string eventName) protected abstract void HandleEvent(string eventName, object untypedArg); - protected void HandleUnknownEvent(string eventName, object untypedArg) + protected void HandleUnknownEvent(string eventName, object untypedArg, IEnumerable> tags = null) { - _genericEventProcessor?.ProcessEvent(eventName, untypedArg); + _genericEventProcessor?.ProcessEvent(eventName, untypedArg, tags); } protected void DisposeActiveScope(bool isScopeRequired, Exception exception = null) diff --git a/src/OpenTracing.Contrib.NetCore/Internal/GenericEventProcessor.cs b/src/OpenTracing.Contrib.NetCore/Internal/GenericEventProcessor.cs index 5438213..bb9058e 100644 --- a/src/OpenTracing.Contrib.NetCore/Internal/GenericEventProcessor.cs +++ b/src/OpenTracing.Contrib.NetCore/Internal/GenericEventProcessor.cs @@ -22,13 +22,13 @@ public GenericEventProcessor(string listenerName, ITracer tracer, ILogger logger _isLogLevelTraceEnabled = _logger.IsEnabled(LogLevel.Trace); } - public void ProcessEvent(string eventName, object untypedArg) + public void ProcessEvent(string eventName, object untypedArg, IEnumerable> tags = null) { Activity activity = Activity.Current; if (activity != null && eventName.EndsWith(".Start", StringComparison.Ordinal)) { - HandleActivityStart(eventName, activity, untypedArg); + HandleActivityStart(eventName, activity, untypedArg, tags); } else if (activity != null && eventName.EndsWith(".Stop", StringComparison.Ordinal)) { @@ -36,11 +36,11 @@ public void ProcessEvent(string eventName, object untypedArg) } else { - HandleRegularEvent(eventName, untypedArg); + HandleRegularEvent(eventName, untypedArg, tags); } } - private void HandleActivityStart(string eventName, Activity activity, object untypedArg) + private void HandleActivityStart(string eventName, Activity activity, object untypedArg, IEnumerable> tags) { ISpanBuilder spanBuilder = _tracer.BuildSpan(activity.OperationName) .WithTag(Tags.Component, _listenerName); @@ -50,6 +50,14 @@ private void HandleActivityStart(string eventName, Activity activity, object unt spanBuilder.WithTag(tag.Key, tag.Value); } + if (tags != null) + { + foreach (var tag in tags) + { + spanBuilder.WithTag(tag.Key, tag.Value); + } + } + spanBuilder.StartActive(); } @@ -66,13 +74,13 @@ private void HandleActivityStop(string eventName, Activity activity) } } - private void HandleRegularEvent(string eventName, object untypedArg) + private void HandleRegularEvent(string eventName, object untypedArg, IEnumerable> tags) { var span = _tracer.ActiveSpan; if (span != null) { - span.Log(GetLogFields(eventName, untypedArg)); + span.Log(GetLogFields(eventName, untypedArg, tags)); } else if (_isLogLevelTraceEnabled) { @@ -80,7 +88,7 @@ private void HandleRegularEvent(string eventName, object untypedArg) } } - private Dictionary GetLogFields(string eventName, object arg) + private Dictionary GetLogFields(string eventName, object arg, IEnumerable> tags) { var fields = new Dictionary { @@ -88,6 +96,14 @@ private Dictionary GetLogFields(string eventName, object arg) { Tags.Component.Key, _listenerName } }; + if (tags != null) + { + foreach (var tag in tags) + { + fields[tag.Key] = tag.Value; + } + } + // TODO improve the hell out of this... :) if (arg != null)