Skip to content

[Observability] Include DAG tags as attributes on spans - #72458

Open
realtimetodie wants to merge 9 commits into
apache:mainfrom
realtimetodie:dag-tags-spans
Open

realtimetodie wants to merge 9 commits into
apache:mainfrom
realtimetodie:dag-tags-spans

Conversation

@realtimetodie

Copy link
Copy Markdown

This change adds a new option to include DAG tags as attributes on spans. DAG tags as attributes on metrics already exist and were originally introduced in #68568.

New configuration option:

[traces]
dag_tags_in_spans = True

Summary

  • Added new configuration option dag_tags_in_spans to add DAG tags to task spans (default False)
  • DAG tags on spans use the same expanded key-value format already used for metrics
  • The DAG tag conversion logic is now shared between metrics and traces
  • Cleaned up and reorganized DAG tag tests
  • Added tests for DAG tags on task spans and tag collision handling
  • No breaking changes

This change adds a new option to include DAG tags as attributes on spans. DAG tags as attributes on metrics already exist and were originally introduced in apache#68568.
@realtimetodie realtimetodie changed the title Include DAG tags as attributes on spans [Observability] Include DAG tags as attributes on spans Sep 3, 2026

@xBis7 xBis7 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@realtimetodie It mostly looks good but I think we don't need to use a config flag unless there is a reason to.

@realtimetodie

Copy link
Copy Markdown
Author

@xBis7 Ah sorry, I think this is a misunderstanding. There is already a dag_tags_in_metrics configuration option, which is disabled by default. See my comments above.

@realtimetodie
realtimetodie requested a review from xBis7 September 4, 2026 05:27

@xBis7 xBis7 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I tested it manually. With these tags

with DAG(
    "otel_test_dag",
    default_args=args,
    schedule=None,
    tags=["etl", "team:observability", "env:production"],

I see

Image

out of this tree

Image

The tags are only added on the worker span. Is this the intended behavior?

BTW, this is the dag https://github.com/realtimetodie/apache-airflow/blob/dag-tags-spans/airflow-core/tests/integration/otel/dags/otel_test_dag.py

@realtimetodie

realtimetodie commented Sep 4, 2026 •

Copy link
Copy Markdown
Author

Yes, you are right. My initial description was ambiguous. I was too focused on my telemetry backend, which can find the root span based on attributes from child spans.

The cleaner OpenTelemetry design is to attach the DAG tags only to the root dag_run span.

The following are the only two attributes present everywhere:

  • airflow.dag_id
  • airflow.dag_run.run_id

In my opinion, duplicating the DAG tags as attributes onto every task_run, worker and trigger span is unnecessary. However, it is technically possible.

Span DAG tags available?
dag_run (root) Yes
task_run Yes, with extra lookup
worker Yes
trigger No, not without changing the internal API

@realtimetodie
realtimetodie requested a review from xBis7 September 4, 2026 20:34
@realtimetodie

realtimetodie commented Sep 4, 2026 •

Copy link
Copy Markdown
Author

@xBis7 I updated the code to include the DAG tags only to the DAG run span (the root span).

@xBis7 xBis7 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for updating the PR. The current approach for adding the tags just on the root span looks better.

@realtimetodie

realtimetodie commented Sep 7, 2026 •

Copy link
Copy Markdown
Author

@xBis7 I rebased with the main branch and had to revert the shared expand_dag_tags usage from stats.py again because, as of #72364, build_dag_metric_tags contains StatsD-specific normalization logic. I do not like how StatsD is polluting the observability layer.

At this point the metrics behavior is no longer equivalent to the generic DAG tag expansion used for spans, so keeping them shared would change the current metrics behavior. I am keeping expand_dag_tags only for spans and leaving build_dag_metric_tags independent.

see #72458 (comment)

As I mentinoned before, in an upcoming pull request, I would like to change how DAG tags are represented in OpenTelemetry.

Expanding every single tag into attributes is needed for StatsD-style metrics, but OpenTelemetry supports string-array attributes. This means all DAG tags in spans and metrics could be exported under a single attribute instead for OpenTelemetry, for example:

airflow.dag.tags = ["finance", "production", "team:data"]

@realtimetodie
realtimetodie requested a review from xBis7 September 7, 2026 21:54
@realtimetodie

Copy link
Copy Markdown
Author

I left a comment in #72364 (comment)

@realtimetodie

Copy link
Copy Markdown
Author

Following #72364, I applied a fix that keeps the StatsD-specific normalization only for StatsD/Datadog metrics. For OpenTelemetry metrics, DAG tags now use expand_dag_tags() so the original attribute values are preserved.

This avoids applying StatsD character restrictions to OpenTelemetry metric attributes.

@xBis7 xBis7 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I went over the code again and I've left a small cosmetic comment. Apart from that it looks good to me.

@realtimetodie
realtimetodie requested a review from xBis7 September 13, 2026 06:59

@xBis7 xBis7 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@realtimetodie

Copy link
Copy Markdown
Author

Thank you. As soon as this is merged, I will submit the announced follow-up PR.

@eladkal eladkal added this to the Airflow 3.4.0 milestone Sep 16, 2026
@eladkal eladkal added the type:improvement Changelog: Improvements label Sep 16, 2026
@vatsrahul1001 vatsrahul1001 added the backport-to-v3-4-test Backport to v3-4-test label Oct 18, 2026

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants