Skip to content

Fix custom stats metrics not being sent from plugins/listeners#69270

Open
Aaryan123456679 wants to merge 3 commits into
apache:mainfrom
Aaryan123456679:fix/plugin-statsd-metrics-scheduler-init-69172
Open

Fix custom stats metrics not being sent from plugins/listeners#69270
Aaryan123456679 wants to merge 3 commits into
apache:mainfrom
Aaryan123456679:fix/plugin-statsd-metrics-scheduler-init-69172

Conversation

@Aaryan123456679

Copy link
Copy Markdown
Contributor

Airflow keeps two independent Stats singletons because of the shared/symlinked library architecture: one internal copy that airflow-core initializes at startup (airflow._shared.observability.metrics.stats), and the task-sdk copy exposed by the officially recommended airflow.sdk.observability.stats import (and the deprecated airflow.stats shim, which re-exports it). Long-running components — scheduler, DAG file processor, API server, executors, triggerer — only initialized the internal copy at startup. The task-sdk copy was only ever initialized inside the task-execution subprocess.

Plugin/listener hooks such as on_dag_run_success/on_dag_run_failed run in the scheduler or API server process, never in the task subprocess, so a plugin calling Stats.gauge(...) via the recommended import path silently fell back to NoStatsLogger — no error, no custom metrics — regardless of StatsD/Datadog/OTel configuration, even though Airflow's own native metrics worked fine.

This adds initialize_sdk_stats_backend() to airflow.observability.metrics.stats_utils, which configures the task-sdk Stats singleton with the same factory/config each process already uses for its own internal singleton, and calls it alongside the existing stats.initialize(...) call in the scheduler, DAG processor, API server, base executor, and triggerer.

closes: #69172

Test plan

  • Added TestInitializeSdkStatsBackend in airflow-core/tests/unit/observability/metrics/test_stats.py verifying the sdk singleton is configured with this process's backend and the same factory/legacy-names flag.
  • Updated existing stats-initialization tests in test_app.py, test_manager.py, test_base_executor.py, test_scheduler_job.py, and test_triggerer_job.py to assert the new call happens alongside the existing one.
  • All affected test files pass locally.
  • Manually reproduced before/after with a scratch script: without the fix, a plugin's Stats.gauge(...) silently resolved to NoStatsLogger; after calling initialize_sdk_stats_backend(), it resolved to SafeStatsdLogger.

Was generative AI tooling used to co-author this PR?
  • Yes — Claude Code (Sonnet 5)

Generated-by: Claude Code (Sonnet 5) following the guidelines

@Aaryan123456679

Copy link
Copy Markdown
Contributor Author
Friendly ping! This PR is ready for review whenever someone has time. Thank you!

@potiuk potiuk added the ready for maintainer review Set after triaging when all criteria pass. label Jul 8, 2026
@Aaryan123456679

Copy link
Copy Markdown
Contributor Author

Friendly ping! This PR is ready for review whenever someone has time. Thank you!

@pierrejeambrun pierrejeambrun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR.

Having calls to initialize_sdk_stats_backend all over the core code base really feels off. We're trying to split server / client and limit the dependencies there. We probably should use a _shared lib.

Comment thread airflow-core/newsfragments/69270.bugfix.rst Outdated
Comment thread airflow-core/src/airflow/observability/metrics/stats_utils.py Outdated
Comment thread airflow-core/src/airflow/executors/base_executor.py Outdated
@pierrejeambrun pierrejeambrun removed the ready for maintainer review Set after triaging when all criteria pass. label Jul 24, 2026
@pierrejeambrun pierrejeambrun added this to the Airflow 3.3.1 milestone Jul 24, 2026
@pierrejeambrun pierrejeambrun added the backport-to-v3-3-test Backport to v3-3-test label Jul 24, 2026
Airflow keeps two independent Stats singletons: one internal to
airflow-core, and the task-sdk copy that airflow.sdk.observability.stats
(and the deprecated airflow.stats shim) expose to plugins and listener
hooks. Long-running components only initialized the internal copy at
startup, so a plugin's DAG-run listener hook running in the scheduler
or API server silently got NoStatsLogger regardless of StatsD config,
even though native Airflow metrics worked fine.

closes: apache#69172
Plugins and listeners typically reach Stats through the task-sdk import
path, a separate singleton from the one each component initializes for
its own internal use, so plugin code kept using an unconfigured
NoStatsLogger even after the component's own Stats was set up.

Propagate the configuration to any sibling copy of the stats module
already loaded under a different distribution path instead of having
core import from airflow.sdk directly to reach it. Metrics
initialization failures no longer prevent the API server, scheduler,
Dag processor, triggerer, or executor from starting.
@Aaryan123456679
Aaryan123456679 force-pushed the fix/plugin-statsd-metrics-scheduler-init-69172 branch from 0de55d0 to 396cb29 Compare July 24, 2026 12:12
@Aaryan123456679

Copy link
Copy Markdown
Contributor Author

Hi @pierrejeambrun , I have done the necessary changes, kindly let me know in case any other change is required. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:API Airflow's REST/HTTP API area:DAG-processing area:Executors-core LocalExecutor & SequentialExecutor area:Scheduler including HA (high availability) scheduler area:Triggerer backport-to-v3-3-test Backport to v3-3-test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot send custom statsd metrics in a plugin

3 participants