如何将正确的日志记录级别设置为 Azure Functions 的 host.json 以优化 Application Insight 的成本?

How to set correct logging levels to host.json of Azure Functions to optimize costs of Application Insight?

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

我好像日志太多了,Application Insight 开销很大。 几乎所有的成本都来自于“消息”。不是来自指标。

我正在尝试修改 host.json,但在本地调试中我的新 host.json 没有提供调试所需的信息。

当我修改日志记录时,我在命令行中看不到任何这些日志记录:

   logging.info(f" Calling Activity Function")

我看到很多不相关的消息,例如(我正在开发 Azure Durable Function)

 testhubname-control-03: Starting lease renewal with token 2c336948-1daa-49db-ae7e-9989cc340461

原始(记录太多到 Application Insight)

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[2.*, 3.0.0)"
  }
}

新(我在命令行中看不到 logging.info(f" 调用 Activity 函数") 结果)

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    },
    "logLevel": {
      "default": "Information",
      "Host.Results": "Error",
      "Function": "Error",
      "Host.Aggregator": "Information"
    }
    
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[2.*, 3.0.0)"
  }
}

https://docs.microsoft.com/en-us/azure/azure-functions/configure-monitoring?tabs=v2

还尝试在 applicationInsight 设置下添加 LogLevel,但未接受“LogLevel”。

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      },
      "LogLevel": {
        "Default": "Warning"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[2.*, 3.0.0)"
  }
}

也试过没有成功:(属性不允许错误发生)

{
  "version": "2.0",
  "ApplicationInsights": {
    "InstrumentationKey": "my-instrumentation-key",
    "EnableAdaptiveSampling": false,
    "EnablePerformanceCounterCollectionModule": false
  },
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Information"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[2.*, 3.0.0)"
  }
}

其中一个解决方法是解决此问题:

  1. 在 VS Code 中创建了 Azure Functions(Python 堆栈)。
  2. 在Activity函数代码中添加了这段代码logging.info(f" Calling Activity Function"),如下所示:

默认情况下,这是 host.json 代码:

"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
        }
    }
}

当我运行这个持久函数使用本地存储模拟器时,它正在记录信息级别:

如果我更改 host.json 代码以包含您在问题中给出的代码,那么我无法在终端中获得 Calling Activity Function logging information:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    },
    "logLevel": {
      "default": "Information",
      "Host.Results": "Error",
      "Function": "Error",
      "Host.Aggregator": "Information"
    }   
  }

修改了 host.json 信息级别日志代码属性值,然后修改了 运行 函数,它正在记录在 activity 函数代码中指定的信息,如下所示:

此外,当Host.Results值为ErrorFunction值为Information级别时,在host.json中的日志代码中也会得到相同的结果:

host.json 中定义的日志记录级别属性值存在差异,例如:

  1. 对于 Host.ResultsFunction 的日志,仅记录 Error 或更高级别的事件。
  2. 对于 Host.Aggregator 的日志,记录所有生成的指标 (Trace)。
  3. 对于所有其他日志,包括用户日志,仅记录 Information 级别及更高级别的事件。

您的代码:

"logLevel": {
      "default": "Information",
      "Host.Results": "Error",
      "Function": "Error",
      "Host.Aggregator": "Information"
    }
  • 如果Host.Results类别设置为Error日志级别,它只会收集主机执行遥测requests table 中的事件 函数执行失败 .
  • 如果Function类别设置为Error日志级别,它将停止收集函数遥测数据与所有函数的 dependenciescustomMetricscustomEvents 相关,从而阻止在 Application Insights 中查看任何此类数据。它只会收集 traces 级别 Error 的记录。

要优化 Application Insights 的成本,您可以将 host.json 日志记录级别设置为收集到足够的数据,以便使用为日志级别中定义的每个属性设置的不同值来了解您的函数行为。

Microsoft 文档中提供的技术很少用于控制监视函数应用程序的潜在成本的影响,例如采样、每日上限设置、预聚合指标、自定义日志收集。

参考:Log Level Configuration and Potential Costs Impact Techniques in Azure Functions

更新答案:

How to configure host.json so that I can set different log levels to AI and the local environment? I need to optimize the number of logs in AI. Could you provide an example?

为了尽量减少日志数量,您可以在 host.json 中使用更高的日志记录级别,如下面的屏幕截图所示:

可以看到这里的logs最小化函数信息log并没有产生输出log在浏览器中,只有手动logging在logs/terminal.

中显示