Skip to content

Optionally emit Dag tags as metric tags#68568

Open
sortega wants to merge 3 commits into
apache:mainfrom
DataDog:sortega/airflow-tagged-metrics
Open

Optionally emit Dag tags as metric tags#68568
sortega wants to merge 3 commits into
apache:mainfrom
DataDog:sortega/airflow-tagged-metrics

Conversation

@sortega

@sortega sortega commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Airflow's Dag-run and task-instance metrics carry a small fixed set of tags (dag_id, run_type, task_id, and team_name). Dag authors already annotate their Dags with free-form tags like production or env:prod, but those never reach the metrics — so dashboards and alerts can't be sliced by team, environment, or criticality without hardcoding dag_id lists.

This PR optionally surfaces each Dag tag as an individual metric tag on all Dag-run and task-instance metrics, across the StatsD, DogStatsD and InfluxDB backends:

  • A tag containing : (e.g. env:prod) splits into a key/value pair (envprod).
  • A plain tag (e.g. production) becomes a standalone DogStatsD tag, or production=true in InfluxDB line protocol.
  • Built-in keys (dag_id, run_type, task_id) always win on collision.

Because Dag tags are unbounded, user-defined strings, the behavior is gated behind a new [metrics] dag_tags_in_metrics option, disabled by default, to avoid surprise cardinality increases on existing installations. Tags are read from the in-memory serialized Dag, so no extra DB queries are issued and the worker never touches the metastore.

This is the metrics counterpart of the goal in #37901 (routing observability by Dag tags), and builds on the team_name work from #68108.

related: #37901


Was generative AI tooling used to co-author this PR?
  • Yes (Claude Code (Opus 4.8))

Generated-by: Claude Code (Opus 4.8) following the guidelines

@sortega sortega force-pushed the sortega/airflow-tagged-metrics branch from 80ec8c9 to fe514c9 Compare June 15, 2026 15:09
@sortega sortega marked this pull request as ready for review June 17, 2026 12:13

@ashb ashb 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.

the behavior is gated behind a new [metrics] dag_tags_in_metrics option, disabled by default, to avoid surprise cardinality increases on existing installations

Perfect, I was thinking the same thing as I read the description.

Overall a good PR though

Comment thread airflow-core/src/airflow/config_templates/config.yml Outdated
Comment thread airflow-core/src/airflow/models/dagrun.py Outdated
Comment thread airflow-core/src/airflow/models/taskinstance.py Outdated
Comment thread airflow-core/tests/unit/models/test_dagrun.py Outdated
Comment thread task-sdk/src/airflow/sdk/execution_time/task_runner.py Outdated
Comment thread task-sdk/tests/task_sdk/execution_time/test_task_runner.py Outdated
@sortega sortega force-pushed the sortega/airflow-tagged-metrics branch 3 times, most recently from 36f5aca to 82d305f Compare June 18, 2026 13:37

@hussein-awala hussein-awala 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 this, I like the idea, being able to slice metrics by dag tags (team/env/criticality) is genuinely useful.

I left a few inline comments

Comment thread airflow-core/src/airflow/models/taskinstance.py Outdated
Comment thread task-sdk/src/airflow/sdk/execution_time/task_runner.py
Comment thread airflow-core/src/airflow/models/taskinstance.py Outdated
Comment thread airflow-core/src/airflow/models/dagrun.py Outdated
@sortega sortega force-pushed the sortega/airflow-tagged-metrics branch from 82d305f to 2e27387 Compare June 24, 2026 14:46
@sortega sortega requested a review from hussein-awala June 24, 2026 14:49
@potiuk

potiuk commented Jun 25, 2026

Copy link
Copy Markdown
Member

@sortega — this PR has 6 unresolved review thread(s) that still look like they need your attention. Once you've addressed them (push changes and/or reply in-thread), please resolve the threads or reply to confirm, and give the reviewer a nudge for another look. Thanks!

See the PR quality criteria.

Automated first-pass triage note drafted by an AI-assisted tool — may get things wrong; once addressed, a real Apache Airflow maintainer takes the next look. (why automated)


Drafted-by: Claude Code (Opus 4.8); reviewed by @potiuk before posting

