Skip to content

Commit

Permalink
Resolves #27 too much logs write by GenericEventProcessor, add option…
Browse files Browse the repository at this point in the history
…s to ignore some logs (#33)
  • Loading branch information
chrishaly authored and austinlparker committed May 28, 2019
1 parent 39d6ada commit ee64533
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ internal sealed class AspNetCoreDiagnostics : DiagnosticListenerObserver

protected override string GetListenerName() => DiagnosticListenerName;

public AspNetCoreDiagnostics(ILoggerFactory loggerFactory, ITracer tracer, IOptions<AspNetCoreDiagnosticOptions> options)
: base(loggerFactory, tracer)
public AspNetCoreDiagnostics(ILoggerFactory loggerFactory, ITracer tracer,
IOptions<AspNetCoreDiagnosticOptions> options, IOptions<GenericEventOptions> genericEventOptions)
: base(loggerFactory, tracer, genericEventOptions.Value)
{
if (options?.Value == null)
throw new ArgumentNullException(nameof(options));
Expand Down
23 changes: 18 additions & 5 deletions src/OpenTracing.Contrib.NetCore/CoreFx/GenericDiagnostics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,26 @@ namespace OpenTracing.Contrib.NetCore.CoreFx
internal sealed class GenericDiagnostics : DiagnosticObserver
{
private readonly GenericDiagnosticOptions _options;
private readonly GenericEventOptions _genericEventOptions;

public GenericDiagnostics(ILoggerFactory loggerFactory, ITracer tracer, IOptions<GenericDiagnosticOptions> options)
public GenericDiagnostics(ILoggerFactory loggerFactory, ITracer tracer, IOptions<GenericDiagnosticOptions> options,
IOptions<GenericEventOptions> genericEventOptions)
: base(loggerFactory, tracer)
{
_options = options?.Value ?? throw new ArgumentNullException(nameof(options));
_genericEventOptions = genericEventOptions.Value;
}

public override IDisposable SubscribeIfMatch(DiagnosticListener diagnosticListener)
{
if (_genericEventOptions.IgnoreAll)
{
return null;
}

if (!_options.IgnoredListenerNames.Contains(diagnosticListener.Name))
{
return new GenericDiagnosticsSubscription(this, diagnosticListener);
return new GenericDiagnosticsSubscription(this, diagnosticListener, _genericEventOptions);
}

return null;
Expand All @@ -40,14 +48,19 @@ private class GenericDiagnosticsSubscription : IObserver<KeyValuePair<string, ob
private readonly IDisposable _subscription;


public GenericDiagnosticsSubscription(GenericDiagnostics subscriber, DiagnosticListener diagnosticListener)
public GenericDiagnosticsSubscription(GenericDiagnostics subscriber, DiagnosticListener diagnosticListener,
GenericEventOptions genericEventOptions)
{
_subscriber = subscriber;
_listenerName = diagnosticListener.Name;

subscriber._options.IgnoredEvents.TryGetValue(diagnosticListener.Name, out _ignoredEvents);

_genericEventProcessor = new GenericEventProcessor(_listenerName, _subscriber.Tracer, subscriber.Logger);
if (!genericEventOptions.IsIgnored(diagnosticListener.Name))
{
_genericEventProcessor = new GenericEventProcessor(_listenerName, _subscriber.Tracer, subscriber.Logger,
genericEventOptions);
}

_subscription = diagnosticListener.Subscribe(this, IsEnabled);
}
Expand Down Expand Up @@ -92,7 +105,7 @@ public void OnNext(KeyValuePair<string, object> value)
if (!IsEnabled(eventName))
return;

_genericEventProcessor.ProcessEvent(eventName, untypedArg);
_genericEventProcessor?.ProcessEvent(eventName, untypedArg);
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OpenTracing.Contrib.NetCore.Configuration;
using OpenTracing.Contrib.NetCore.Internal;
using OpenTracing.Propagation;
using OpenTracing.Tag;
Expand Down Expand Up @@ -31,8 +32,9 @@ internal sealed class HttpHandlerDiagnostics : DiagnosticListenerObserver

protected override string GetListenerName() => DiagnosticListenerName;

public HttpHandlerDiagnostics(ILoggerFactory loggerFactory, ITracer tracer, IOptions<HttpHandlerDiagnosticOptions> options)
: base(loggerFactory, tracer)
public HttpHandlerDiagnostics(ILoggerFactory loggerFactory, ITracer tracer,
IOptions<HttpHandlerDiagnosticOptions> options, IOptions<GenericEventOptions> genericEventOptions)
: base(loggerFactory, tracer, genericEventOptions.Value)
{
_options = options?.Value ?? throw new ArgumentNullException(nameof(options));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OpenTracing.Contrib.NetCore.Configuration;
using OpenTracing.Contrib.NetCore.Internal;
using OpenTracing.Tag;

Expand All @@ -19,8 +20,9 @@ internal sealed class EntityFrameworkCoreDiagnostics : DiagnosticListenerObserve

protected override string GetListenerName() => DiagnosticListenerName;

public EntityFrameworkCoreDiagnostics(ILoggerFactory loggerFactory, ITracer tracer, IOptions<EntityFrameworkCoreDiagnosticOptions> options)
: base(loggerFactory, tracer)
public EntityFrameworkCoreDiagnostics(ILoggerFactory loggerFactory, ITracer tracer,
IOptions<EntityFrameworkCoreDiagnosticOptions> options, IOptions<GenericEventOptions> genericEventOptions)
: base(loggerFactory, tracer, genericEventOptions.Value)
{
_options = options?.Value ?? throw new ArgumentNullException(nameof(options));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Extensions.Logging;
using OpenTracing.Contrib.NetCore.Configuration;

namespace OpenTracing.Contrib.NetCore.Internal
{
Expand All @@ -15,10 +16,13 @@ internal abstract class DiagnosticListenerObserver : DiagnosticObserver, IObserv
/// </summary>
protected abstract string GetListenerName();

protected DiagnosticListenerObserver(ILoggerFactory loggerFactory, ITracer tracer)
protected DiagnosticListenerObserver(ILoggerFactory loggerFactory, ITracer tracer, GenericEventOptions options)
: base(loggerFactory, tracer)
{
_genericEventProcessor = new GenericEventProcessor(GetListenerName(), Tracer, Logger);
if (!options.IsIgnored(GetListenerName()))
{
_genericEventProcessor = new GenericEventProcessor(GetListenerName(), Tracer, Logger, options);
}
}

public override IDisposable SubscribeIfMatch(DiagnosticListener diagnosticListener)
Expand Down Expand Up @@ -60,7 +64,7 @@ protected virtual bool IsEnabled(string eventName)

protected void ProcessUnhandledEvent(string eventName, object untypedArg)
{
_genericEventProcessor.ProcessEvent(eventName, untypedArg);
_genericEventProcessor?.ProcessEvent(eventName, untypedArg);
}

protected void DisposeActiveScope(bool isScopeRequired, Exception exception = null)
Expand Down
127 changes: 127 additions & 0 deletions src/OpenTracing.Contrib.NetCore/Internal/GenericEventOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
using System;
using System.Collections.Generic;

namespace OpenTracing.Contrib.NetCore.Internal
{
public class GenericEventOptions
{
private bool _hasFilter;
private bool _ignoreAll;
private Dictionary<string, HashSet<string>> _ignoredEvents;
private HashSet<string> _ignoredListeners;

/// <summary>
/// is ignore all events log
/// </summary>
public bool IgnoreAll
{
get => _ignoreAll;
set
{
_ignoreAll = value;
CheckHasFilter();
}
}

/// <summary>
/// listener names which will be ignored for logging
/// </summary>
public HashSet<string> IgnoredListenerNames
{
get => _ignoredListeners;
set
{
_ignoredListeners = value;
CheckHasFilter();
}
}

/// <summary>
/// events which will be ignored for logging. Key is listener name, HashSet item is event name of the listener
/// </summary>
public Dictionary<string, HashSet<string>> IgnoredEvents
{
get => _ignoredEvents;
set
{
_ignoredEvents = value;
CheckHasFilter();
}
}

public void IgnoreListener(string listenerName)
{
if (listenerName == null)
throw new ArgumentNullException(nameof(listenerName));

if (IgnoredListenerNames == null)
IgnoredListenerNames = new HashSet<string>();

IgnoredListenerNames.Add(listenerName);
_hasFilter = true;
}

public void IgnoreEvent(string listenerName, string eventName)
{
if (listenerName == null)
throw new ArgumentNullException(nameof(listenerName));

if (eventName == null)
throw new ArgumentNullException(nameof(eventName));

if (IgnoredListenerNames == null)
IgnoredListenerNames = new HashSet<string>();

if (!IgnoredEvents.TryGetValue(listenerName, out var ignoredListenerEvents))
{
ignoredListenerEvents = new HashSet<string>();
IgnoredEvents.Add(listenerName, ignoredListenerEvents);
}

ignoredListenerEvents.Add(eventName);
_hasFilter = true;
}

/// <summary>
/// usually used in DiagnosticObserver
/// </summary>
/// <param name="listenerName"></param>
/// <returns></returns>
public bool IsIgnored(string listenerName)
{
if (!_hasFilter)
return false;

if (IgnoreAll)
return true;

return IgnoredListenerNames.Count > 0
&& IgnoredListenerNames.Contains(listenerName);
}

public bool IsIgnored(string listenerName, string eventName)
{
if (!_hasFilter)
return false;

if (IgnoreAll)
return true;

return IgnoredEvents.TryGetValue(listenerName, out var set)
&& set.Contains(eventName);
}

private void CheckHasFilter()
{
if (IgnoredListenerNames == null)
IgnoredListenerNames = new HashSet<string>();

if (IgnoredEvents == null)
IgnoredEvents = new Dictionary<string, HashSet<string>>();

_hasFilter = IgnoreAll
|| IgnoredListenerNames.Count > 0
|| IgnoredEvents.Count > 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Extensions.Logging;
using OpenTracing.Contrib.NetCore.Configuration;
using OpenTracing.Tag;

namespace OpenTracing.Contrib.NetCore.Internal
Expand All @@ -12,12 +13,14 @@ internal class GenericEventProcessor
private readonly ITracer _tracer;
private readonly ILogger _logger;
private readonly bool _isLogLevelTraceEnabled;
private readonly GenericEventOptions _options;

public GenericEventProcessor(string listenerName, ITracer tracer, ILogger logger)
public GenericEventProcessor(string listenerName, ITracer tracer, ILogger logger, GenericEventOptions options)
{
_listenerName = listenerName ?? throw new ArgumentNullException(nameof(listenerName));
_tracer = tracer ?? throw new ArgumentNullException(nameof(tracer));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_options = options;

_isLogLevelTraceEnabled = _logger.IsEnabled(LogLevel.Trace);
}
Expand All @@ -26,6 +29,11 @@ public void ProcessEvent(string eventName, object untypedArg)
{
Activity activity = Activity.Current;

if (_options != null && _options.IsIgnored(_listenerName, eventName))
{
return;
}

if (activity != null && eventName.EndsWith(".Start", StringComparison.Ordinal))
{
HandleActivityStart(eventName, activity, untypedArg);
Expand Down

0 comments on commit ee64533

Please sign in to comment.