如何收集无法导出至Azure Monitor的Azure Event Hub指标
采集Azure Event Hub未导出指标到存储的方案
方法1:通过Azure Monitor REST API定时拉取
直接调用Azure Monitor指标API,定期获取ThrottledRequests、ActiveConnections这类无法导出的指标,再持久化到存储中:
- 先创建具备Monitoring Reader角色的服务主体,用于API授权
- 编写脚本(以PowerShell为例)实现拉取与存储:
# 配置参数 $tenantId = "你的租户ID" $clientId = "服务主体ID" $clientSecret = "服务主体密钥" $subscriptionId = "订阅ID" $resourceGroupName = "资源组名" $eventHubNamespace = "Event Hub命名空间" $storageAccountName = "存储账户名" $storageContainer = "容器名" $storageKey = "存储账户密钥" # 获取访问令牌 $tokenUri = "https://login.microsoftonline.com/$tenantId/oauth2/token" $tokenBody = @{ grant_type = "client_credentials" client_id = $clientId client_secret = $clientSecret resource = "https://management.azure.com/" } $tokenResponse = Invoke-RestMethod -Uri $tokenUri -Method Post -Body $tokenBody $accessToken = $tokenResponse.access_token # 调用API获取指标 $metricUri = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.EventHub/namespaces/$eventHubNamespace/providers/microsoft.insights/metrics?api-version=2018-01-01&metricnames=ActiveConnections,ThrottledRequests&aggregation=Average" $metricData = Invoke-RestMethod -Uri $metricUri -Headers @{Authorization = "Bearer $accessToken"} # 将指标写入Blob存储 $blobName = "eventhub-metrics-$(Get-Date -Format 'yyyyMMddHHmm').json" $metricData | ConvertTo-Json | Out-File -FilePath "./temp-metric.json" az storage blob upload --account-name $storageAccountName --account-key $storageKey --container-name $storageContainer --file "./temp-metric.json" --name $blobName - 用Azure自动化账户或GitHub Actions设置定时任务(如每5分钟执行一次),实现持续采集
方法2:基于Azure Functions托管式采集
用Azure Functions的定时触发器替代脚本的定时逻辑,实现更轻量化的托管采集:
- 创建定时触发的Function,在代码中调用Monitor指标API
- 将拉取到的指标数据直接写入目标存储(Blob存储、Cosmos DB、ADLS Gen2等)
- 优势:无需维护服务器,自动伸缩,与Azure生态集成更顺畅
方法3:借助第三方监控栈代理采集
如果已有Prometheus+Grafana监控体系,可通过azure-metrics-exporter拉取Event Hub指标:
- 配置exporter指定Event Hub命名空间的资源ID,开启目标指标采集
- 通过Prometheus的remote write功能,将数据写入Azure存储或时序数据库,供后续分析和面板使用
关键注意事项
- 控制API调用频率,避免触发Azure的配额限流
- 存储数据时按时间分区(如按天/小时创建文件夹),方便后续分析查询
- 对
ActiveConnections这类实时性要求高的指标,可将采集间隔缩短至1分钟
内容的提问来源于stack exchange,提问作者Alezis
相关产品推荐
相关产品推荐

