如何用KQL查询Application Insights中特定API的最大RPS?
计算Application Insights中特定API的最大RPS(长时段适用)
要统计长时间段(比如2个月)内特定API的最大每秒请求数(RPS),核心思路是按秒级时间粒度统计请求量,再找出峰值。直接秒级聚合在超长时间范围下可能存在性能问题,下面提供两种方案,按需选择:
方案1:精确计算最大RPS
适合对精度要求高、数据量可承受的场景,直接按1秒窗口统计每秒请求数,再取最大值:
// 替换为你的目标API标识(名称/URL片段) let targetApi = "/api/your-target-endpoint"; requests | where timestamp between (startofmonth(ago(2months)) .. endofmonth(ago(0months))) | where name contains targetApi // 根据实际字段调整,比如用url matches regex targetApi | summarize requestsPerSecond = count() by bin(timestamp, 1s) | summarize maxRPS = max(requestsPerSecond)
方案2:性能优先的近似计算
如果2个月的数据量过大,直接秒级聚合性能不佳,可以用更大的时间窗口(比如5秒、10秒)统计,再转换为每秒请求数,牺牲少量精度换取计算效率:
let targetApi = "/api/your-target-endpoint"; let binSize = 5s; // 可根据需求调整窗口大小 requests | where timestamp between (startofmonth(ago(2months)) .. endofmonth(ago(0months))) | where name contains targetApi | summarize requestsPerBin = count() by bin(timestamp, binSize) | extend rps = requestsPerBin / (binSize / 1s) // 将窗口请求数转换为每秒请求数 | summarize maxRPS = max(rps)
关键说明
- 字段匹配:
requests表中,name通常对应API的操作名称,url是完整请求地址,根据你的实际数据结构调整筛选条件 - 时间范围:示例中用
startofmonth(ago(2months)) .. endofmonth(ago(0months))表示最近2个月,也可以用ago(60d)直接指定天数 - 维度拆分:如果需要按天/小时查看不同时段的最大RPS,可在最后一步
summarize前添加by bin(timestamp, 1d)
内容的提问来源于stack exchange,提问作者Insolent Goof
相关产品推荐
相关产品推荐

