Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddtrace/tracer: fix concurrent map writes when applying trace sampling rules and setting tags concurrently #2727

Merged
merged 3 commits into from
Jun 4, 2024

Conversation

darccio
Copy link
Member

@darccio darccio commented Jun 4, 2024

What does this PR do?

Fixes a race condition bug reported by TEE, where a span was being modified concurrently to inject context and to set tags and trace sampling rules are in place.

Motivation

Avoid race conditions in our code and improve reliability of our tracer.

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.
  • 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!

@darccio darccio requested a review from a team as a code owner June 4, 2024 10:57
@pr-commenter
Copy link

pr-commenter bot commented Jun 4, 2024

Benchmarks

Benchmark execution time: 2024-06-04 11:18:03

Comparing candidate commit 9b132d7 in PR branch dario.castane/fix-concurrent-write-maps-inject with baseline commit 6126fb6 in branch main.

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

scenario:BenchmarkStartSpanConcurrent-24

  • 🟩 execution_time [-630.832ns; -374.968ns] or [-9.072%; -5.392%]

// not (through tracer.Inject when trace sampling rules are in place) does not cause
// concurrent map writes. It seems to only be consistently reproduced with the -count=100
// flag when running go test, but it's a good test to have.
func TestConcurrentSpanSetTag(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, if I revert the fix and run this with -count=1 under the race detector, it passes. But if I do -count=2, it fails... It fails consistently if I swap the go statements (so the Inject one is first, SetTag is second. Strange! It also fails consistently if I do this:

func TestConcurrentSpanSetTag(t *testing.T) {
    testConcurrentSpanSetTag(t)
    testConcurrentSpanSetTag(t)
}

func testConcurrentSpanSetTag(t *testing.T) {
    // the actual implementation
}

I don't have any good ideas for why it doesn't trigger the failure the way you wrote it. But one of the things I did should hopefully catch the bug consistently without requiring -count > 1

Copy link
Member Author

Choose a reason for hiding this comment

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

That's really curious, good catch! Although it doesn't fail consistently in my machine, it fails more often. I updated the code to reflect your suggestions. It won't be a flaky test because running it with my fix under the race detecter and -count=100 doesn't trigger any error.

Copy link
Contributor

@nsrip-dd nsrip-dd left a comment

Choose a reason for hiding this comment

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

LGTM!

@darccio darccio enabled auto-merge (squash) June 4, 2024 14:02
auto-merge was automatically disabled June 4, 2024 14:05

Pull Request is not mergeable

@darccio darccio merged commit eeaff7c into main Jun 4, 2024
157 of 158 checks passed
@darccio darccio deleted the dario.castane/fix-concurrent-write-maps-inject branch June 4, 2024 14:15
nsrip-dd pushed a commit that referenced this pull request Jun 7, 2024
nsrip-dd added a commit that referenced this pull request Jun 7, 2024
…g rules and setting tags concurrently (#2727) (#2734)

This backports #2727 to the v1.64 release branch.

Co-authored-by: Dario Castañé <[email protected]>
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