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.
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.