我有 Azure Function(Python) 和 Azure Application Insight。 Application Insight 成本巨大,因为它每月获取 200GB 的数据。 我正在尝试追踪问题出在哪里。
我想知道 Azure Function 会记录所有内容并且是否可以进行优化。 我们不需要“信息”。
如何优化 Application Insight for Azure Functions 中的成本?
应用洞察:
logger = logging.getLogger(__name__)
logger.info('Python HTTP trigger function received a request.')
try:
instrumentationKey = os.environ['APPINSIGHTS_INSTRUMENTATIONKEY']
logger.addHandler(AzureLogHandler(
connection_string=f'InstrumentationKey={instrumentationKey}')
)
except Exception as e:
exception_text = f"{e}"
logging.error(f"Could not add Application Insights logging: {exception_text}")
记录使用:
logging.error(f"EXCEPTION: {exception_text}")
logging.info(f" Calling Activity Function")
我想知道 Azure Function 会记录所有内容并且是否可以进行优化。我们不需要“信息”。
1。禁用不需要的模块: 编辑 ApplicationInsights.config 以关闭不需要的收集模块。
Application Insights .NET SDK 由许多 NuGet 包组成,其中包含用于发送遥测数据的 API,还包含用于自动跟踪应用程序及其上下文的遥测模块和初始化程序。 您可以启用或禁用遥测模块和初始化程序,并为其中一些设置参数。
每个模块的配置文件中都有一个节点。要禁用模块,请删除该节点或将其注释掉。
2。动态禁用遥测: 要在代码中的任何位置有条件且动态地禁用遥测,请使用
DisableTelemetry
实例设置 TelemetryConfiguration
标志。
public void ConfigureServices(IServiceCollection services)
{
services.AddApplicationInsightsTelemetry();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, TelemetryConfiguration configuration)
{
configuration.DisableTelemetry = true;
...
}
此代码示例可防止将遥测数据发送到 Application Insights,但不会阻止自动收集模块收集遥测数据,并且为了删除自动收集模块,请参阅此 Microsoft 文档。
3.自定义日志收集:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
},
"ApplicationInsights": {
"LogLevel": {
"Default": "Information"
}
}
}
}
以下配置允许 Application Insights 捕获所有
Information
日志和严重 warning
日志。
要更改此行为,请显式覆盖提供程序的日志记录配置ApplicationInsights
,如下所示:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
}
}
还有一些技术可以管理用于遥测数据优化的数据量,例如:
请查看这些参考资料以获取更多信息:
因为成本而降低你的可观察性是非常不幸的。我对 ApplInsights 的成本感到非常失望,并立即开始寻找替代方案。
就我而言,根据我的要求,拥有带有 Grafana 的 Loki 就足够了,但我的项目和公司都很小。
附注我怀疑 AppInsights 非常昂贵,因为它的技术过时,我猜它使用关系数据库,这对于日志和指标来说是非常值得怀疑的决定。