將日誌寫入 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] 部分。
預設情況下,使用 Application Default Credentials 來獲取憑據。如果想使用自己的服務賬號,也可以在
[logging]部分設定google_key_path選項。確保使用這些憑據可以讀寫 Stackdriver 日誌。
安裝
google包,如下所示:pip install 'apache-airflow[google]'。重啟 Airflow webserver 和 scheduler,並觸發(或等待)新的任務執行。
驗證在新執行的任務的日誌是否顯示在你定義的儲存桶中。
驗證 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。
Google Stackdriver 外部連結¶
當配置 Airflow 將 Google Stackdriver 用作遠端日誌系統時,Airflow 會自動顯示一個指向它的連結。