如何从Azure应用洞察探查器自动拉取DIAGSESSION/ETW/ETL文件至流水线
从Azure Application Insights Profiler自动拉取DIAGSESSION/ETW/ETL文件
核心结论
可以通过Azure REST API或Azure PowerShell模块自动拉取这些文件,无需手动浏览器操作,完全适配流水线或VM/代理执行场景。
具体操作步骤
1. 准备权限与身份验证
- 确保用于自动化的服务主体(或托管身份)拥有Application Insights Contributor或Monitoring Reader权限,至少能访问目标Application Insights资源的探查器数据。
- 若使用Azure PowerShell,可通过服务主体登录方式完成认证:
$tenantId = "你的租户ID" $clientId = "服务主体客户端ID" $clientSecret = ConvertTo-SecureString "服务主体密钥" -AsPlainText -Force $credential = New-Object System.Management.Automation.PSCredential($clientId, $clientSecret) Connect-AzAccount -ServicePrincipal -Credential $credential -Tenant $tenantId
2. 定位目标探查器会话ID
首先需要筛选对应测试时间段内的探查器会话,可通过PowerShell查询:
$resourceGroupName = "你的资源组名" $appInsightsName = "你的Application Insights实例名" # 查询最近的探查器会话(可按时间范围过滤) $profilerSessions = Get-AzApplicationInsightsProfiler -ResourceGroupName $resourceGroupName -Name $appInsightsName # 筛选目标会话(比如匹配测试执行的时间窗口) $targetSession = $profilerSessions | Where-Object { $_.StartTime -ge (Get-Date).AddHours(-1) } | Select-Object -First 1 $sessionId = $targetSession.SessionId
3. 自动下载DIAGSESSION文件
通过REST API直接调用下载,PowerShell示例如下:
$subscriptionId = "你的订阅ID" $apiVersion = "2020-10-01" # 构建下载请求URL $downloadUrl = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/microsoft.insights/components/$appInsightsName/profilerSessions/$sessionId/export?api-version=$apiVersion" # 获取Azure访问令牌 $token = (Get-AzAccessToken -ResourceUrl "https://management.azure.com").Token # 执行下载并保存到本地 Invoke-RestMethod -Uri $downloadUrl -Headers @{Authorization = "Bearer $token"} -OutFile "profiler-test-session.diagsession"
注:导出的DIAGSESSION文件包含ETW/ETL数据,可直接用PerfView或WPA打开分析。
4. 集成到CI/CD流水线
- 在Azure DevOps、GitHub Actions等流水线中,将上述脚本作为PowerShell任务执行,确保代理已安装Azure PowerShell模块。
- 若使用托管身份(如Azure DevOps托管代理),可直接用
Connect-AzAccount -Identity完成认证,无需存储服务主体密钥。
5. 结合PerfView/WPA自动化分析
- 用PowerShell调用PerfView命令行工具加载文件并导出分析数据:
# 示例:导出指定进程的CPU使用率分析结果 & "PerfView.exe" /nogui /output:analysis-results /processName:your-api-process.exe analyze profiler-test-session.diagsession - 针对WPA,可利用其命令行接口自动化生成特定图表,比如响应时长分布、错误关联事件的可视化报表。
注意事项
- 探查器会话数据仅保留30天,需确保在有效期内拉取。
- 代理/VM需允许访问Azure管理API(
management.azure.com),若有防火墙需配置对应规则。 - 若需关联高错误率、长响应请求的会话,可先通过Application Insights日志查询筛选目标时间段的异常数据,再匹配对应的探查器会话ID。
内容的提问来源于stack exchange,提问作者itsme017
相关产品推荐
相关产品推荐

