.NET 代理中的自定义检测适用于使用 HttpContext 对象的 Web 事务。另一方面,我们的 .NET 代理 API 允许您收集可以显示在自定义仪表板中的指标。特别是,RecordMetric、RecordResponseTimeMetric 和 IncrementCounter 非常有用,因为它们适用于非 Web 应用程序。
但是,从 .NET 代理的 2.24.218.0 版本开始,可以使用一项新功能来创建代理通常不会这样做的事务。这是通过自定义检测文件进行的手动过程。
在 CoreInstrumentation.xml 旁边的 C:\ProgramData\New Relic.NET Agent\Extensions 中创建一个名为 CustomInstrumentation.xml 的自定义检测文件。将以下内容添加到您的自定义检测文件中:
<?xml version="1.0" encoding="utf-8"?>
<extension xmlns="urn:newrelic-extension">
<instrumentation>
<tracerFactory name="NewRelic.Agent.Core.Tracer.Factories.BackgroundThreadTracerFactory" metricName="Category/Name">
<match assemblyName="AssemblyName" className="NameSpace.ClassName">
<exactMethodMatcher methodName="MethodName" />
</match>
</tracerFactory>
</instrumentation>
</extension>
您必须更改上面的属性值 Category/Name、AssemblyName、NameSpace.ClassName 和 MethodName:
当来自程序集 AssemblyName 的 NameSpace.ClassName 类型的对象调用 MethodName 方法时,事务开始。当方法返回或抛出异常时,事务结束。该事务将被命名为 Name,并将分组到 Category 指定的事务类型中。在 New Relic UI 中,您可以在查看 Monitoring > Transactions 页面时从 Type 下拉菜单中选择事务类型。
请注意,类别和名称都必须存在,并且必须用斜杠分隔。
如您所料,在方法调用期间发生的检测活动(方法、数据库、外部)将显示在事务的分解表和事务跟踪中。
这是一个更具体的例子。一、检测文件:
<?xml version="1.0" encoding="utf-8"?>
<extension xmlns="urn:newrelic-extension">
<instrumentation>
<tracerFactory name="NewRelic.Agent.Core.Tracer.Factories.BackgroundThreadTracerFactory" metricName="Background/Bars">
<match assemblyName="Foo" className="Foo.Bar">
<exactMethodMatcher methodName="Bar1" />
<exactMethodMatcher methodName="Bar2" />
</match>
</tracerFactory>
<tracerFactory metricName="Custom/some custom metric name">
<match assemblyName="Foo" className="Foo.Bar">
<exactMethodMatcher methodName="Bar3" />
</match>
</tracerFactory>
</instrumentation>
</extension>
现在一些代码:
var foo = new Foo();
foo.Bar1(); // Creates a transaction named Bars in category Background
foo.Bar2(); // Same here.
foo.Bar3(); // Won't create a new transaction. See notes below.
public class Foo
{
// this will result in a transaction with an External Service request segment in the transaction trace
public void Bar1()
{
new WebClient().DownloadString("http://www.google.com/);
}
// this will result in a transaction that has one segment with a category of "Custom" and a name of "some custom metric name"
public void Bar2()
{
// the segment for Bar3 will contain your SQL query inside of it and possibly an execution plan
Bar3();
}
// if Bar3 is called directly, it won't get a transaction made for it.
// However, if it is called inside of Bar1 or Bar2 then it will show up as a segment containing the SQL query
private void Bar3()
{
using (var connection = new SqlConnection(ConnectionStrings["MsSqlConnection"].ConnectionString))
{
connection.Open();
using (var command = new SqlCommand("SELECT * FROM table", connection))
using (var reader = command.ExecuteReader())
{
reader.Read();
}
}
}
}
这是一个演示自定义事务的简单控制台应用程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Custom Transactions");
var t = new CustomTransaction();
for (int i = 0; i < 100; ++i )
t.StartTransaction();
}
}
class CustomTransaction
{
public void StartTransaction()
{
Console.WriteLine("StartTransaction");
Dummy();
}
void Dummy()
{
System.Threading.Thread.Sleep(5000);
}
}
}
使用以下自定义检测文件:
<?xml version="1.0" encoding="utf-8"?>
<extension xmlns="urn:newrelic-extension">
<instrumentation>
<tracerFactory name="NewRelic.Agent.Core.Tracer.Factories.BackgroundThreadTracerFactory" metricName="Background/CustomTransaction">
<match assemblyName="ConsoleApplication1" className="ConsoleApplication1.CustomTransaction">
<exactMethodMatcher methodName="StartTransaction" />
</match>
</tracerFactory>
<tracerFactory metricName="Custom/Dummy">
<match assemblyName="ConsoleApplication1" className="ConsoleApplication1.CustomTransaction">
<exactMethodMatcher methodName="Dummy" />
</match>
</tracerFactory>
</instrumentation>
</extension>
运行应用程序几次后,您应该会在“其他事务,后台”类别中看到一个自定义事务。您应该在事务分解表和事务跟踪中看到 Dummy 段。