-
Notifications
You must be signed in to change notification settings - Fork 17.4k
Centralise structlog initialisation in BaseDagBundle #69859
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
Open
dabla
wants to merge
9
commits into
apache:main
Choose a base branch
from
dabla:feature/make-basedagbundle-logging-dry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7d323f7
refactor: Centralise structlog initialisation in BaseDagBundle
dabla 1b7170f
refactor: Implemented BaseDagBundle in common-compat so older Airflow…
dabla 49785d9
refactor: Skip dag bundle test if import of bundles doesn't exist
dabla cbb18e1
refactor: Check if BaseDagBundle has a _log property as checking exis…
dabla 8d574f0
Merge branch 'main' into feature/make-basedagbundle-logging-dry
dabla 4f3b45a
Update airflow-core/src/airflow/dag_processing/bundles/base.py
dabla 1322642
Fix _log setter, naming, types and test isolation in BaseDagBundle
dabla 5d34bef
Added missing setter on log for backward compatible BaseDagBundle in …
dabla 7968931
Merge branch 'main' into feature/make-basedagbundle-logging-dry
dabla File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
providers/common/compat/src/airflow/providers/common/compat/bundles/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # 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. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import inspect | ||
|
|
||
| import structlog | ||
|
|
||
| from airflow.dag_processing.bundles.base import BaseDagBundle as _BaseDagBundle | ||
|
|
||
| if isinstance(inspect.getattr_static(_BaseDagBundle, "_log", None), property): | ||
| # Modern Airflow already provides BaseDagBundle._log – use it as-is. | ||
| BaseDagBundle = _BaseDagBundle | ||
| else: | ||
| # Older Airflow: inject _log via a transparent subclass so that all | ||
| # bundle implementations can use self._log without version checks. | ||
| class BaseDagBundle(_BaseDagBundle): # type: ignore[no-redef] | ||
| """Drop-in replacement for BaseDagBundle with a back-filled _log property.""" | ||
|
|
||
| @property | ||
| def _log(self): | ||
| """Lazy structlog logger bound with common bundle context.""" | ||
| if not hasattr(self, "_compat_structlog"): | ||
| log_context = self._get_log_context() if hasattr(self, "_get_log_context") else {} | ||
| self._compat_structlog = structlog.get_logger(type(self).__module__).bind( | ||
| bundle_name=self.name, | ||
| version=self.version, | ||
| **log_context, | ||
| ) | ||
| return self._compat_structlog | ||
|
|
||
| @_log.setter | ||
| def _log(self, value) -> None: | ||
| self._compat_structlog = value | ||
|
|
||
|
|
||
| __all__ = ["BaseDagBundle"] |
16 changes: 16 additions & 0 deletions
16
providers/common/compat/tests/unit/common/compat/bundles/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # 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. |
103 changes: 103 additions & 0 deletions
103
providers/common/compat/tests/unit/common/compat/bundles/test_dag_bundle.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| # 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. | ||
| from __future__ import annotations | ||
|
|
||
| import importlib | ||
| from pathlib import Path | ||
| from unittest import mock | ||
|
|
||
| import pytest | ||
| import structlog.testing | ||
|
|
||
| pytest.importorskip("airflow.dag_processing.bundles", reason="Requires Airflow 3+") | ||
|
|
||
| from airflow.dag_processing.bundles.base import BaseDagBundle as CoreBaseDagBundle | ||
| from airflow.providers.common.compat.bundles import BaseDagBundle | ||
|
|
||
| from tests_common.test_utils.config import conf_vars | ||
|
|
||
|
|
||
| def _make_bundle_class(base): | ||
| """Return a concrete bundle subclass with the given base.""" | ||
|
|
||
| class _DummyBundle(base): | ||
| @property | ||
| def path(self) -> Path: | ||
| return Path("/tmp") | ||
|
|
||
| def initialize(self) -> None: | ||
| pass | ||
|
|
||
| def get_current_version(self): | ||
| return None | ||
|
|
||
| def refresh(self) -> None: | ||
| pass | ||
|
|
||
| return _DummyBundle | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def legacy_compat_base(): | ||
| """Yield the compat BaseDagBundle as it would appear on an older Airflow without _log.""" | ||
| import airflow.providers.common.compat.bundles as mod | ||
|
|
||
| with mock.patch.object(CoreBaseDagBundle, "_log", None, create=True): | ||
| importlib.reload(mod) | ||
| yield mod.BaseDagBundle | ||
| # Reload again outside the patch so the real module is back in sys.modules. | ||
| importlib.reload(mod) | ||
|
|
||
|
|
||
| class TestDagBundleCompat: | ||
| def test_base_dag_bundle_is_importable(self): | ||
| assert BaseDagBundle is not None | ||
|
|
||
| def test_base_dag_bundle_is_subclass_of_core(self): | ||
| assert issubclass(BaseDagBundle, CoreBaseDagBundle) | ||
|
|
||
| def test_has_log_property(self): | ||
| assert hasattr(BaseDagBundle, "_log") | ||
|
|
||
| def test_log_property_on_subclass_instance(self, tmp_path): | ||
| """_log returns a bound structlog logger on concrete bundle subclasses.""" | ||
| DummyBundle = _make_bundle_class(BaseDagBundle) | ||
| with conf_vars({("dag_processor", "dag_bundle_storage_path"): str(tmp_path)}): | ||
| bundle = DummyBundle(name="test-bundle") | ||
|
|
||
| first = bundle._log | ||
| assert first is not None | ||
| assert bundle._log is first # cached | ||
|
|
||
| def test_log_is_bound_with_context(self, tmp_path): | ||
| """_log includes bundle_name and version in its bound variables.""" | ||
| DummyBundle = _make_bundle_class(BaseDagBundle) | ||
| with conf_vars({("dag_processor", "dag_bundle_storage_path"): str(tmp_path)}): | ||
| bundle = DummyBundle(name="my-bundle", version="abc123") | ||
|
|
||
| with structlog.testing.capture_logs() as cap: | ||
| bundle._log.info("test event") | ||
| assert cap[0]["bundle_name"] == "my-bundle" | ||
| assert cap[0]["version"] == "abc123" | ||
|
|
||
| def test_compat_subclass_provides_log_when_missing(self, tmp_path, legacy_compat_base): | ||
| """Even when the installed BaseDagBundle has no _log, the compat class fills it in.""" | ||
| DummyBundle = _make_bundle_class(legacy_compat_base) | ||
| with conf_vars({("dag_processor", "dag_bundle_storage_path"): str(tmp_path)}): | ||
| bundle = DummyBundle(name="legacy-bundle") | ||
|
|
||
| assert bundle._log is not None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.