使用 MassTransit 配置 NewRelic(类似于 NServiceBus)

Configuring NewRelic with MassTransit (Similar to NServiceBus)

我一直在尝试在 NewRelic 中为 MassTransit 设置自定义指标,类似于 NServiceBus 与 NewRelic 的工作方式。使用 NewRelic .Net Custom Transactions 文档,这似乎很容易实现。

到目前为止,我还没有检查 GitHub 上的 MassTransit 代码,我已经设法为 MassTransit 整理了一个简单的检测文件,但这只包含消息发送,但我真的希望消息也能收到,但我不能似乎找到了第一个与传输实现无关的拦截。

<?xml version="1.0" encoding="utf-8"?>
<extension xmlns="urn:newrelic-extension">
    <instrumentation>
        <tracerFactory metricName="MassTransit/Send">
            <match assemblyName="MassTransit" className="MassTransit.Transports.SendEndpoint">
                <exactMethodMatcher methodName="Send" parameters="!!0,System.Threading.CancellationToken" />
                <exactMethodMatcher methodName="Send" parameters="!!0,MassTransit.Pipeline.IPipe`1[MassTransit.SendContext`1[!!0]],System.Threading.CancellationToken" />
            </match>
        </tracerFactory>
    </instrumentation>
</extension>

我正在尝试创建等效的 MassTransit 文件的 NServiceBus 检测文件示例如下:

<?xml version="1.0" encoding="utf-8"?>
<extension xmlns="urn:newrelic-extension">
    <instrumentation>
        <tracerFactory>
            <match assemblyName="NServiceBus.Core" className="NServiceBus.InvokeHandlersBehavior">
                <exactMethodMatcher methodName="Invoke" parameters="NServiceBus.Pipeline.Contexts.IncomingContext,System.Action" />
            </match>
        </tracerFactory>
        <tracerFactory>
            <match assemblyName="NServiceBus.Core" className="NServiceBus.Unicast.UnicastBus">
                <exactMethodMatcher methodName="SendMessage" parameters="NServiceBus.Unicast.SendOptions,NServiceBus.Unicast.Messages.LogicalMessage" />
            </match>
        </tracerFactory>
    </instrumentation>
</extension>

更新

正如 Chris Patterson 所建议的那样,我已经更新到新包,所以我可以在 ReceivePipe.

中使用一种方法

然后我尝试检测此方法但没有成功,我怎么能看到在打开所有日志记录时可以在日志中检测:

[Trace] 2015-10-19 09:08:45 Possibly instrumenting: (Module: D:\Liberis\Services\Liberis.Salesforce.Service\MassTransit.dll, AppDomain: Liberis.Salesforce.Service.exe)[MassTransit]MassTransit.Pipeline.Pipes.ReceivePipe.MassTransit.Pipeline.IPipe<MassTransit.ReceiveContext>.Send(MassTransit.ReceiveContext)

我也尝试将配置更新为以下内容以匹配上面的内容,但仍然没有成功:

<?xml version="1.0" encoding="utf-8"?>
<extension xmlns="urn:newrelic-extension">
    <instrumentation>
        <tracerFactory name="NewRelic.Agent.Core.Tracer.Factories.BackgroundThreadTracerFactory" metricName="MassTransit/Receive">
            <match assemblyName="MassTransit" className="MassTransit.Pipeline.Pipes.ReceivePipe.MassTransit.Pipeline.IPipe<MassTransit.ReceiveContext>">
                <exactMethodMatcher methodName="Send" parameters="MassTransit.ReceiveContext" />
            </match>
        </tracerFactory>
    </instrumentation>
</extension>

我也尝试过使用 nrconfig 工具,但它只为 ReceivePipe class 输出以下内容:

  <match assemblyName="MassTransit" className="MassTransit.Pipeline.Pipes.ReceivePipe">
    <exactMethodMatcher methodName=".ctor" parameters="MassTransit.Pipeline.IPipe`1&lt;MassTransit.ReceiveContext&gt;,MassTransit.Pipeline.IConsumePipe" />
  </match>

由于 class 隐式实现了 IPipe<ReceiveContext> 接口,我觉得我需要以不同的方式检测它?

您应该能够以类似的方式挂接 ReceivePipe.Send 方法。

https://github.com/MassTransit/MassTransit/blob/master/src/MassTransit/Pipeline/Pipes/ReceivePipe.cs#L30

<?xml version="1.0" encoding="utf-8"?>
<extension xmlns="urn:newrelic-extension">
    <instrumentation>
        <tracerFactory metricName="MassTransit/Receive">
            <match assemblyName="MassTransit" className="MassTransit.Pipeline.Pipe.ReceivePipe">
                <exactMethodMatcher methodName="Send" parameters="ReceiveContext" />
            </match>
        </tracerFactory>
    </instrumentation>
</extension>

至少,这是我对 XML 语法的猜测,但该方法是合理的。

在@Chris Patterson 的帮助下,我设法解决了这个问题,比我想象的要付出更多的努力。

下面是需要的配置 xml:

<?xml version="1.0" encoding="utf-8"?>
<extension xmlns="urn:newrelic-extension">
    <instrumentation>
        <tracerFactory name="NewRelic.Agent.Core.Tracer.Factories.BackgroundThreadTracerFactory" metricName="MassTransit/Send">
            <match assemblyName="MassTransit" className="MassTransit.Transports.SendEndpoint">
                <exactMethodMatcher methodName="Send" parameters="!!0,System.Threading.CancellationToken" />
                <exactMethodMatcher methodName="Send" parameters="!!0,MassTransit.Pipeline.IPipe`1[MassTransit.SendContext`1[!!0]],System.Threading.CancellationToken" />
            </match>
        </tracerFactory>
        <tracerFactory name="NewRelic.Agent.Core.Tracer.Factories.BackgroundThreadTracerFactory" metricName="MassTransit/Receive">
            <match assemblyName="MassTransit" className="MassTransit.Pipeline.Pipes.ReceivePipe">
                <exactMethodMatcher methodName="MassTransit.Pipeline.IPipe&lt;MassTransit.ReceiveContext&gt;.Send" parameters="MassTransit.ReceiveContext" />
            </match>
        </tracerFactory>
    </instrumentation>
</extension>

由于隐式实现了 IPipe 接口,方法签名似乎完全改变了。

我也已将其添加到 github 存储库 - https://github.com/LiberisLabs/NewRelic.Agent.CustomTransactions