求助:如何通过PowerShell设置Application Insights数据采样限制为12.5%?
调整Application Insights数据采样至12.5%的PowerShell解决方案
我之前也碰到过这个困惑——Azure并没有提供专门的Set-AzApplicationInsightsSampling这类直接命令,得通过修改资源属性来实现。下面是亲测有效的步骤:
方法一:通过Az.Resources模块修改资源配置
这是最直接的PowerShell方式,通过更新Application Insights资源的samplingSettings属性来调整采样率:
准备工作:确保你安装了最新的Az模块并登录Azure账号
# 安装Az模块(首次使用) Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force -AllowClobber # 登录Azure账号 Connect-AzAccount获取目标Application Insights资源
替换下面的资源组名称和AI资源名称:$resourceGroupName = "你的资源组名称" $appInsightsName = "你的Application Insights资源名称" # 获取AI资源对象 $appInsights = Get-AzResource -ResourceGroupName $resourceGroupName -ResourceName $appInsightsName -ResourceType "Microsoft.Insights/components"更新采样设置
这里我们设置固定采样率为12.5%:# 如果资源已有samplingSettings属性,直接修改;如果没有则添加 if ($appInsights.Properties.ContainsKey("samplingSettings")) { $appInsights.Properties.samplingSettings.percentage = 12.5 } else { $appInsights.Properties | Add-Member -MemberType NoteProperty -Name "samplingSettings" -Value @{ percentage = 12.5 samplingType = "FixedRate" # 固定采样模式,也可根据需求改为"Adaptive" } -Force }提交更新到Azure
Set-AzResource -ResourceId $appInsights.ResourceId -Properties $appInsights.Properties -Force
验证是否生效
执行完命令后,你可以再次获取资源对象来确认采样设置已经更新:
$updatedAppInsights = Get-AzResource -ResourceGroupName $resourceGroupName -ResourceName $appInsightsName -ResourceType "Microsoft.Insights/components" $updatedAppInsights.Properties.samplingSettings
或者直接登录Azure门户,进入你的Application Insights资源,在“配置”>“数据采样”里查看是否已经显示12.5%的采样率。
注意事项
- 如果使用自适应采样(
samplingType = "Adaptive"),除了percentage还可以配置其他参数,比如maxTelemetryItemsPerSecond,你可以根据需求调整。 - 确保你的账号有足够的权限(比如Contributor角色)来修改Application Insights资源。
内容的提问来源于stack exchange,提问作者Koti
相关产品推荐
相关产品推荐

