如何在Azure Portal中查询含HTTP参数的Azure Functions请求响应时间
Azure Functions HTTP请求响应时间+参数查询方案
核心查询语句(直接可用)
以下Kusto查询可同时获取响应时间和HTTP请求详情,替换占位符后即可在Application Insights日志中运行:
requests | where cloud_RoleName == "你的函数应用名称" // 替换为实际函数应用名 | where operation_Name matches regex @"^(GET|POST|PUT|DELETE)" // 过滤HTTP请求类型 | extend HttpMethod = tostring(customDimensions["HttpMethod"]), RequestUrl = tostring(customDimensions["RequestUrl"]), QueryParams = parse_url(RequestUrl).Query, // 自动提取URL中的查询参数 DurationMs = duration / 1ms // 将响应时间转换为毫秒格式 | project 时间戳 = timestamp, 响应时间(ms) = DurationMs, 请求方法 = HttpMethod, 请求URL = RequestUrl, 查询参数 = QueryParams, 响应状态码 = resultCode, 请求ID = operation_Id | order by 时间戳 desc
生成可点击详情的散点图
运行查询后按以下步骤操作:
- 切换到顶部的图表视图
- 图表类型选择散点图
- X轴设置为
时间戳,Y轴设置为响应时间(ms) - 点击任意散点,右侧面板会自动展开该请求的完整详情(包括请求方法、URL、查询参数等)
扩展:追踪请求体或自定义业务参数
默认Application Insights不会收集请求体或自定义参数,需完成以下配置:
1. 修改host.json开启请求体日志
在函数应用的host.json中添加配置,开启请求体追踪(注意设置大小阈值避免日志过载):
{ "logging": { "applicationInsights": { "requestTrackingSettings": { "enableRequestBodyLogging": true, "requestBodyLoggingThreshold": 2048 // 最大记录2KB的请求体内容 } } } }
2. 代码中添加自定义参数追踪(可选)
如果需要追踪特定业务参数(如订单ID、用户ID),可在函数代码中手动写入自定义维度:
// C# 示例 using Microsoft.ApplicationInsights; public static async Task<IActionResult> Run(HttpRequest req, TelemetryClient telemetry) { // 获取业务参数 var orderId = req.Query["orderId"]; var requestBody = await new StreamReader(req.Body).ReadToEndAsync(); // 将参数写入Application Insights自定义维度 telemetry.Context.GlobalProperties["OrderId"] = orderId; telemetry.Context.GlobalProperties["RequestBody"] = requestBody; // 函数业务逻辑... return new OkObjectResult("处理完成"); }
对应查询语句(含自定义参数)
requests | where cloud_RoleName == "你的函数应用名称" | where operation_Name matches regex @"^(GET|POST|PUT|DELETE)" | extend HttpMethod = tostring(customDimensions["HttpMethod"]), RequestUrl = tostring(customDimensions["RequestUrl"]), QueryParams = parse_url(RequestUrl).Query, DurationMs = duration / 1ms, OrderId = tostring(customDimensions["OrderId"]), // 提取自定义订单ID RequestBody = tostring(customDimensions["RequestBody"]) // 提取请求体 | project 时间戳 = timestamp, 响应时间(ms) = DurationMs, 请求方法 = HttpMethod, 请求URL = RequestUrl, 查询参数 = QueryParams, 订单ID = OrderId, 请求体 = RequestBody, 响应状态码 = resultCode, 请求ID = operation_Id | order by 时间戳 desc
内容的提问来源于stack exchange,提问作者TY00
相关产品推荐
相关产品推荐

