Skip to content

Commit

Permalink
#70 add "level" tag for generic EF Core events
Browse files Browse the repository at this point in the history
  • Loading branch information
cwe1ss committed Dec 12, 2020
1 parent 423d556 commit e5b0c8a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,20 @@ protected override void HandleEvent(string eventName, object untypedArg)
break;

default:
HandleUnknownEvent(eventName, untypedArg);
break;
{
Dictionary<string, string> tags = null;
if (untypedArg is EventData eventArgs)
{
tags = new Dictionary<string, string>
{
{ "level", eventArgs.LogLevel.ToString() },
};
}

HandleUnknownEvent(eventName, untypedArg, tags);
break;
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<KeyValuePair<string, string>> tags = null)
{
_genericEventProcessor?.ProcessEvent(eventName, untypedArg);
_genericEventProcessor?.ProcessEvent(eventName, untypedArg, tags);
}

protected void DisposeActiveScope(bool isScopeRequired, Exception exception = null)
Expand Down
30 changes: 23 additions & 7 deletions src/OpenTracing.Contrib.NetCore/Internal/GenericEventProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@ 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<KeyValuePair<string, string>> 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))
{
HandleActivityStop(eventName, activity);
}
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<KeyValuePair<string, string>> tags)
{
ISpanBuilder spanBuilder = _tracer.BuildSpan(activity.OperationName)
.WithTag(Tags.Component, _listenerName);
Expand All @@ -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();
}

Expand All @@ -66,28 +74,36 @@ private void HandleActivityStop(string eventName, Activity activity)
}
}

private void HandleRegularEvent(string eventName, object untypedArg)
private void HandleRegularEvent(string eventName, object untypedArg, IEnumerable<KeyValuePair<string, string>> tags)
{
var span = _tracer.ActiveSpan;

if (span != null)
{
span.Log(GetLogFields(eventName, untypedArg));
span.Log(GetLogFields(eventName, untypedArg, tags));
}
else if (_isLogLevelTraceEnabled)
{
_logger.LogTrace("No ActiveSpan. Event: {ListenerName}/{Event}", _listenerName, eventName);
}
}

private Dictionary<string, object> GetLogFields(string eventName, object arg)
private Dictionary<string, object> GetLogFields(string eventName, object arg, IEnumerable<KeyValuePair<string, string>> tags)
{
var fields = new Dictionary<string, object>
{
{ LogFields.Event, eventName },
{ 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)
Expand Down

0 comments on commit e5b0c8a

Please sign in to comment.