This is an automated email from the ASF dual-hosted git repository. kamilbregula pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push: new 057cc0a Add in before_send config option to sentry integration (#18261) 057cc0a is described below commit 057cc0a399ce32cd2067e1846e84df7f2b91c77f Author: Leon Smith <_...@leonmarksmith.com> AuthorDate: Wed Sep 29 17:35:39 2021 +0100 Add in before_send config option to sentry integration (#18261) --- airflow/config_templates/config.yml | 8 +++++++- airflow/config_templates/default_airflow.cfg | 5 ++++- airflow/sentry.py | 3 ++- tests/core/test_sentry.py | 28 +++++++++++++++++++++++++++- 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/airflow/config_templates/config.yml b/airflow/config_templates/config.yml index e02ab37..42d9bca 100644 --- a/airflow/config_templates/config.yml +++ b/airflow/config_templates/config.yml @@ -1419,7 +1419,7 @@ additional configuration options based on the Python platform. See: https://docs.sentry.io/error-reporting/configuration/?platform=python. Unsupported options: ``integrations``, ``in_app_include``, ``in_app_exclude``, - ``ignore_errors``, ``before_breadcrumb``, ``before_send``, ``transport``. + ``ignore_errors``, ``before_breadcrumb``, ``transport``. options: - name: sentry_on description: Enable error reporting to Sentry @@ -1433,6 +1433,12 @@ type: string example: ~ default: "" + - name: before_send + description: Dotted path to a before_send function that the sentry SDK should be configured to use. + version_added: 2.2.0 + type: string + example: ~ + default: ~ - name: celery_kubernetes_executor description: | This section only applies if you are using the ``CeleryKubernetesExecutor`` in diff --git a/airflow/config_templates/default_airflow.cfg b/airflow/config_templates/default_airflow.cfg index 230aacc..c7ede49 100644 --- a/airflow/config_templates/default_airflow.cfg +++ b/airflow/config_templates/default_airflow.cfg @@ -702,11 +702,14 @@ smtp_retry_limit = 5 # additional configuration options based on the Python platform. See: # https://docs.sentry.io/error-reporting/configuration/?platform=python. # Unsupported options: ``integrations``, ``in_app_include``, ``in_app_exclude``, -# ``ignore_errors``, ``before_breadcrumb``, ``before_send``, ``transport``. +# ``ignore_errors``, ``before_breadcrumb``, ``transport``. # Enable error reporting to Sentry sentry_on = false sentry_dsn = +# Dotted path to a before_send function that the sentry SDK should be configured to use. +# before_send = + [celery_kubernetes_executor] # This section only applies if you are using the ``CeleryKubernetesExecutor`` in diff --git a/airflow/sentry.py b/airflow/sentry.py index 19d83f6..9cc9f9b 100644 --- a/airflow/sentry.py +++ b/airflow/sentry.py @@ -72,7 +72,6 @@ if conf.getboolean("sentry", 'sentry_on', fallback=False): "in_app_exclude", "ignore_errors", "before_breadcrumb", - "before_send", "transport", ) ) @@ -110,6 +109,8 @@ if conf.getboolean("sentry", 'sentry_on', fallback=False): ", ".join(unsupported_options), ) + sentry_config_opts['before_send'] = conf.getimport('sentry', 'before_send') + if dsn: sentry_sdk.init(dsn=dsn, integrations=integrations, **sentry_config_opts) else: diff --git a/tests/core/test_sentry.py b/tests/core/test_sentry.py index 99fac40..59cc831 100644 --- a/tests/core/test_sentry.py +++ b/tests/core/test_sentry.py @@ -18,6 +18,7 @@ import datetime import importlib +from unittest import mock import pytest from freezegun import freeze_time @@ -25,6 +26,7 @@ from sentry_sdk import configure_scope from airflow.operators.python import PythonOperator from airflow.utils import timezone +from airflow.utils.module_loading import import_string from airflow.utils.state import State from tests.test_utils.config import conf_vars @@ -62,6 +64,10 @@ CRUMB = { } +def before_send(_): + pass + + class TestSentryHook: @pytest.fixture def task_instance(self, dag_maker): @@ -80,8 +86,19 @@ class TestSentryHook: dag_maker.session.rollback() @pytest.fixture + def sentry_sdk(self): + with mock.patch('sentry_sdk.init') as sentry_sdk: + yield sentry_sdk + + @pytest.fixture def sentry(self): - with conf_vars({('sentry', 'sentry_on'): 'True', ('sentry', 'default_integrations'): 'False'}): + with conf_vars( + { + ('sentry', 'sentry_on'): 'True', + ('sentry', 'default_integrations'): 'False', + ('sentry', 'before_send'): 'tests.core.test_sentry.before_send', + }, + ): from airflow import sentry importlib.reload(sentry) @@ -109,3 +126,12 @@ class TestSentryHook: with configure_scope() as scope: test_crumb = scope._breadcrumbs.pop() assert CRUMB == test_crumb + + def test_before_send(self, sentry_sdk, sentry): + """ + Test before send callable gets passed to the sentry SDK. + """ + assert sentry + called = sentry_sdk.call_args[1]['before_send'] + expected = import_string('tests.core.test_sentry.before_send') + assert called == expected