Skip to content

Conversation

darccio
Copy link
Member

@darccio darccio commented Aug 12, 2025

What does this PR do?

Adds ExtractCtx to simplify the creation of child spans from propagated contexts.

Some notes:

  • All the functions are unexported except for ExtractCtx to reduce the increase on public API.
  • We are still relying in the deprecated ChildOf internally, which expects a SpanContext, so instead of reusing the existing ActiveSpanKey we create a new one for ActiveSpanContextKey (which it doesn't have precedence over ActiveSpanKey value).

Motivation

Fixes #3793

Reviewer's Checklist

  • Changed code has unit tests for its functionality at or near 100% coverage.
  • System-Tests covering this feature have been added and enabled with the va.b.c-dev version tag.
  • There is a benchmark for any new code, or changes to existing code.
  • If this interacts with the agent in a new way, a system test has been added.
  • New code is free of linting errors. You can check this by running ./scripts/lint.sh locally.
  • Add an appropriate team label so this PR gets put in the right place for the release notes.
  • Non-trivial go.mod changes, e.g. adding new modules, are reviewed by @DataDog/dd-trace-go-guild.

Unsure? Have a question? Request a review!

@pr-commenter
Copy link

pr-commenter bot commented Aug 12, 2025

Benchmarks

Benchmark execution time: 2025-08-13 09:29:15

Comparing candidate commit 831161b in PR branch dario.castane/langplat-727/extract-ctx with baseline commit 4d47dd6 in branch main.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 24 metrics, 0 unstable metrics.

if err != nil {
return nil, err
}
return contextWithSpanContext(ctx, sctx), nil
Copy link
Contributor

@mtoffl01 mtoffl01 Aug 12, 2025

Choose a reason for hiding this comment

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

If Extract returns a nil sctx, then we set nil on the context at the key. Is that ok?

Copy link
Member Author

@darccio darccio Aug 13, 2025

Choose a reason for hiding this comment

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

Uhm, you are totally right, and I think it's not ok. We should avoid setting any nil value.

Copy link
Member Author

Choose a reason for hiding this comment

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

Improved. Thanks!


// contextWithSpanContext returns a copy of the given context which includes the span context sctx.
func contextWithSpanContext(ctx context.Context, sctx *SpanContext) context.Context {
return orchestrion.CtxWithValue(ctx, internal.ActiveSpanKey, sctx)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be internal.ActiveSpanContextKey instead of internal.ActiveSpanKey

Copy link
Member Author

Choose a reason for hiding this comment

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

You are right!

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

Copy link
Contributor

@mtoffl01 mtoffl01 left a comment

Choose a reason for hiding this comment

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

I know this is just a draft, but looking forward to the tests to better understand how these functions will be used!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants