-
Notifications
You must be signed in to change notification settings - Fork 16.4k
Write action log to DB when DAG run is trigged via API #28998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| dag_hash=get_airflow_app().dag_bag.dags_hash.get(dag_id), | ||
| session=session, | ||
| ) | ||
| from airflow.models.log import Log |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of this, there's action_logging decorator you should use, see
| @action_logging( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ephraimbuddy thanks, my dev env was a bit outdated so I didn't have this decorator.
Updated and changed the way for db insertion. Please review.
| "owner": g.user.username, | ||
| "extra": None, | ||
| "task_id": None, | ||
| "dag_id": f"{dag_run.dag_id}", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the action_logging, do we still need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point was to write the log table. To do so we first need to create Log object. That's what we actually do here.
We don't write to dag_run table, thus cannot use DagRun object as itself.
And also, owner is only accessible from flask.g.
So we need to "construct" Log object
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you look at the code for the action_logging?
airflow/airflow/www/decorators.py
Lines 105 to 112 in be0e353
| log = Log( | |
| event=event or f.__name__, | |
| task_instance=None, | |
| owner=user, | |
| extra=str(extra_fields), | |
| task_id=params.get("task_id"), | |
| dag_id=params.get("dag_id"), | |
| ) |
| @provide_session | ||
| @action_logging( | ||
| event=action_event_from_permission( | ||
| prefix="log", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can have a global constant for this prefix. Check the usage in other places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same, as above. In other places we are logging the "main" object to its correspondent table. For instance, in connection_endpoint.py we are logging the Connection object to connection table. In variable_endpoint.py we are logging the Variable object to variable table.
Here is the difference, in dag_run_endpoint.py we are logging to log table rather than dag_run table.
Based on that I decided not to have prefix in a global constatnt. I mean if we want to add something more to dag_run table we will need RESOURCE_EVENT_PREFIX = "dag_run" rather than RESOURCE_EVENT_PREFIX = "log".
@ephraimbuddy so, the question, is it ok to use RESOURCE_EVENT_PREFIX or it's better to use smth like RESOURCE_EVENT_LOG_PREFIX ?
#example
RESOURCE_EVENT_LOG_PREFIX = "log"
...
@action_logging(
event=action_event_from_permission(
prefix=RESOURCE_EVENT_LOG_PREFIX,
permission=permissions.ACTION_CAN_CREATE,
),
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same, as above. In other places we are logging the "main" object to its correspondent table. For instance, in connection_endpoint.py we are logging the Connection object to connection table. In variable_endpoint.py we are logging the Variable object to variable table.
This is wrong. The logging is all done in the log table through the action_logging decorator.
|
@ephraimbuddy |
We also need a test |
Ok, will update once it's done. |
|
Hi, @ephraimbuddy |
|
Above there was a conflict with the very fresh commit b94f36b in |
(cherry picked from commit edc2e0b)
(cherry picked from commit edc2e0b)
(cherry picked from commit edc2e0b)
Based on #28435
The purpose is to save to
Logtable an airflow user who created a dagrun via REST API.Currently, such event as
trigger(from UI) andcli_dag_trigger(from CLI) are being written to metadb, But the api endpoint dagrun request is missing there.I suggest to add a log entry in dag_run_endpoint.py like this:
'rest_dag_trigger' is discussable, it might be someone could advise a better text. I just named it in the likeness of 'cli_dag_trigger'.
The other columns are obvious.