Hey, guys,
I need to know who ran a DAG on the GCP composer airflow(Version 1 and 2).
Do you have any tips for knowing which user ran a dag?
Solved! Go to Solution.
Tracking the specific user who triggered a DAG run in Google Cloud Composer presents challenges due to Airflow's design and Composer's logging capabilities. However, here are some strategies and workarounds to gain insights into DAG triggering:
Challenges:
Workarounds and Best Practices:
os.getlogin()
has limitations in Composer, consider:
Security and Privacy Considerations:
Example: Improved Custom Logging Task
import os
from airflow import DAG
from airflow.operators.python import PythonOperator
from datetime import datetime
from airflow.utils.log.logging_mixin import LoggingMixin
def log_dag_trigger_info():
potential_username = os.environ.get('USER', 'unknown')
# Consider capturing additional info, such as IP address or DAG ID
logger = LoggingMixin().log
logger.info("DAG Triggered by: %s", potential_username)
with DAG('example_dag',
start_date=datetime(2021, 1, 1),
catchup=False) as dag:
log_info_task = PythonOperator(
task_id='log_trigger_info',
python_callable=log_dag_trigger_info
)
# Include the rest of your DAG tasks here
os.getlogin()
Limitations: Addresses issues and suggests alternatives for Composer.Tracking the specific user who triggered a DAG run in Google Cloud Composer presents challenges due to Airflow's design and Composer's logging capabilities. However, here are some strategies and workarounds to gain insights into DAG triggering:
Challenges:
Workarounds and Best Practices:
os.getlogin()
has limitations in Composer, consider:
Security and Privacy Considerations:
Example: Improved Custom Logging Task
import os
from airflow import DAG
from airflow.operators.python import PythonOperator
from datetime import datetime
from airflow.utils.log.logging_mixin import LoggingMixin
def log_dag_trigger_info():
potential_username = os.environ.get('USER', 'unknown')
# Consider capturing additional info, such as IP address or DAG ID
logger = LoggingMixin().log
logger.info("DAG Triggered by: %s", potential_username)
with DAG('example_dag',
start_date=datetime(2021, 1, 1),
catchup=False) as dag:
log_info_task = PythonOperator(
task_id='log_trigger_info',
python_callable=log_dag_trigger_info
)
# Include the rest of your DAG tasks here
os.getlogin()
Limitations: Addresses issues and suggests alternatives for Composer.Thanks for the tip. I really didn't bother putting this log into the DAG.