寫入日誌到 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. 預設情況下,應用程式預設憑證用於取得憑證。如果您想使用自己的服務帳戶,您也可以在 [logging] 區段中設定 google_key_path 選項。

  2. 請確保使用這些憑證,您可以讀取和寫入在 remote_base_log_folder 中定義的 Google Cloud Storage 儲存桶。

  3. 安裝 google 套件,如下所示:pip install 'apache-airflow[google]'

  4. 重新啟動 Airflow 網頁伺服器和排程器,並觸發(或等待)新的任務執行。

  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 選項,以指定服務帳戶金鑰檔案的路徑。如果省略,將使用基於應用程式預設憑證的身份驗證和授權。請確保使用這些憑證,您可以讀取和寫入日誌。

注意

上述憑證與您使用 google_cloud_default 連線配置的憑證不同。它們通常應該與 google_cloud_default 憑證不同,僅具有讀取和寫入日誌的功能。出於安全原因,限制日誌讀取器的存取權限僅允許日誌讀取和寫入是一項重要的安全措施。

此條目是否有幫助?