将新的Relic Custom Instrumentation添加到Windows中的后台进程

时间:2014-04-15 05:43:30

标签: c# .net windows-services newrelic newrelic-platform

我正在尝试监控.NET应用程序中的方法,这是使用New Relic的后台进程,我知道我需要添加自定义工具。

我已经重新安装了.NET代理,在配置“Instrument all .NET Applications”并在app.config和newrelic.config文件中进行更改后,我在新版本中获取后台进程的基本数据遗物仪表板。

现在,要添加自定义检测,我在extensions目录中添加了另一个检测配置文件。重新启动了应用程序,但仍无法看到我想要监控的新/自定义方法。

这是我的检测文件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>

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

1 个答案:

答案 0 :(得分:3)

.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中,您可以在查看监控&gt;时从类型下拉菜单中选择交易类型。交易页面。

请注意,类别和名称必须存在且必须用斜杠分隔。

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

这是一个更具体的例子。首先,检测文件:

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

在运行应用程序几次后,您应该在“其他事务,背景”类别中看到自定义事务。您应该在事务细分表和事务跟踪中看到虚拟段。