Skip to content

celery: give sampler visibility on spans attributes - #5312

Open
xrmx wants to merge 3 commits into
open-telemetry:mainfrom
xrmx:celery-sampler-visibility
Open

xrmx wants to merge 3 commits into
open-telemetry:mainfrom
xrmx:celery-sampler-visibility

Conversation

@xrmx

@xrmx xrmx commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Description

Add known attributes at span creation time so that the sampler has visibility on them.

Refs #5253

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • I am a human and not a bot
  • I have run tox -e precommit
  • I have run tox -e typecheck
  • I have run the relevant tests and linting tox environments
  • This requires the following PR in core:

@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Waiting on the author · refreshed 2026-11-11 17:13 UTC

Two things need attention:

  • Required checks are failing — investigate the failures.
  • 1 review item — respond to each (e.g. link a commit, explain why not, ask a follow-up):
    • Inline threads: 1
Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Should this be with reviewers? Comment /dashboard route:reviewers to route it to them.
  • Anything wrong — including the routing? Report it with what you expected; it helps us improve the dashboard.

@xrmx
xrmx requested a review from a team as a code owner November 9, 2026 10:13
@herin049
herin049 requested a balanced review from Copilot November 11, 2026 16:02

Copilot AI 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.

🟢 Approval recommended

No active review issues are reported for the Celery sampler-attribute update.

No open issues 🎉

What changed in this PR

Updates Celery instrumentation to expose known span attributes to samplers at span creation, enabling attribute-based sampling as requested in #5253.

Changes:

  • Collects producer and consumer attributes before starting spans, while retaining post-execution state updates.
  • Refactors context extraction to return an attribute dictionary.
  • Adds tests for sampler visibility, attribute-based sampling, and context cleanup.
File Description
instrumentation/​opentelemetry-instrumentation-celery/​tests/​test_utils.py Updates tests for dictionary-based attribute extraction.
instrumentation/​opentelemetry-instrumentation-celery/​tests/​test_tasks.py Tests sampler attributes, sampling decisions, and cleanup.
instrumentation/​opentelemetry-instrumentation-celery/​src/​opentelemetry/​instrumentation/​celery/​utils.py Returns extracted attributes and uses destination-kind enums.
instrumentation/​opentelemetry-instrumentation-celery/​src/​opentelemetry/​instrumentation/​celery/​__init__.py Supplies known attributes when creating producer and consumer spans.
.changelog/​5312.changed Documents sampler visibility for Celery attributes.

Review effort: Balanced

utils.set_attributes_from_context(span, kwargs)
utils.set_attributes_from_context(span, task.request)
span.set_attribute(_TASK_NAME_KEY, task.name)
span.set_attributes(utils.get_attributes_from_context(kwargs))

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.

small nit: do we maybe want to split out get_attributes_from_context into prerun/postrun to avoid redundant computation here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Will take a look tomorrow

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

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants