Skip to content

Commit

Permalink
Changed the duplicate checker to be a plugin!
Browse files Browse the repository at this point in the history
  • Loading branch information
niemyjski committed Apr 2, 2015
1 parent f930245 commit fc9b3ab
Show file tree
Hide file tree
Showing 14 changed files with 22 additions and 65 deletions.
2 changes: 1 addition & 1 deletion Source/Extras/Plugins/ErrorPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Exceptionless.Models;

namespace Exceptionless.Plugins {
[Priority(40)]
[Priority(30)]
public class ErrorPlugin : IEventPlugin {
private readonly IExceptionlessLog _log;

Expand Down
4 changes: 2 additions & 2 deletions Source/Samples/SampleConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ private static void Main() {
Console.CursorVisible = false;
StartDisplayingLogMessages();

ExceptionlessClient.Default.Configuration.UseTraceLogEntriesPlugin();
ExceptionlessClient.Default.Configuration.AddPlugin<SystemUptimePlugin>();
ExceptionlessClient.Default.Configuration.UseFolderStorage("store");
ExceptionlessClient.Default.Configuration.UseLogger(_log);
//ExceptionlessClient.Default.Configuration.SubmissionBatchSize = 1;
Expand All @@ -62,8 +64,6 @@ private static void Main() {
if (false)
SampleApiUsages();

ExceptionlessClient.Default.Configuration.UseTraceLogEntriesPlugin();
ExceptionlessClient.Default.Configuration.AddPlugin<SystemUptimePlugin>();

ExceptionlessClient.Default.Configuration.AddPlugin(ctx => ctx.Event.Data[RandomData.GetWord()] = RandomData.GetWord());
ExceptionlessClient.Default.Configuration.AddPlugin(ctx => ctx.Event.Data[RandomData.GetWord()] = RandomData.GetWord());
Expand Down
4 changes: 0 additions & 4 deletions Source/Shared/Dependency/DependencyResolver.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Exceptionless.Duplicates;
using Exceptionless.Logging;
using Exceptionless.Queue;
using Exceptionless.Serializer;
Expand Down Expand Up @@ -50,9 +49,6 @@ public static void RegisterDefaultServices(IDependencyResolver resolver) {
var lastClientIdManager = new Lazy<ILastReferenceIdManager>(() => resolver.Resolve<DefaultLastReferenceIdManager>());
resolver.Register(typeof(ILastReferenceIdManager), () => lastClientIdManager.Value);

var duplicateChecker = new Lazy<IDuplicateChecker>(() => resolver.Resolve<DefaultDuplicateChecker>());
resolver.Register(typeof(IDuplicateChecker), () => duplicateChecker.Value);

var persistedClientData = new Lazy<PersistedDictionary>(() => new PersistedDictionary("client-data.json", resolver.Resolve<IObjectStorage>(), resolver.Resolve<IJsonSerializer>()));
resolver.Register(typeof(PersistedDictionary), () => persistedClientData.Value);
}
Expand Down
5 changes: 0 additions & 5 deletions Source/Shared/Dependency/DependencyResolverExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Exceptionless.Duplicates;
using Exceptionless.Logging;
using Exceptionless.Queue;
using Exceptionless.Serializer;
Expand Down Expand Up @@ -82,9 +81,5 @@ public static IEnvironmentInfoCollector GetEnvironmentInfoCollector(this IDepend
public static ILastReferenceIdManager GetLastReferenceIdManager(this IDependencyResolver resolver) {
return resolver.Resolve<ILastReferenceIdManager>() ?? resolver.Resolve<DefaultLastReferenceIdManager>();
}

public static IDuplicateChecker GetDuplicateChecker(this IDependencyResolver resolver) {
return resolver.Resolve<IDuplicateChecker>() ?? resolver.Resolve<DefaultDuplicateChecker>();
}
}
}
8 changes: 0 additions & 8 deletions Source/Shared/Duplicates/IDuplicateChecker.cs

This file was deleted.

10 changes: 0 additions & 10 deletions Source/Shared/Duplicates/NoDuplicateChecker.cs

This file was deleted.

4 changes: 1 addition & 3 deletions Source/Shared/Exceptionless.Portable.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@
<Compile Include="Dependency\DependencyResolverExtensions.cs" />
<Compile Include="Dependency\IDependencyResolver.cs" />
<Compile Include="Dependency\TinyIoC.cs" />
<Compile Include="Duplicates\DefaultDuplicateChecker.cs" />
<Compile Include="Duplicates\IDuplicateChecker.cs" />
<Compile Include="Duplicates\NoDuplicateChecker.cs" />
<Compile Include="Plugins\Default\DuplicateCheckerPlugin.cs" />
<Compile Include="Plugins\ContextData.cs" />
<Compile Include="Plugins\Default\ActionPlugin.cs" />
<Compile Include="Plugins\Default\ConfigurationDefaultsPlugin.cs" />
Expand Down
6 changes: 0 additions & 6 deletions Source/Shared/ExceptionlessClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Threading.Tasks;
using Exceptionless.Dependency;
using Exceptionless.Duplicates;
using Exceptionless.Plugins;
using Exceptionless.Logging;
using Exceptionless.Models;
Expand All @@ -15,7 +14,6 @@ public class ExceptionlessClient : IDisposable {
private readonly Lazy<IEventQueue> _queue;
private readonly Lazy<ISubmissionClient> _submissionClient;
private readonly Lazy<ILastReferenceIdManager> _lastReferenceIdManager;
private readonly Lazy<IDuplicateChecker> _duplicateChecker;

public ExceptionlessClient() : this(new ExceptionlessConfiguration(DependencyResolver.CreateDefault())) { }

Expand Down Expand Up @@ -43,7 +41,6 @@ public ExceptionlessClient(ExceptionlessConfiguration configuration) {

_submissionClient = new Lazy<ISubmissionClient>(() => Configuration.Resolver.GetSubmissionClient());
_lastReferenceIdManager = new Lazy<ILastReferenceIdManager>(() => Configuration.Resolver.GetLastReferenceIdManager());
_duplicateChecker = new Lazy<IDuplicateChecker>(() => Configuration.Resolver.GetDuplicateChecker());
}

public ExceptionlessConfiguration Configuration { get; private set; }
Expand Down Expand Up @@ -144,9 +141,6 @@ public void SubmitEvent(Event ev, ContextData pluginContextData = null) {
if (context.Cancel)
return;

if (_duplicateChecker.Value != null && _duplicateChecker.Value.IsDuplicate(ev))
return;

// ensure all required data
if (String.IsNullOrEmpty(ev.Type))
ev.Type = Event.KnownTypes.Log;
Expand Down
3 changes: 1 addition & 2 deletions Source/Shared/Plugins/Default/ActionPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Exceptionless.Dependency;
using Exceptionless.Logging;

namespace Exceptionless.Plugins.Default {
Expand All @@ -14,7 +13,7 @@ public void Run(EventPluginContext context) {
try {
_pluginAction(context);
} catch (Exception ex) {
context.Resolver.GetLog().FormattedError(typeof(ActionPlugin), ex, "An error occurred while running an custom plugin: {0}", ex.Message);
context.Log.FormattedError(typeof(ActionPlugin), ex, "An error occurred while running an custom plugin: {0}", ex.Message);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,28 @@
using System.Collections.Concurrent;
using System.Linq;
using Exceptionless.Logging;
using Exceptionless.Models;
using Exceptionless.Models.Data;

namespace Exceptionless.Duplicates {
public class DefaultDuplicateChecker : IDuplicateChecker {
namespace Exceptionless.Plugins.Default {
[Priority(40)]
public class DuplicateCheckerPlugin : IEventPlugin {
private readonly ConcurrentQueue<Tuple<int, DateTime>> _recentlyProcessedErrors = new ConcurrentQueue<Tuple<int, DateTime>>();
private readonly IExceptionlessLog _log;

public DefaultDuplicateChecker(IExceptionlessLog log) {
_log = log;
}

public bool IsDuplicate(Event ev) {
if (!ev.IsError())
return false;
public void Run(EventPluginContext context) {
if (!context.Event.IsError())
return;

InnerError current = ev.GetError();
InnerError current = context.Event.GetError();
DateTime repeatWindow = DateTime.Now.AddSeconds(-2);

while (current != null) {
int hashCode = current.GetHashCode();

// make sure that we don't process the same error multiple times within 2 seconds.
if (_recentlyProcessedErrors.Any(s => s.Item1 == hashCode && s.Item2 >= repeatWindow)) {
_log.FormattedInfo(typeof(ExceptionlessClient), "Ignoring duplicate error event: hash={0}", hashCode);
return true;
context.Log.FormattedInfo(typeof(ExceptionlessClient), "Ignoring duplicate error event: hash={0}", hashCode);
context.Cancel = true;
return;
}

// add this exception to our list of recent errors that we have processed.
Expand All @@ -40,8 +36,6 @@ public bool IsDuplicate(Event ev) {

current = current.Inner;
}

return false;
}
}
}
}
2 changes: 1 addition & 1 deletion Source/Shared/Plugins/Default/EnvironmentInfoPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void Run(EventPluginContext context) {
info.InstallId = context.Client.Configuration.GetInstallId();
context.Event.Data.Add(Event.KnownDataKeys.EnvironmentInfo, info);
} catch (Exception ex) {
context.Resolver.GetLog().FormattedError(typeof(EnvironmentInfoPlugin), ex, "Error adding environment information: {0}", ex.Message);
context.Log.FormattedError(typeof(EnvironmentInfoPlugin), ex, "Error adding environment information: {0}", ex.Message);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Shared/Plugins/Default/SimpleErrorPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Exceptionless.Models;

namespace Exceptionless.Plugins.Default {
[Priority(40)]
[Priority(30)]
public class SimpleErrorPlugin : IEventPlugin {
public void Run(EventPluginContext context) {
var exception = context.ContextData.GetException();
Expand Down
2 changes: 1 addition & 1 deletion Source/Shared/Plugins/Default/SubmissionMethodPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Exceptionless.Models;

namespace Exceptionless.Plugins.Default {
[Priority(30)]
[Priority(100)]
public class SubmissionMethodPlugin : IEventPlugin {
public void Run(EventPluginContext context) {
string submissionMethod = context.ContextData.GetSubmissionMethod();
Expand Down
7 changes: 3 additions & 4 deletions Source/Shared/Plugins/EventPluginManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Linq;
using Exceptionless.Dependency;
using Exceptionless.Plugins.Default;
using Exceptionless.Logging;

Expand All @@ -10,17 +9,16 @@ public static class EventPluginManager {
/// Called when the event object is created and can be used to add information to the event.
/// </summary>
/// <param name="context">Context information.</param>
/// <param name="ev">Event that was created.</param>
public static void Run(EventPluginContext context) {
foreach (IEventPlugin plugin in context.Client.Configuration.Plugins.Select(e => e.Plugin).ToList()) {
try {
plugin.Run(context);
if (context.Cancel) {
ExceptionlessLogExtensions.FormattedInfo(context.Resolver.GetLog(), plugin.GetType(), "Event submission cancelled by plugin: id={0} type={1}", context.Event.ReferenceId, context.Event.Type);
context.Log.FormattedInfo(plugin.GetType(), "Event submission cancelled by plugin: id={0} type={1}", context.Event.ReferenceId, context.Event.Type);
return;
}
} catch (Exception ex) {
context.Resolver.GetLog().FormattedError(typeof(EventPluginManager), ex, "An error occurred while running {0}.Run(): {1}", plugin.GetType().FullName, ex.Message);
context.Log.FormattedError(typeof(EventPluginManager), ex, "An error occurred while running {0}.Run(): {1}", plugin.GetType().FullName, ex.Message);
}
}
}
Expand All @@ -29,6 +27,7 @@ public static void AddDefaultPlugins(ExceptionlessConfiguration config) {
config.AddPlugin<ConfigurationDefaultsPlugin>();
config.AddPlugin<EnvironmentInfoPlugin>();
config.AddPlugin<SimpleErrorPlugin>();
config.AddPlugin<DuplicateCheckerPlugin>();
config.AddPlugin<SubmissionMethodPlugin>();
}
}
Expand Down

0 comments on commit fc9b3ab

Please sign in to comment.