gpt4 book ai didi

azure - 是否可以使用 New Relic 来监控 Azure Function 应用程序?

转载 作者:行者123 更新时间:2023-12-02 20:51:53 29 4
gpt4 key购买 nike

我们有一个每隔几秒运行一次的 Azure 函数。我们希望使用 New Relic 来让我们知道这段代码是否抛出异常或开始出现异常行为。我已在虚拟机上安装了 New Relic 代理,并通过 NuGet 引入了 NewRelic.Agent.Api DLL。我已将 NewRelic.AppName 设置为 AzureFunctionAppTest 并设置 NEWRELIC_LICENSEKEY 环境变量。日志文件表明我正在连接到 NewRelic。现在,我正在运行以下代码:

#r "Microsoft.windowsazure.storage"

using System;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage.Table;

public static void Run(TimerInfo myTimer, TraceWriter log, CloudTable syncTimesTable)
{
NewRelic.Api.Agent.NewRelic.SetTransactionName("SyncEvents", "GetRoutineData");

var rnd = new Random(DateTime.Now.Millisecond).Next(30);
log.Info($"Logging {rnd}");

var eventAttributes = new Dictionary<string, object>() {{"result", "Success!"}, {"Count", rnd.ToString()}};
NewRelic.Api.Agent.NewRelic.RecordCustomEvent("DevicesSynced", eventAttributes);

NewRelic.Api.Agent.NewRelic.NoticeError(new SyncException("This is another custom error"));
}

public class SyncException : Exception
{
public SyncException(string msg) : base(msg)
{
}
}

所以,基本上我尝试设置交易名称,然后生成一个随机数并将其记录为自定义事件,然后尝试记录自定义错误。

我让它运行一会儿,然后使用 New Relic 仪表板进行检查。这是我得到的:

  • 我看到我的新应用程序显示在 APM 仪表板上。
  • 在 Insights Dashboard 上,我可以查询 SELECT * FROM DevicesSynced SINCE 30 MINUTES AGO,然后我看到一个表,其中包含一堆记录“成功!”计数列中包含一个随机数。
  • 在 APM->Events->Errors 中,我看到“NewRelic.Api.Agent.NoticeError API Call”的“URL 和类型”Submission#0+SyncException”,带有消息“这是另一个自定义错误”和计数。
  • 在错误分析中,我看到“太棒了!此时间窗口内没有错误!” (最后30分钟)
  • 在“监控”->“事务”中,我仅看到 HTTP 请求的事务,例如/Home/Get、/Admin/GetHostStatus、/Keys/Get 和/Admin/GetFunctionStatus。我认为这些 HTTP 请求来自 Azure Functions 的内部工作,并且可能在我在 Azure 控制台中单击时发生。我没有看到任何名为 GetRoutineData 的事务。如果我运行 SELECT * FROM Transactions ,我也看不到这一点。

我有一种感觉,我实际上并没有参与我的 Azure Function 代码中的事务,因此 New Relic 不允许我设置名称。我还有一种感觉,即使我使用 NOTICEError 记录错误,它也不会链接到任何事务,因此它不会显示在错误分析中。

我的问题:

  • 有没有办法让 New Relic 将此代码路径识别为事务?
  • 如果没有,是否至少有一种方法可以设置 New Relic 应用程序监控,以便在发生特定异常类型(例如我的 SyncException 类)或达到特定阈值时向我发出警报?

谢谢!

更新:

我尝试在 Extensions 目录中添加一个名为 CustomInstrumentation.xml 的文件,按照 New Relic instructions包含以下内容:

<?xml version="1.0" encoding="utf-8"?>
<extension
xmlns="urn:newrelic-extension">
<instrumentation>
<!-- Define the method which triggers the creation of a transaction. -->
<tracerFactory name="NewRelic.Agent.Core.Tracer.Factories.BackgroundThreadTracerFactory" metricName="Background/AzureDeviceSync">
<match assemblyName="Microsoft.Azure.WebJobs.Script" className="Microsoft.Azure.WebJobs.Script.Description.FunctionInvokerBase">
<exactMethodMatcher methodName="Invoke" />
</match>
</tracerFactory>
</instrumentation>
</extension>

但是,我没有看到行为有任何差异。我尝试了几种不同的方法名称匹配(例如 InvokeCoreInvoke),但似乎没有什么区别。

几个问题:

新遗物文档说:

To view the transaction: From the New Relic menu bar, select APM > Applications > (selected app) > Monitoring > Transactions > Type > (selected type). (The type is defined by Category/Name.) You can then view all custom transactions of the selected type.

它显示了带有自定义选项的下拉菜单的图片:

enter image description here

但是,我的 New Relic 界面没有任何类型的“类型”下拉列表:

enter image description here

