Identify Task's Own Log Web Address

I’m running a task that will potentially emit an alert (via webhook). In the message I send in the webhook, I’d like to link directly to the task that is emitting the alert (as it also detects the error state and has useful logs).

I realized that I don’t even know how to identify the webserver for a currently running task from within the task. I glanced through the macros. What looked most promising, {{ ti.hostname }} ended up carrying information specific to where I launched the pod rather than about the Airflow deployment itself (task is executed via a K8sOperator).

I’m sure I could just set a variable for the deployment manually - but it seems like there probably is a better ‘right’ location that carries this information.

Any ideas?

Here is what I settled on… for future me (and maybe you).

from airflow import configuration

log_url = configuration.conf.get("webserver", "base_url") + # this coming from airflow.cfg, and is automatically set correctly by Astronomer
  "/log?execution_date={{ ts | urlencode }}" + # Jinja has these inline, functions, nice, right? 
  f"&dag_id={{ dag.dag_id }}&task_id={{ task.task_id }}"

execution_date needed the :'s and + escaped, but if we tried to use urlencode of a dict with Jinja strings, it would just escape the }'s rather than doing the string substition.

I’m still hopeful that someone else has a more appealing solution!

The task instance class has a property called log_url. Is that what you’re looking for?

I use {{ ti.log_url }} in a template in one of my DAGs


Yes, that’s perfect, thank you!

1 Like