你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
适用于:所有 API 管理层级
emit-metric 策略将指定格式的自定义指标发送到 Application Insights。
Note
按照策略声明中提供的顺序设置策略的元素和子元素。 详细了解如何设置或编辑 API 管理策略。
自定义指标的限制
Azure Monitor 对可能影响从 API 管理发出指标能力的自定义指标实施使用限制。 例如,Azure Monitor 当前为每个指标设置 10 个维度键的限制,并为订阅中每个区域设置 50,000 个总活动时序数的限制(期限为 12 小时)。 在 API 管理中,其中 5 个用于默认维度,包括:
- Region
- Service ID
- Service Name
- Service Type
这些限制对在 API 管理策略(例如 emit-metric 或 azure-openai-emit-token-metric)中配置自定义指标具有以下影响:
每个策略最多可以配置 5 个自定义维度。
策略在 12 小时期限内生成的活动时序数是该期限内每个已配置维度的唯一值数的乘积。 例如,如果在策略中配置了三个自定义维度,并且每个维度在该期限内有 10 个可能的值,则 策略将生成 1,000 (10 x 10 x 10) 个活动时序。
如果在订阅的同一区域中的多个 API 管理实例中配置 策略,则所有实例都可能导致区域活动时序限制。
详细了解 Azure Monitor 中自定义指标的设计限制和注意事项。
Prerequisites
- API 管理实例必须与 Application Insights 集成。 有关详细信息,请参阅如何将 Azure API 管理与 Azure Application Insights 集成。
- 为 API 启用 Application Insights 日志记录。
- 在 Application Insights 中启用含有维度的自定义指标。 有关详细信息,请参阅发出自定义指标。
Policy statement
<emit-metric name="name of custom metric" value="value of custom metric" namespace="metric namespace">
<dimension name="dimension name" value="dimension value" />
</emit-metric>
Attributes
| Attribute | Description | Required | Default value |
|---|---|---|---|
| name | A string. 自定义指标的名称。 不允许使用策略表达式。 | Yes | N/A |
| 命名空间 | A string. 自定义指标的命名空间。 不允许使用策略表达式。 | No | API Management |
| value | 以双精度表示的自定义指标的值。 允许使用策略表达式。 | No | 1 |
Elements
| Element | Description | Required |
|---|---|---|
| dimension | 为自定义指标中包含的每个维度添加一个或多个此类元素。 | Yes |
Dimension attributes
| Attribute | Description | Required | Default value |
|---|---|---|---|
| name | 字符串或策略表达式。 维度的名称。 | Yes | N/A |
| value | 字符串或策略表达式。 维度的值。 如果 name 与一个默认维度匹配,则只能省略。 如果是这样,则按维度名称提供值。 |
No | N/A |
无需值即可使用的默认维度名称
- API ID
- Operation ID
- Product ID
- User ID
- Subscription ID
- Location
- Gateway ID
- 后端 ID (仅在出站策略部分使用)
Usage
Usage notes
- 最多可以为此策略配置 5 个自定义维度。
Example
以下示例发送自定义指标,以将 API 请求数以及 API ID 作为默认维度进行计数。
<policies>
<inbound>
<emit-metric name="Request" value="1" namespace="my-metrics">
<dimension name="API ID" />
</emit-metric>
</inbound>
<outbound>
</outbound>
</policies>
Related policies
Related content
有关使用策略的详细信息,请参阅:
- 教程:转换和保护 API
- 策略参考,其中提供了策略语句及其设置的完整列表
- Policy expressions
- 设置或编辑策略
- 重复使用策略配置
- 策略片段存储库
- 策略场存储库
- Azure API 管理策略工具包
- 获取 Copilot 帮助以创建、解释策略和排查策略问题