Skip to content

Commit

Permalink
new BehaviorTracer for the redone instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Aug 25, 2015
1 parent 3bf5b31 commit a8d75f8
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 6 deletions.
37 changes: 37 additions & 0 deletions src/FubuMVC.Core/Diagnostics/Instrumentation/BehaviorTracer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using FubuMVC.Core.Behaviors;
using FubuMVC.Core.Registration.Nodes;

namespace FubuMVC.Core.Diagnostics.Instrumentation
{
public class BehaviorTracer : WrappingBehavior
{
private readonly BehaviorNode _node;
private readonly IChainExecutionLog _log;

public BehaviorTracer(BehaviorNode node, IChainExecutionLog log)
{
_node = node;
_log = log;
}

protected override void invoke(Action action)
{
_log.StartSubject(_node);

try
{
action();
}
catch (Exception ex)
{
_log.LogException(ex);
throw;
}
finally
{
_log.FinishSubject();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace FubuMVC.Core.Diagnostics.Instrumentation
{
public class ChainExecutionLog :IRequestLog, ISubject
public class ChainExecutionLog :IRequestLog, ISubject, IChainExecutionLog
{
private readonly IDictionary<string, object> _request;
private readonly Stopwatch _stopwatch = new Stopwatch();
Expand Down
13 changes: 13 additions & 0 deletions src/FubuMVC.Core/Diagnostics/Instrumentation/IChainExecutionLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace FubuMVC.Core.Diagnostics.Instrumentation
{
public interface IChainExecutionLog
{
void StartSubject(ISubject subject);
void FinishSubject();
void LogException(Exception ex);
void AddLog(object log);
void Trace(string description, Action action);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@

namespace FubuMVC.Core.Diagnostics.Instrumentation
{
public interface IExecutionRecorder
public interface IExecutionLogger
{
void Record(ChainExecutionLog log, IDictionary<string, object> http);
}


public class NulloExecutionRecorder : IExecutionRecorder
public class NulloExecutionRecorder : IExecutionLogger
{
public void Record(ChainExecutionLog log, IDictionary<string, object> http)
{
// no-op
}
}

public class VerboseExecutionRecorder : IExecutionRecorder
public class VerboseExecutionRecorder : IExecutionLogger
{
private readonly IExecutionLogStorage _storage;

Expand All @@ -34,7 +34,7 @@ public void Record(ChainExecutionLog log, IDictionary<string, object> http)
}
}

public class ProductionExecutionRecorder : IExecutionRecorder
public class ProductionExecutionRecorder : IExecutionLogger
{
private readonly IExecutionLogStorage _storage;

Expand Down
4 changes: 3 additions & 1 deletion src/FubuMVC.Core/FubuMVC.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@
<Compile Include="DeactivatorExecuted.cs" />
<Compile Include="DiagnosticsExtensions.cs" />
<Compile Include="Diagnostics\Instrumentation\Activity.cs" />
<Compile Include="Diagnostics\Instrumentation\BehaviorTracer.cs" />
<Compile Include="Diagnostics\Instrumentation\ChainExecutionLog.cs" />
<Compile Include="Diagnostics\Instrumentation\IExecutionRecorder.cs" />
<Compile Include="Diagnostics\Instrumentation\IChainExecutionLog.cs" />
<Compile Include="Diagnostics\Instrumentation\IExecutionLogger.cs" />
<Compile Include="Diagnostics\Instrumentation\IRequestLog.cs" />
<Compile Include="Diagnostics\Instrumentation\ISubject.cs" />
<Compile Include="Diagnostics\Instrumentation\PerformanceHistory.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using FubuMVC.Core.Behaviors;
using FubuMVC.Core.Diagnostics.Instrumentation;
using FubuMVC.Core.Registration.Nodes;
using FubuMVC.Tests.Registration;
using FubuMVC.Tests.TestSupport;
using NUnit.Framework;
using Rhino.Mocks;
using Shouldly;

namespace FubuMVC.Tests.Diagnostics.Instrumentation
{
[TestFixture]
public class BehaviorTracerTester : InteractionContext<FubuMVC.Core.Diagnostics.Instrumentation.BehaviorTracer>
{
[Test]
public void when_the_inner_behavior_runs_cleanly()
{
var node = Wrapper.For<FakeBehavior>();
Services.Inject<BehaviorNode>(node);

var inner = MockFor<IActionBehavior>();
ClassUnderTest.Inner = inner;

ClassUnderTest.Invoke();

var log = MockFor<IChainExecutionLog>();
log.AssertWasCalled(x => x.StartSubject(node));

inner.AssertWasCalled(x => x.Invoke());

log.AssertWasCalled(x => x.FinishSubject());

}

[Test]
public void when_the_inner_behavior_throws_an_exception()
{
var node = Wrapper.For<FakeBehavior>();
Services.Inject<BehaviorNode>(node);

var ex = new DivideByZeroException();
var inner = MockFor<IActionBehavior>();
inner.Stub(x => x.Invoke()).Throw(ex);

ClassUnderTest.Inner = inner;

Exception<DivideByZeroException>.ShouldBeThrownBy(() =>
{
ClassUnderTest.Invoke();
});



var log = MockFor<IChainExecutionLog>();
log.AssertWasCalled(x => x.StartSubject(node));

inner.AssertWasCalled(x => x.Invoke());
log.AssertWasCalled(x => x.LogException(ex));

log.AssertWasCalled(x => x.FinishSubject());
}
}
}
1 change: 1 addition & 0 deletions src/FubuMVC.Tests/FubuMVC.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
<Compile Include="Diagnostics\DiagnosticSettings_TraceLevel_Registration_Tester.cs" />
<Compile Include="Diagnostics\FakeFubuDiagnostics.cs" />
<Compile Include="Diagnostics\Instrumentation\ActivityTester.cs" />
<Compile Include="Diagnostics\Instrumentation\BehaviorTracerTester.cs" />
<Compile Include="Diagnostics\Instrumentation\ChainExecutionLogTester.cs" />
<Compile Include="Diagnostics\Instrumentation\PerformanceHistoryTester.cs" />
<Compile Include="Diagnostics\InteractionContextExtensions.cs" />
Expand Down

0 comments on commit a8d75f8

Please sign in to comment.