Aaryan123456679 commented on code in PR #69270:
URL: https://github.com/apache/airflow/pull/69270#discussion_r4163649202
##########
shared/observability/src/airflow_shared/observability/metrics/stats.py:
##########
@@ -102,19 +103,63 @@ def initialize(
factory: Callable[[], StatsLogger | NoStatsLogger],
export_legacy_names: bool,
) -> None:
- """Initialize the stats module with a backend factory and legacy name
configuration."""
+ """
+ Explicitly initialize the stats module with a backend factory and legacy
name configuration.
+
+ This is an eager alternative to the lazy self-configuration in
:func:`_self_configure` — call
+ sites use it so that a metrics misconfiguration surfaces immediately at
component startup
+ (where it can be caught and logged) rather than on the first metric
emission. It only affects
+ *this* module object; see :func:`_self_configure` for how the other copies
of this
+ symlinked file end up configured too.
+ """
global _factory, _backend, _export_legacy_names
_factory = factory
_backend = None
_export_legacy_names = export_legacy_names
+def _self_configure() -> Callable[[], StatsLogger | NoStatsLogger]:
+ """
+ Lazily build this module copy's own factory from its own distribution's
configuration.
+
+ This source file is symlinked into multiple distributions (e.g.
``airflow-core`` and
+ ``task-sdk``), each importing it under a different module name
(``airflow._shared...`` vs
+ ``airflow.sdk._shared...``). Python treats each as a distinct module
object with its own
+ module-level globals, so a process that has both loaded (e.g. the
scheduler, which also runs
+ plugin/listener code that reaches ``Stats`` through the task-sdk path)
needs both copies
+ configured independently, or one of them silently defaults to
``NoStatsLogger``.
+
+ Rather than have one copy reach across into the other's globals once it is
``initialize()``d
+ (fragile: it depends on both copies already being loaded by the time
``initialize()`` runs, so
+ a later/local import of the sibling, or an ``initialize()`` call that
races module loading,
+ could still end up unconfigured), each copy resolves its *own*
configuration independently,
+ on first use, from its own module name: ``__name__`` is
``<root>._shared...`` (``root`` is
+ ``airflow`` or ``airflow.sdk``), and every such root exposes
``<root>.configuration.conf`` and
+ ``<root>.observability.metrics.stats_utils.get_stats_factory()``. This
works regardless of
+ import order, and regardless of whether the other copy is ever loaded at
all.
+
+ Explicit ``initialize()`` calls still take priority over this — they set
``_factory`` directly,
+ so this function only runs when nothing has done that yet.
+ """
+ global _factory, _export_legacy_names
+ root, _, _ = __name__.partition("._shared")
+ try:
+ stats_utils =
import_module(f"{root}.observability.metrics.stats_utils")
Review Comment:
https://github.com/apache/airflow/issues/74072
Raised an issue for the same. Will work on this PR seperately.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]