【问题标题】:Adding New Relic's Custom Instrumentation to background process in Windows将 New Relic 的自定义检测添加到 Windows 中的后台进程
【发布时间】:2014-04-15 05:43:29
【问题描述】:

我正在尝试监视 .NET 应用程序中的方法,该应用程序是使用 New Relic 的后台进程,我知道我需要为其添加自定义仪器。

我已经重新安装了 .NET 代理,在配置了“检测所有 .NET 应用程序”,并在 app.config 和 newrelic.config 文件中进行了更改后,我在 new 中获取了后台进程的基本数据遗物仪表板。

现在,为了添加自定义检测,我在扩展目录中添加了另一个检测配置文件。重新启动应用程序,但仍然看不到我正在尝试监控的新/自定义方法。

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

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

【问题讨论】:

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


    【解决方案1】:

    .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 下拉菜单中选择事务类型。

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

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

    这是一个更具体的例子。一、instrumentation文件:

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

    【讨论】:

    • 使用 NewRelic.Api 和 XML 有区别吗?还是一样,只是语法不同?
    • @Snæbjørn custom instrumentation 的 XML 文件与 .NET Agent API 提供的功能明显不同。使用 XML 文件为您希望探查器跟踪的方法定义检测。将 API 用于特定实用程序,例如命名事务或记录错误。直接使用 API 记录指标会生成可以在 custom dashboards 中查看的指标。
    • 玩了一会儿我想我现在明白了:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多