ashb commented on a change in pull request #5382: [AIRFLOW-4741] Include Sentry 
for Airflow
URL: https://github.com/apache/airflow/pull/5382#discussion_r291545312
 
 

 ##########
 File path: airflow/contrib/hooks/sentry_hook.py
 ##########
 @@ -0,0 +1,91 @@
+from airflow.hooks.base_hook import BaseHook
+from airflow.utils.db import provide_session
+from airflow.models import TaskInstance
+
+from sentry_sdk.integrations.celery import CeleryIntegration
+from sentry_sdk.integrations.logging import ignore_logger
+from sentry_sdk import configure_scope, add_breadcrumb, init
+
+original_task_init = TaskInstance.__init__
+original_clear_xcom = TaskInstance.clear_xcom_data
+SCOPE_TAGS = frozenset(("task_id", "dag_id", "execution_date", "ds", 
"operator"))
+
+
+@provide_session
+def get_task_instance_attr(self, task_id, attr, session=None):
+    """
+    Retrieve attribute from task.
+    """
+    TI = TaskInstance
+    ti = (
+        session.query(TI)
+        .filter(
+            TI.dag_id == self.dag_id,
+            TI.task_id == task_id,
+            TI.execution_date == self.execution_date,
+        )
+        .all()
+    )
+    if ti:
+        attr = getattr(ti[0], attr)
+    else:
+        attr = None
+    return attr
+
+
+@property
+def ds(self):
+    """
+    Date stamp for task object.
+    """
+    return self.execution_date.strftime("%Y-%m-%d")
+
+
+@provide_session
+def new_clear_xcom(self, session=None):
+    """
+    Add breadcrumbs just before task is executed.
+    """
+    for task in self.task.get_flat_relatives(upstream=True):
+        state = get_task_instance_attr(self, task.task_id, "state")
+        operation = get_task_instance_attr(self, task.task_id, "operator")
 
 Review comment:
   No need to go to the DB for this one
   ```suggestion
           operation = task.__class__.__name__
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to