You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "stiak (via GitHub)" <gi...@apache.org> on 2024/02/27 04:30:13 UTC

[PR] Set job labels for traceability [airflow]

stiak opened a new pull request, #37736:
URL: https://github.com/apache/airflow/pull/37736

   <!--
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at
   
      http://www.apache.org/licenses/LICENSE-2.0
   
    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
    -->
   
   <!--
   Thank you for contributing! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   In case of an existing issue, reference it using one of the following:
   
   closes: #ISSUE
   related: #ISSUE
   
   How to write a good git commit message:
   http://chris.beams.io/posts/git-commit/
   -->
   
   This adds the airflow dag ID and task ID as labels to BigQuery jobs.  This helps trace the jobs back to their corresponding DAG from audit logs and job history.
   
   Let me know if there is a better place to put this than the `_submit_job` call.
   
   <!-- Please keep an empty line above the dashes. -->
   ---
   **^ Add meaningful description above**
   Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information.
   In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
   In case of backwards incompatible changes please leave a note in a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [newsfragments](https://github.com/apache/airflow/tree/main/newsfragments).
   


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Set job labels for traceability [airflow]

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #37736:
URL: https://github.com/apache/airflow/pull/37736#discussion_r1513976178


##########
airflow/providers/google/cloud/operators/bigquery.py:
##########
@@ -2768,11 +2771,26 @@ def prepare_template(self) -> None:
             with open(self.configuration) as file:
                 self.configuration = json.loads(file.read())
 
+    def _add_job_labels(self) -> None:
+        dag_label = self.dag_id.lower()
+        task_label = self.task_id.lower()
+
+        if LABEL_REGEX.match(dag_label) and LABEL_REGEX.match(task_label):
+            automatic_labels = {"airflow-dag": dag_label, "airflow-task": task_label}
+            if "labels" in self.configuration:
+                if isinstance(self.configuration["labels"], dict):

Review Comment:
   How about `isinstance(self.configuration.get("labels"), dict)`?



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Set job labels for traceability [airflow]

Posted by "stiak (via GitHub)" <gi...@apache.org>.
stiak commented on code in PR #37736:
URL: https://github.com/apache/airflow/pull/37736#discussion_r1505112975


##########
airflow/providers/google/cloud/operators/bigquery.py:
##########
@@ -2773,6 +2773,14 @@ def _submit_job(
         hook: BigQueryHook,
         job_id: str,
     ) -> BigQueryJob:
+        # Annotate the job with dag and task id labels
+        if "labels" in self.configuration:
+            if isinstance(self.configuration["labels"], dict):
+                self.configuration["labels"]["airflow-dag"] = self.dag_id
+                self.configuration["labels"]["airflow-task"] = self.task_id

Review Comment:
   Added a pattern match



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Set job labels for traceability [airflow]

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #37736:
URL: https://github.com/apache/airflow/pull/37736#discussion_r1512996161


##########
airflow/providers/google/cloud/operators/bigquery.py:
##########
@@ -2768,11 +2772,27 @@ def prepare_template(self) -> None:
             with open(self.configuration) as file:
                 self.configuration = json.loads(file.read())
 
+    def _add_job_labels(self) -> None:
+        dag_label = self.dag_id.lower()
+        task_label = self.task_id.lower()
+
+        if len(dag_label) <= LABEL_SIZE_LIMIT and len(task_label) <= LABEL_SIZE_LIMIT:
+            if LABEL_REGEX.match(dag_label) and LABEL_REGEX.match(task_label):
+                if "labels" in self.configuration:
+                    if isinstance(self.configuration["labels"], dict):

Review Comment:
   These checks are too naive—I would instead do something like
   
   ```python
   try:
       self.configuration["labels"].upgrade({"airflow-dag": dag_label, "airflow-task": task_label})
   except (KeyError, TypeError, ValueError):
       self.configuration["labels"] = {"airflow-dag": dag_label, "airflow-task": task_label}
   ```



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Set job labels for traceability in BigQuery jobs [airflow]

Posted by "stiak (via GitHub)" <gi...@apache.org>.
stiak commented on PR #37736:
URL: https://github.com/apache/airflow/pull/37736#issuecomment-1998839920

   Looks like there was maybe a timeout in one of the build steps.  Is there anything I need to do?


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Set job labels for traceability [airflow]

Posted by "stiak (via GitHub)" <gi...@apache.org>.
stiak commented on code in PR #37736:
URL: https://github.com/apache/airflow/pull/37736#discussion_r1515232407


##########
airflow/providers/google/cloud/operators/bigquery.py:
##########
@@ -2768,11 +2771,26 @@ def prepare_template(self) -> None:
             with open(self.configuration) as file:
                 self.configuration = json.loads(file.read())
 
+    def _add_job_labels(self) -> None:
+        dag_label = self.dag_id.lower()
+        task_label = self.task_id.lower()
+
+        if LABEL_REGEX.match(dag_label) and LABEL_REGEX.match(task_label):
+            automatic_labels = {"airflow-dag": dag_label, "airflow-task": task_label}
+            if "labels" in self.configuration:
+                if isinstance(self.configuration["labels"], dict):

Review Comment:
   Yep, this makes the conditions more explicit and concise, thanks!



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Set job labels for traceability in BigQuery jobs [airflow]

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk merged PR #37736:
URL: https://github.com/apache/airflow/pull/37736


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Set job labels for traceability [airflow]

Posted by "stiak (via GitHub)" <gi...@apache.org>.
stiak commented on code in PR #37736:
URL: https://github.com/apache/airflow/pull/37736#discussion_r1505079476


##########
airflow/providers/google/cloud/operators/bigquery.py:
##########
@@ -2773,6 +2773,14 @@ def _submit_job(
         hook: BigQueryHook,
         job_id: str,
     ) -> BigQueryJob:
+        # Annotate the job with dag and task id labels
+        if "labels" in self.configuration:
+            if isinstance(self.configuration["labels"], dict):
+                self.configuration["labels"]["airflow-dag"] = self.dag_id
+                self.configuration["labels"]["airflow-task"] = self.task_id

Review Comment:
   https://cloud.google.com/bigquery/docs/labels-intro#requirements
   Good point, there are some restrictions outline here.  I'll take a look.



##########
airflow/providers/google/cloud/operators/bigquery.py:
##########
@@ -2773,6 +2773,14 @@ def _submit_job(
         hook: BigQueryHook,
         job_id: str,
     ) -> BigQueryJob:
+        # Annotate the job with dag and task id labels
+        if "labels" in self.configuration:
+            if isinstance(self.configuration["labels"], dict):
+                self.configuration["labels"]["airflow-dag"] = self.dag_id
+                self.configuration["labels"]["airflow-task"] = self.task_id

Review Comment:
   https://cloud.google.com/bigquery/docs/labels-intro#requirements
   Good point, there are some restrictions outlined here.  I'll take a look.



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Set job labels for traceability [airflow]

Posted by "eladkal (via GitHub)" <gi...@apache.org>.
eladkal commented on code in PR #37736:
URL: https://github.com/apache/airflow/pull/37736#discussion_r1503669245


##########
airflow/providers/google/cloud/operators/bigquery.py:
##########
@@ -2773,6 +2773,14 @@ def _submit_job(
         hook: BigQueryHook,
         job_id: str,
     ) -> BigQueryJob:
+        # Annotate the job with dag and task id labels
+        if "labels" in self.configuration:
+            if isinstance(self.configuration["labels"], dict):
+                self.configuration["labels"]["airflow-dag"] = self.dag_id
+                self.configuration["labels"]["airflow-task"] = self.task_id

Review Comment:
   Do labels support all chars?



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Set job labels for traceability [airflow]

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #37736:
URL: https://github.com/apache/airflow/pull/37736#discussion_r1512992579


##########
airflow/providers/google/cloud/operators/bigquery.py:
##########
@@ -2768,11 +2772,27 @@ def prepare_template(self) -> None:
             with open(self.configuration) as file:
                 self.configuration = json.loads(file.read())
 
+    def _add_job_labels(self) -> None:
+        dag_label = self.dag_id.lower()
+        task_label = self.task_id.lower()
+
+        if len(dag_label) <= LABEL_SIZE_LIMIT and len(task_label) <= LABEL_SIZE_LIMIT:
+            if LABEL_REGEX.match(dag_label) and LABEL_REGEX.match(task_label):

Review Comment:
   You can merge these checks with `^[a-z][\w-]{0,63}$`



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Set job labels for traceability [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #37736:
URL: https://github.com/apache/airflow/pull/37736#discussion_r1503875260


##########
airflow/providers/google/cloud/operators/bigquery.py:
##########
@@ -2773,6 +2773,14 @@ def _submit_job(
         hook: BigQueryHook,
         job_id: str,
     ) -> BigQueryJob:
+        # Annotate the job with dag and task id labels
+        if "labels" in self.configuration:
+            if isinstance(self.configuration["labels"], dict):
+                self.configuration["labels"]["airflow-dag"] = self.dag_id
+                self.configuration["labels"]["airflow-task"] = self.task_id

Review Comment:
   We should do a pattern match here so that no character that is not allowed in `configuration` goes in



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Set job labels for traceability [airflow]

Posted by "stiak (via GitHub)" <gi...@apache.org>.
stiak commented on code in PR #37736:
URL: https://github.com/apache/airflow/pull/37736#discussion_r1505111633


##########
airflow/providers/google/cloud/operators/bigquery.py:
##########
@@ -2773,6 +2773,14 @@ def _submit_job(
         hook: BigQueryHook,
         job_id: str,
     ) -> BigQueryJob:
+        # Annotate the job with dag and task id labels
+        if "labels" in self.configuration:
+            if isinstance(self.configuration["labels"], dict):
+                self.configuration["labels"]["airflow-dag"] = self.dag_id
+                self.configuration["labels"]["airflow-task"] = self.task_id

Review Comment:
   I've added length and content checks according to these docs.  The labels won't be added unless everything is valid.



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Set job labels for traceability [airflow]

Posted by "stiak (via GitHub)" <gi...@apache.org>.
stiak commented on code in PR #37736:
URL: https://github.com/apache/airflow/pull/37736#discussion_r1513643096


##########
airflow/providers/google/cloud/operators/bigquery.py:
##########
@@ -2768,11 +2772,27 @@ def prepare_template(self) -> None:
             with open(self.configuration) as file:
                 self.configuration = json.loads(file.read())
 
+    def _add_job_labels(self) -> None:
+        dag_label = self.dag_id.lower()
+        task_label = self.task_id.lower()
+
+        if len(dag_label) <= LABEL_SIZE_LIMIT and len(task_label) <= LABEL_SIZE_LIMIT:
+            if LABEL_REGEX.match(dag_label) and LABEL_REGEX.match(task_label):
+                if "labels" in self.configuration:
+                    if isinstance(self.configuration["labels"], dict):

Review Comment:
   I've switched to `update` for brevity, but I'm not keen on coding by exception so I'm still checking I have a dict first.  This also means it respects a `"labels": None` value.



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org