I need to measure invocation time of Handle method in every instance of IHandleMessages<> interface. I tried to use Interceptor of Castle Windsor,
public class NsbHandlerMeasurementInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
if (invocation.Method.Name == ExpressionExtender.GetMethodName<IHandleMessages<DummyType>>(b => b.Handle(null)))
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
invocation.Proceed();
stopwatch.Stop();
// save stopwatch.ElapsedMilliseconds value
}
else
{
invocation.Proceed();
}
}
}
with installation code:
container.Register(Component.For<NsbHandlerMeasurementInterceptor>());
container.Kernel.ComponentModelBuilder.AddContributor(new NsbWindsorModelConstructionContributor());
public class NsbWindsorModelConstructionContributor : IContributeComponentModelConstruction
{
public void ProcessModel(global::Castle.MicroKernel.IKernel kernel, global::Castle.Core.ComponentModel model)
{
if (model.Services.Any(s => s.ImplementsGenericInterface(typeof(IHandleMessages<>))))
{
model.Interceptors.AddIfNotInCollection(new InterceptorReference(typeof(NsbHandlerMeasurementInterceptor)));
}
}
}
but from that moment Handle method is not firing.
I know about performance counters in NSB, but I need more specific, type-oriented measurements. Is it possible and achievable?
To measure all there is indeed performance counter but if that is not sufficient then you can create your own step in the NServiceBus pipeline.
http://docs.particular.net/nservicebus/pipeline/customizing
Create a custom behavior by inheriting
IBehavior<IncomingContext>
and implement the interface. You now have access to theIncomingContext
argument which contain information about the types.Take a look at the implementation of the
InvokeHandlersBehavior
behavior. This behavior invokes the actual handler and probably want to wrap that.https://github.com/Particular/NServiceBus/blob/5.2.0/src/NServiceBus.Core/Unicast/Behaviors/InvokeHandlersBehavior.cs
You then need to register it so that it called included in the pipeline.
Please note that this code requires v5. Check the Particular documentation website for help on other versions.