如何优化 Azure Functions 的 Application Insight 成本?

问题描述 投票:0回答:2

我有 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-functions azure-application-insights
2个回答
2
投票

我想知道 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"
    }
  }
}

还有一些技术可以管理用于遥测数据优化的数据量,例如:

  • 采样:采样是用于调整发送的数据量的主要工具。 Application Insights 中的采样
  • 每日上限: Application Insights 中的最大上限为 1000 GB/天,除非您为高流量应用程序请求更高的上限。
  • 如果您有基于工作区的 Application Insights 资源,我们建议使用工作区的每日上限来限制摄取和成本,而不是 Application Insights 中的上限。
  • 预聚合指标:如果您在应用程序中调用 TrackMetric,则可以通过使用接受一批测量的平均值和标准差计算的过载来减少流量。或者,您可以使用预聚合包

请查看这些参考资料以获取更多信息:

  1. 解决日志在 Application Insights 中显示两次的问题
  2. 优化 Azure Functions 的日志记录成本
  3. 配置或删除必要的遥测初始化程序

0
投票

因为成本而降低你的可观察性是非常不幸的。我对 ApplInsights 的成本感到非常失望,并立即开始寻找替代方案。

就我而言,根据我的要求,拥有带有 Grafana 的 Loki 就足够了,但我的项目和公司都很小。

附注我怀疑 AppInsights 非常昂贵,因为它的技术过时,我猜它使用关系数据库,这对于日志和指标来说是非常值得怀疑的决定。

© www.soinside.com 2019 - 2024. All rights reserved.