For each operator, you can define its own extra links that can redirect users to external systems. The extra link buttons will be available on the task page:
🧛‍♂️Define an operator extra link - 图1
The following code shows how to add extra links to an operator via Plugins:

  1. from airflow.models.baseoperator import BaseOperator, BaseOperatorLink
  2. from airflow.plugins_manager import AirflowPlugin
  3. from airflow.utils.decorators import apply_defaults
  4. class GoogleLink(BaseOperatorLink):
  5. name = 'Google'
  6. def get_link(self, operator, dttm):
  7. return "https://www.google.com"
  8. class MyFirstOperator(BaseOperator):
  9. operator_extra_links = (
  10. GoogleLink(),
  11. )
  12. @apply_defaults
  13. def __init__(self, **kwargs):
  14. super().__init__(**kwargs)
  15. def execute(self, context):
  16. self.log.info("Hello World!")
  17. # Defining the plugin class
  18. class AirflowExtraLinkPlugin(AirflowPlugin):
  19. name = "extra_link_plugin"
  20. operator_extra_links = [GoogleLink(), ]

:::tips 🔖 Note ::: :::info Operator Extra Links should be registered via Airflow Plugins or custom Airflow Provider to work. :::

You can also add a global operator extra link that will be available to all the operators through an airflow plugin or through airflow providers. You can learn more about it in the plugin example and in Provider packages.

1. Add or override Links to Existing Operators

You can also add (or override) an extra link to an existing operators through an Airflow plugin or custom provider.

For example, the following Airflow plugin will add an Operator Link on all tasks using [GCSToS3Operator](https://airflow.apache.org/docs/apache-airflow-providers-amazon/stable/_api/airflow/providers/amazon/aws/transfers/gcs_to_s3/index.html#airflow.providers.amazon.aws.transfers.gcs_to_s3.GCSToS3Operator) operator.

Adding Operator Links to Existing Operators plugins/extra_link.py:

  1. from airflow.plugins_manager import AirflowPlugin
  2. from airflow.models.baseoperator import BaseOperatorLink
  3. from airflow.providers.amazon.aws.transfers.gcs_to_s3 import GCSToS3Operator
  4. class S3LogLink(BaseOperatorLink):
  5. name = 'S3'
  6. # Add list of all the operators to which you want to add this OperatorLinks
  7. # Example: operators = [GCSToS3Operator, GCSToBigQueryOperator]
  8. operators = [GCSToS3Operator]
  9. def get_link(self, operator, dttm):
  10. return 'https://s3.amazonaws.com/airflow-logs/{dag_id}/{task_id}/{execution_date}'.format(
  11. dag_id=operator.dag_id,
  12. task_id=operator.task_id,
  13. execution_date=dttm,
  14. )
  15. # Defining the plugin class
  16. class AirflowExtraLinkPlugin(AirflowPlugin):
  17. name = "extra_link_plugin"
  18. operator_extra_links = [S3LogLink(), ]

12.1 Overriding Operator Links of Existing Operators

It is also possible to replace a built in link on an operator via a Plugin. For example [BigQueryExecuteQueryOperator](https://airflow.apache.org/docs/apache-airflow-providers-google/stable/_api/airflow/providers/google/cloud/operators/bigquery/index.html#airflow.providers.google.cloud.operators.bigquery.BigQueryExecuteQueryOperator) includes a link to the Google Cloud Console, but if we wanted to change that link we could:

  1. from airflow.plugins_manager import AirflowPlugin
  2. from airflow.models.baseoperator import BaseOperatorLink
  3. from airflow.providers.google.cloud.operators.bigquery import BigQueryOperator
  4. # Change from https to http just to display the override
  5. BIGQUERY_JOB_DETAILS_LINK_FMT = 'http://console.cloud.google.com/bigquery?j={job_id}'
  6. class BigQueryConsoleLink(BaseOperatorLink):
  7. """
  8. Helper class for constructing BigQuery link.
  9. """
  10. name = 'BigQuery Console'
  11. operators = [BigQueryOperator]
  12. def get_link(self, operator, dttm):
  13. ti = TaskInstance(task=operator, execution_date=dttm)
  14. job_id = ti.xcom_pull(task_ids=operator.task_id, key='job_id')
  15. return BIGQUERY_JOB_DETAILS_LINK_FMT.format(job_id=job_id) if job_id else ''
  16. # Defining the plugin class
  17. class AirflowExtraLinkPlugin(AirflowPlugin):
  18. name = "extra_link_plugin"
  19. operator_extra_links = [BigQueryConsoleLink(), ]

12.2 Adding Operator Links via Providers

As explained in Provider packages, when you create your own Airflow Provider, you can specify the list of operators that provide extra link capability. This happens by including the operator class name in the provider-info information stored in your Provider’s package meta-data:

Example meta-data required in your provider-info dictionary (this is part of the meta-data returned by apache-airflow-providers-google provider currently:

  1. extra-links:
  2. - airflow.providers.google.cloud.operators.bigquery.BigQueryConsoleLink
  3. - airflow.providers.google.cloud.operators.bigquery.BigQueryConsoleIndexableLink
  4. - airflow.providers.google.cloud.operators.mlengine.AIPlatformConsoleLink

You can include as many operators with extra links as you want.