此外,如果我检测代码以显示 Function 应用程序内的堆栈跟踪,这就是我得到的结果。也许会有帮助:

Stack:

at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
at System.Environment.get_StackTrace()
at Submission#0.Run(TimerInfo myTimer, TraceWriter log, CloudTable syncTimesTable) in :line 9
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.Azure.WebJobs.Script.Description.DotNetFunctionInvoker.<InvokeCore>d__23.MoveNext()
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine)
at Microsoft.Azure.WebJobs.Script.Description.DotNetFunctionInvoker.InvokeCore(Object[] parameters, FunctionInvocationContext context)
at Microsoft.Azure.WebJobs.Script.Description.FunctionInvokerBase.<Invoke>d__29.MoveNext()
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine)
at Microsoft.Azure.WebJobs.Script.Description.FunctionInvokerBase.Invoke(Object[] parameters)
at Host.Functions.LogNewRelicAlert(TimerInfo myTimer, TraceWriter log, CloudTable syncTimesTable, ExecutionContext _context)
at lambda_method(Closure , Functions , Object[] )
at Microsoft.Azure.WebJobs.Host.Executors.TaskMethodInvoker`1.InvokeAsync(TReflected instance, Object[] arguments)
at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`1.<InvokeAsync>d__8.MoveNext()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(Object stateMachine)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()
at System.Runtime.CompilerServices.YieldAwaitable.YieldAwaiter.RunAction(Object state)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

我想知道是否有任何类型的日志文件可以显示是否确实加载了自定义事务设置以及何时创建了新事务。现在我猜我的 XML 匹配了错误的内容。

还有一件事。如果我添加以下行:

NewRelic.Api.Agent.NewRelic.SetTransactionName("Custom", "Testing");

在我的 Function 应用中,我会在 New Relic 日志中看到以下警告:

2017-02-01 21:45:25,005 NewRelic WARN: Agent API Error: An error occurred invoking API method "SetTransactionName" - "System.InvalidOperationException: The API method called is only valid from within a transaction. This error can occur if you call the API method from a thread other than the one the transaction started on.

我认为这清楚地表明我们此时实际上并未处于事务中,除非它发生在另一个线程上(如果是这种情况我不会感到惊讶)。

还有其他想法吗?

最佳答案

终于成功了!好吧,至少我认为它会起作用。这是/Extensions 目录中我的 .XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<extension
xmlns="urn:newrelic-extension">
<instrumentation>
<!-- Define the method which triggers the creation of a transaction. -->
<tracerFactory
name="NewRelic.Agent.Core.Tracer.Factories.BackgroundThreadTracerFactory"
metricName="Limeade/AzureDeviceSync">
<match assemblyName="Microsoft.Azure.WebJobs.Script" className="Microsoft.Azure.WebJobs.Script.Description.DotNetFunctionInvoker">
<exactMethodMatcher methodName="InvokeCore" />
</match>
</tracerFactory>
</instrumentation>
</extension>

这个文件的名称并不重要,因为 New Relic 将加载 Extensions 目录中的每个 XML 文件。

以下是我学到的一些东西:

  1. 当您更改任何 New Relic 配置(包括扩展 XML 文件)时,您必须重置 IIS。然而,SCM 上的“重新启动站点”按钮实际上并没有杀死 IIS 进程。您必须进入 Kudu 中的 Process Explorer 并关闭 w3wp.exe 进程。如果您无法确定要终止哪个进程,可以添加 log.Info($"Currently running in PID: {System.Diagnostics.Process.GetCurrentProcess().Id}"); 到您的函数应用程序以找出答案。

  2. 默认情况下,New Relic 将生成令人困惑的事务名称,例如 Microsoft.Azure.WebJobs.Script.Description.DotNetFunctionInvoker/InvokeCore。我找不到在 XML 文件中设置事务名称的方法。但是,您可以在 Azure Function 代码中设置新事务名称,如下所示:

    NewRelic.Api.Agent.NewRelic.SetTransactionName("SyncEvents", "GetRoutineData");

  3. 如果从 Run 方法抛出异常并将该异常记录为 New Relic 中的错误,那就太好了,但经过相当多的尝试后我无法得到这个上类。我认为这些异常正在某个地方被吃掉,并且我无法找出跟踪器配置的正确组合来在它们丢失之前拦截它们。但是,您可以使用NoticeError()调用:NewRelic.Api.Agent.NewRelic.NoticeError(new SyncException("这是自定义错误"));这看起来工作原理是一样的,这些错误将显示在 New Relic 的错误错误分析中。缺点是您可能必须重构一堆 Azure Function 代码来捕获所有错误、注意到它们,然后在必要时重新抛出它们。

我花了两天时间才让它发挥作用,所以我希望这对某人有帮助!!

关于azure - 是否可以使用 New Relic 来监控 Azure Function 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41969537/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com