@sortega sortega requested a review from ashb June 25, 2026 12:13
@sortega

sortega commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

I've addressed the feedback. @ashb, @hussein-awala could you take another look, please?

@sortega sortega force-pushed the sortega/airflow-tagged-metrics branch 5 times, most recently from dbaff3b to be52464 Compare July 2, 2026 10:07
@sortega sortega force-pushed the sortega/airflow-tagged-metrics branch from be52464 to 666fcb5 Compare July 2, 2026 13:01
@potiuk potiuk added the ready for maintainer review Set after triaging when all criteria pass. label Jul 2, 2026

@ashb ashb 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.

A few nits and small things, but close.

Please ping me again on Slack when you've had another pass.

Comment thread airflow-core/src/airflow/jobs/scheduler_job_runner.py Outdated
Comment thread airflow-core/src/airflow/jobs/scheduler_job_runner.py Outdated
Comment thread airflow-core/src/airflow/models/dagrun.py Outdated
Comment thread shared/observability/src/airflow_shared/observability/metrics/datadog_logger.py Outdated
Comment thread shared/observability/tests/observability/metrics/test_stats.py Outdated
Comment thread shared/observability/tests/observability/metrics/test_stats.py Outdated
Comment thread task-sdk/src/airflow/sdk/execution_time/task_runner.py Outdated
Comment thread task-sdk/tests/task_sdk/execution_time/test_task_runner.py Outdated
Comment thread task-sdk/tests/task_sdk/execution_time/test_task_runner.py Outdated
@sortega sortega force-pushed the sortega/airflow-tagged-metrics branch 3 times, most recently from 4256096 to 58b977b Compare July 13, 2026 13:06
@sortega sortega force-pushed the sortega/airflow-tagged-metrics branch 3 times, most recently from efd4519 to 6831198 Compare July 14, 2026 13:31
@ashb ashb changed the title Emit Dag tags as metric tags Optionally emit Dag tags as metric tags Jul 14, 2026
Comment thread airflow-core/src/airflow/models/dagrun.py Outdated
Comment thread airflow-core/src/airflow/models/taskinstance.py Outdated
@sortega sortega force-pushed the sortega/airflow-tagged-metrics branch from 4aa2660 to 0abe042 Compare July 15, 2026 08:17
sortega added 3 commits July 15, 2026 11:20
Add a [metrics] dag_tags_in_metrics option (default False). When enabled, each
Dag tag becomes a metric tag on Dag-run and task-instance metrics: tags with a
colon (e.g. env:prod) split into a key/value pair; plain tags (e.g. production)
become standalone DogStatsd tags, or tag=true in InfluxDB line protocol. Built-in
keys (dag_id, run_type, task_id, team_name) win on collision.

Tags come from the DagRun's dag_model.tags. The scheduler hot loop
(get_running_dag_runs_to_examine) and the executor-event failure path eager-load
them (gated on the flag) to avoid per-DagRun queries; other in-session emission
paths fall back to a lazy load, so the tags reach all Dag-run and task-instance
metrics. DagRun.dag_tags_for_stats swallows SQLAlchemyError so a detached/expired
instance degrades to no tags rather than breaking the caller.

TaskInstance.stats_tags reuses DagRun.dag_tags_for_stats and adds task_id and
run_type; the Task SDK worker reads the in-memory Dag and also adds run_type, so
ti.* metrics carry a consistent tag set across the worker and scheduler emitters.

The build_dag_metric_tags helper lives in the shared observability stats module.
Keep the scheduler hot path free of per-task conf reads. Warm the dag tags via the existing
TaskInstance -> DagModel join rather than an extra dag_run hop, since the
DagModel is shared in the identity map. Fold the repeated dogstatsd tag-list
guard into a single helper, and consolidate the tag-formatting tests.
Make session a keyword-only argument on get_running_dag_runs_to_examine, in
line with the convention for session parameters. Reduce TaskInstance.stats_tags
to the dag run's tags plus the task-level ones, keeping the TI's transiently
resolved team_name when present.
@sortega sortega force-pushed the sortega/airflow-tagged-metrics branch from 0abe042 to b312cbb Compare July 15, 2026 09:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants