.NET エージェントのカスタム インストルメンテーションは、HttpContext オブジェクトを使用する Web トランザクションで動作します。一方、.NET エージェント API を使用すると、カスタム ダッシュボードに表示できるメトリックを収集できます。特に、RecordMetric、RecordResponseTimeMetric、および IncrementCounter は、Web 以外のアプリケーションで機能するため便利です。
ただし、.NET エージェントのバージョン 2.24.218.0 以降では、新しい機能を使用して、通常はエージェントが作成しないトランザクションを作成できます。これは、カスタム インストルメンテーション ファイルによる手動プロセスです。
C:\ProgramData\New Relic.NET Agent\Extensions に、CoreInstrumentation.xml と並んで 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>
アプリケーションを数回実行すると、[その他のトランザクション] の [バックグラウンド] カテゴリにカスタム トランザクションが表示されます。トランザクション内訳テーブルとトランザクション追跡にダミー セグメントが表示されます。