【问题标题】:Error reporting in Azure Functions vs App InsightsAzure Functions 与 App Insights 中的错误报告
【发布时间】:2021-11-26 13:07:27
【问题描述】:

我们调用由 Azure Functions 托管的基于 REST 的 API,但未能实现支持 App Insights 的一致错误处理,想知道可以做些什么:

  • 如果我们不处理函数的异常,那么 App Insights 报告“失败”,但服务只向调用者返回错误代码,但没有错误内容: 因此,客户端会收到 500,仅此而已。

  • 如果我们处理异常并将其记录(到 AppInsights),则 App Insights 会停止报告“失败”,因此对功能级别的监控会中断。我们可以查询异常,但它们是脱离上下文的(即,我们只能通过自定义查询看到异常)并且我们不知道实际影响的是哪个函数。

如何结合这两种需求:

  1. 让函数失败,以便 AppInsights 报告失败(并且监视器可以发出警报)
  2. 向调用者返回比 500 更有意义的错误消息。

AppInsights 中的外观示例:

Exception is visible on the Exceptions tab, but the underlying operation has not failed

更新: 根据 Microsoft 的说法,App Insight 故障是未处理异常所独有的。不过,打开是否有办法至少传递错误消息。

【问题讨论】:

    标签: azure-functions azure-application-insights


    【解决方案1】:

    如果不处理函数异常,则 App Insights 报告“失败”,返回错误代码,但没有错误内容。因此,客户端会收到 500,仅此而已。

    App Insights 不返回任何内容,那么返回错误代码是什么意思

    如果我们处理异常,将其记录(到 AppInsights),在代码 500 中返回简单的错误消息,然后 App Insights 不会将此记录为“失败”,因此无法进行监控。

    您能展示一下您是如何进行日志记录的吗?因为一旦您使用 App Insights 记录异常,您应该会在 failures 下看到它。

    这应该可行:

    try
    {
       ...
    }
    catch(Exception e)
    {
       logger.LogError(e, e.Message);
       return httpRequest.CreateResponse(HttpStatusCode.InternalServerError, e.Message);
    }
    

    【讨论】:

    • 没错,我应该说“报告”错误代码,而不是失败。因此,我们可以有 100 个错误代码为 500 的响应,但在 App Insights 上看不到任何故障。我们按照您的描述进行尝试捕获;做 (ILogger 记录器).... 捕获 (...) { logger.LogError(ex, ex.Message).这会记录异常,但不会导致 App Insights 出现“失败”
    猜你喜欢
    • 2021-11-14
    • 1970-01-01
    • 2017-02-10
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 2020-05-14
    • 1970-01-01
    • 2021-09-14
    相关资源
    最近更新 更多