寫入日誌到 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
預設情況下,使用 Application Default Credentials 獲取憑據。如果您想使用自己的服務賬號,也可以在
[logging]部分設定google_key_path選項。確保使用這些憑據,您可以對上面在
remote_base_log_folder中定義的 Google Cloud Storage 儲存桶擁有讀取和寫入訪問許可權。安裝
google包,例如:pip install 'apache-airflow[google]'。重啟 Airflow webserver 和 scheduler,並觸發(或等待)新的任務執行。
驗證您定義的儲存桶中是否顯示了新執行任務的日誌。
驗證 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 憑據不同,僅具備讀取和寫入日誌的能力。出於安全原因,限制日誌讀取器的訪問許可權,使其僅允許讀取和寫入日誌,是一項重要的安全措施。