Custom instrumentation for generic classes in .Net using NewRelic

大城市里の小女人 提交于 2019-12-24 17:00:14

问题


Is there any way to instrument generic classes in .Net with new relic custom instrumenation? I added the instrumentation config like I do for non generic classes but with no luck.


回答1:


You will need to use an IL signature. The best way to determine the signature is to turn up New Relic logging to 'all' in newrelic.config, run your app and then search the corresponding profiler log for the method name (make sure to set the logging back to info when finished).

Using the signature you can the build Custom Instrumentation and Custom Transactions.

Using example code:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var t = new Test<int>();
            while (true)
            {
                StartTransaction();
                t.mymethod(100);
            }
        }
        static void StartTransaction()
        {
            Console.WriteLine("Start Transaction");
            System.Threading.Thread.Sleep(1100);
            MethodA();
        }
        static void MethodA()
        {
            Console.WriteLine("MethodA");
        }
    }
    class Test<T>
    {
        public void mymethod(T t) {
            var k = t;
            System.Threading.Thread.Sleep(1000);
        }
    }
}

Looking in the profiler log, NewRelic.Profiler.[pid] where [pid] corresponds to the w3wp process or other process running your application, you would see the following:

[Trace]Possibly instrumenting: (Module: C:\Users\me\Documents\Visual Studio 2013\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe, AppDomain: ConsoleApplication1.exe)[ConsoleApplication1]ConsoleApplication1.Test`1.mymethod

The AppDomain without the extension ConsoleApplication1 and the class/method name ConsoleApplication1.Test`1.mymethod are needed to build the custom instrumentation/transaction.

Example (custom transaction):

<?xml version="1.0" encoding="utf-8"?>
<extension xmlns="urn:newrelic-extension">
    <instrumentation>
      <tracerFactory name="NewRelic.Agent.Core.Tracer.Factories.BackgroundThreadTracerFactory" metricName="Background/MyTaskRunner">
        <match assemblyName="ConsoleApplication1" className="ConsoleApplication.Test`1">
          <exactMethodMatcher methodName="mymethod" />
        </match>
      </tracerFactory>
    </instrumentation>
</extension>

In general you don't need to specify method parameters in the instrumentation file.



来源:https://stackoverflow.com/questions/25554810/custom-instrumentation-for-generic-classes-in-net-using-newrelic

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!