如何为失败的持久功能编排设置 Azure 警报?

How to Setup Azure Alert for Failed Durable Function Orchestration?

当作为持久编排链一部分的 activity 函数中发生未知异常时,我希望通过电子邮件警报收到通知。问题是当 Activity 函数中抛出异常时,Application Insights 中记录的错误显示状态代码为零。在 Azure 警报中,没有警报配置会查找零日志消息状态代码,只有 1xx、2xx、4xx 等。无论我在代码上尝试了什么,我都无法获得 return 4xx 的 5xx 错误代码。

如何在 Azure Monitor Alert 中设置以捕获失败的业务流程?有没有办法在抛出异常时强制使用 non-zero 状态码?

背景信息

如以下屏幕截图所示,用于持久功能的 Azure Monitor 显示故障,这正是我所期望的...

但是,我无法 figure-out 如何设置警报来检测此故障。在Application Insights中发现异常,导致失败的响应码为零...

转到 Azure Monitor 警报,当 setting-up 一个新警报检测到这种情况时,没有状态代码为零的警报条件(这有道理,但没有帮帮我)...

这是 Activity 函数的 shell,它故意抛出异常以帮助我设置警报并知道它正在工作...

[FunctionName("SearchProductIndexFunctionActivity")]
public static async Task Run(
    [ActivityTrigger]string input,
    ILogger logger,
    ExecutionContext context)
{
    logger.LogInformation("Started SearchProductIndexFunctionActivity...");

    throw new ArgumentException("Oops...");

    logger.LogInformation("Finished SearchProductIndexFunctionActivity successfully.");
}

这里是调用 Activity 函数的 Orchestrator 函数。如果未捕获并重新抛出所有异常,Orchestrator 函数将显示 "success" 状态,这没有帮助...我想知道某些事情失败了。但是,由此异常产生的响应状态代码为零无助于设置警报。关于如何解决这个问题有什么建议吗?

[FunctionName("SearchOrchestration")]
public static async Task Run(
    [OrchestrationTrigger]DurableOrchestrationContext orchestrationContext,
    ExecutionContext context,
    ILogger logger)
{
    try
    {
        await orchestrationContext.CallActivityAsync(PART_INDEX_UPDATE_ACTIVITY_NAME, null);

        await orchestrationContext.CallActivityAsync(PRODUCT_INDEX_UPDATE_ACTIVITY_NAME, null);
    }
    catch (Exception ex)
    {
        logger.LogError(ex, $"Search index orchestration failed.");
        throw;
    }
}

由于您使用的是 Application insights,您可以考虑使用创建 自定义日志搜索警报 根据此 doc

对于 "Resource",您应该 select 用于 azure 函数的应用程序见解。

为"Condition",select为"Custom log search"。然后在 "Search query" 文本框中,您可以编写查询。在您的情况下,错误应该出现在应用程序见解的异常 table 中,因此查询看起来像 exceptions | your_query_for_error.


如果你对application insights中的sql类查询不熟悉,可以参考这个article。您可以先在 application insights 中编写查询,以确保它可以按预期工作。应用洞察中的查询截图: