写入日志到 Google Cloud Storage

远程日志记录到 Google Cloud Storage 使用现有的 Airflow 连接来读取或写入日志。如果您没有正确设置连接,此过程将失败。

按照以下步骤启用 Google Cloud Storage 日志记录。

要启用此功能,必须按照此示例配置 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 = gs://my-bucket/path/to/logs
remote_log_conn_id = my_gcs_conn
  1. 默认情况下,使用 Application Default Credentials 获取凭据。如果您想使用自己的服务账号,也可以在 [logging] 部分设置 google_key_path 选项。

  2. 确保使用这些凭据,您可以对上面在 remote_base_log_folder 中定义的 Google Cloud Storage 存储桶拥有读取和写入访问权限。

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

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

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

  6. 验证 Google Cloud Storage 查看器在 UI 中是否正常工作。打开一个新执行的任务,并验证您是否看到类似以下内容:

*** Reading remote log from gs://<bucket where logs should be persisted>/example_bash_operator/run_this_last/2017-10-03T00:00:00/16.log.
[2017-10-03 21:57:50,056] {cli.py:377} INFO - Running on host chrisr-00532
[2017-10-03 21:57:50,093] {base_task_runner.py:115} INFO - Running: ['bash', '-c', 'airflow tasks run example_bash_operator run_this_last 2017-10-03T00:00:00 --job-id 47 --raw -S DAGS_FOLDER/example_dags/example_bash_operator.py']
[2017-10-03 21:57:51,264] {base_task_runner.py:98} INFO - Subtask: [2017-10-03 21:57:51,263] {__init__.py:45} INFO - Using executor SequentialExecutor
[2017-10-03 21:57:51,306] {base_task_runner.py:98} INFO - Subtask: [2017-10-03 21:57:51,306] {models.py:186} INFO - Filling up the DagBag from /airflow/dags/example_dags/example_bash_operator.py

注意,远程日志文件的路径列在第一行。

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

remote_base_log_folder 选项包含指定要使用的处理程序类型的 URL。为了与 GCS 集成,此选项应以 gs:// 开头。URL 的路径部分指定了 GCS 中日志对象的存储桶和前缀。gs://my-bucket/path/to/logs 会将日志写入 my-bucket 中,并带有 path/to/logs 前缀。

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

注意

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

本条目是否有帮助?