You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure API Management消费计划下如何将订阅密钥传入Application Insights

实现方案:将APIM订阅键传入Application Insights

1. 启用APIM与Application Insights的日志集成

消费计划的APIM虽无内置分析功能,但可配置将日志发送至Application Insights。确保已在APIM的监测 > 日志页面完成Application Insights实例绑定,并开启Requests等必要日志类别。

2. 通过APIM策略注入订阅键到遥测属性

在APIM的API级别或全局级别添加以下入站策略,提取订阅键并添加到遥测自定义属性中,使其随请求日志同步到Application Insights:

<inbound>
    <!-- 从请求头获取订阅键(若使用查询参数传递,替换为Query.GetValueOrDefault) -->
    <set-variable name="subscriptionKey" value="@(context.Request.Headers.GetValueOrDefault("Ocp-Apim-Subscription-Key", "unknown"))" />
    <!-- 将订阅键添加到遥测属性 -->
    <set-telemetry-properties>
        <property name="subscriptionKey" value="@((string)context.Variables["subscriptionKey"])" />
    </set-telemetry-properties>
</inbound>

注:若用户通过查询参数传递订阅键,需将context.Request.Headers替换为context.Request.Query.GetValueOrDefault("subscription-key", "unknown")

3. 修改Application Insights查询

配置完成后等待5-15分钟让日志同步,即可在查询中新增subscriptionKey字段,修改后的查询语句如下:

requests
| project
    timestamp,
    url,
    operation_Name,    
    success,
    resultCode,
    duration,
    cloud_RoleName,
    subscriptionKey = customDimensions.subscriptionKey
| where timestamp > ago(1d)
| order by timestamp desc
| take 100

扩展统计示例

若需按订阅键统计调用数据,可使用以下查询:

requests
| where timestamp > ago(7d)
| summarize 
    totalCalls = count(),
    successRate = avg(todouble(success)) * 100,
    avgDuration = avg(duration)
    by subscriptionKey = customDimensions.subscriptionKey
| order by totalCalls desc

内容的提问来源于stack exchange,提问作者Jesse O

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 11:28:29