添加新遗迹';s自定义检测到Windows中的后台进程

本文关键字:检测 自定义 Windows 后台进程 新遗迹 遗迹 添加 | 更新日期: 2023-09-27 17:58:33

我正在尝试监视.NET应用程序中的方法,该应用程序是一个使用New Relic的后台进程,我知道我需要添加Custom Instrumentation。

我已经重新安装了.NET Agent,在配置了"检测所有.NET应用程序",并在app.config和newrelic.config文件中进行了更改后,我将在新的遗迹面板中获得后台进程的基本数据。

现在,为了添加Custom Instrumentation,我在extensions目录中添加了另一个Instrumentation配置文件。已重新启动应用程序,但仍然看不到我试图监视的新方法/自定义方法。

这是我的检测文件MyInstrumentation.xml

<?xml version="1.0" encoding="utf-8"?>

<!-- instrument EngineService.BestAgentSolver.Solve inside EngineService.BestAgentSolver -->
<tracerFactory metricName="Cast-a-Net.EngineService.BestAgentSolver.Solve-Metric">
  <match assemblyName="Cast-a-Net.EngineService" className="Cast-a-Net.EngineService.BestAgentSolver">
    <exactMethodMatcher methodName="Solve" />
  </match>
</tracerFactory>
<!-- instrument EngineService.SessonManager.BroadcastLeadCounts inside EngineService.SessionManager -->
<tracerFactory metricName="Cast-a-Net.EngineService.SessionManager.BroadcastLeadCounts-Metric">
  <match assemblyName="Cast-a-Net.EngineService" className="Cast-a-Net.EngineService.SessionManager">
    <exactMethodMatcher methodName="BroadcastLeadCounts" />
  </match>
</tracerFactory>
<tracerFactory metricName="myapp.Web.Controllers.CallListController.ActionResult-Metric">
  <match assemblyName="myapp.Web" className="myapp.Web.Controllers.CallListController">
    <exactMethodMatcher methodName="ActionResult" />
  </match>
</tracerFactory>

我是错过了一步还是做错了什么?

添加新遗迹';s自定义检测到Windows中的后台进程

.NET代理中的自定义插入与使用HttpContext对象的web事务一起工作。另一方面,我们的.NET代理API允许您收集可以在自定义仪表板中显示的指标。特别是RecordMetric、RecordResponseTimeMetric和IncrementCounter非常有用,因为它们可用于非web应用程序。

但是,从2.24.218.0版本的.NET代理开始,可以使用一个新功能来创建代理通常不会执行的事务。这是通过自定义检测文件手动执行的过程。

在C:''ProgramData''New Relic.NET Agent''Extensions中创建一个名为CustomInstrumentation.xml的自定义检测文件,并与CoreInstrumentation.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中,当查看监控>事务页面时,您可以从类型下拉菜单中选择事务类型。

请注意,Category和Name必须同时存在,并且必须用斜线分隔。

正如您所期望的,在方法调用期间发生的插入的活动(方法、数据库、外部)将显示在事务的细分表和事务跟踪中。

这里有一个更具体的例子。首先,仪器文件:

<?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段。