将日志写入 Google Stackdriver

Airflow 可以配置为在 Google Stackdriver Logging 中读取和写入任务日志。

要启用此功能,必须按照此示例配置 airflow.cfg

[logging]
# Airflow can store logs remotely in AWS S3, Google Cloud Storage or Elastic Search.
# Users must supply an Airflow connection id that provides access to the storage
# location. If remote_logging is set to true, see UPDATING.md for additional
# configuration requirements.
remote_logging = True
remote_base_log_folder = stackdriver:///logs-name

所有配置选项都在 [logging] 部分。

  1. 默认情况下,使用 Application Default Credentials 来获取凭据。如果想使用自己的服务账号,也可以在 [logging] 部分设置 google_key_path 选项。

  2. 确保使用这些凭据可以读写 Stackdriver 日志。

  3. 安装 google 包,如下所示: pip install 'apache-airflow[google]'

  4. 重启 Airflow webserver 和 scheduler,并触发(或等待)新的任务执行。

  5. 验证在新执行的任务的日志是否显示在你定义的存储桶中。

  6. 验证 Google Cloud Storage 查看器在 UI 中是否正常工作。使用 Stackdriver,你应该会看到日志实时拉取过来

字段 remote_logging 的值必须始终设置为 True,此功能才能工作。关闭此选项将导致数据不发送到 Stackdriver。

remote_base_log_folder 选项包含一个 URL,用于指定要使用的处理程序类型。为了与 Stackdriver 集成,此选项应以 stackdriver:// 开头。URL 的路径部分指定日志的名称,例如 stackdriver:///airflow-tasks 将日志写入 airflow-tasks 名称下。

可以在 [logging] 部分设置 google_key_path 选项来指定 服务账号密钥文件的路径。如果省略,将使用基于 Application Default Credentials 的身份验证和授权。确保使用这些凭据可以读写日志。

注意

上述凭据与使用 google_cloud_default Connection 配置的凭据不同。它们通常应与 google_cloud_default 凭据不同,仅具有读写日志的能力。出于安全原因,将日志读取器的访问权限限制为仅允许日志读写是一项重要的安全措施。

通过使用 logging_config_class 选项,可以获得此处理程序的高级功能。详细信息可在处理程序的文档中找到 - StackdriverTaskHandler

此条目有帮助吗?