我在New Relic工作。
只要满足以下要求,就可以监控非IIS应用程序的性能:
您可以在我们的文档网站上阅读有关这些要求的更多信息:
https://docs.newrelic.com/docs/dotnet/instrumenting-custom-applications
您可能需要使用我们的.NET代理API来收集自定义指标。方法RecordMetric、RecordResponseTimeMetric和IncrementCounter专门用于非Web应用程序。
我们的.NET代理API文档位于此处:https://docs.newrelic.com/docs/dotnet/net-agent-api
您还可以设置自定义事务以跟踪非Web事务。通常,我们可以跟踪使用HttpObjects的函数,但以下是代理版本2.24.218.0中实现的新功能。
对于没有事务上下文的非Web应用程序和异步调用,可以使用以下功能创建事务,代理通常无法执行此操作。这是通过自定义插装文件进行的手动过程。
在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();
foo.Bar2();
foo.Bar3();
public class Foo
{
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>
*
)的方法来监视所有代码。也就是说,任何给定类的任何调用方法都可以在不太具体地指定应用程序域的情况下进行监视。您能确认吗? - Ali