Azure 在监控功能时将什么计为错误?

What does Azure count as error while monitoring functions?

我正在 运行 跟踪简单的功能,以检查 Function App 中的监控是如何工作的。如果我的函数 returns “400” 状态代码,在监控部分(以及随后的应用程序洞察),它将执行标记为成功。此外,如果它抛出并捕获错误,仍然标记为成功。如果它抛出错误但没有捕获它,那么它会检测并将其计为错误(但这种情况并不常见,因为在实际应用中,可能的错误总是需要被捕获)。

  1. 这就是 Azure 函数中监控的工作方式吗?因此,将执行标记为错误的唯一方法是抛出未捕获的错误?!?!

  2. 在 Application Insight 中,是否可以根据请求的响应状态代码对请求进行排序?例如,是否可以查看单个函数返回了多少 500 个请求?

module.exports = async function (context, req) {

    if (req.query.name || (req.body && req.body.name)) {
        context.res = {
            body: "Hello " + (req.query.name || req.body.name)
        };
    } else {
        // only if following line is uncommented, it counts the funciton execution as error
        // throw new Error('this is a test error')

        try {
            throw new Error('this is a test error')
        } catch (e) {
            // in this case, it counts the function execution as successfull
            return context.res = {
                status: 500,
                body: "caught the internal error"
            };
        }
        // in this case, counts the function execution as successfull
        return context.res = {
            status: 400,
            body: "didn't catch the error. Please pass a name on the query string or in the request body"
        };

    }
};

Is this how monitoring in Azure functions work? So the only way to mark the execution as faulty is to throw an uncaught error?

是的,你是对的,详情可以参考这个

In Application Insight, is there anyway to sort the requests based on their response status code? For example is there anyway to see how many 500 requests has been returned from an individual function?

您可以使用应用程序洞察分析来实现您的目标,编写如下简单的查询:

requests 
| where name ="your function app name"
| where resultCode =="500 or other status code"
| count

结果如下:

注意:如果您不知道如何导航到应用程序洞察分析,请按照以下步骤操作:

1.Nav 到 Azure 门户中的应用程序见解(与函数应用程序相关联)-> 概览边栏选项卡 -> 在顶部栏中,单击“分析”选项。