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: add integration tag to spans_started/finished #3023

Open
wants to merge 51 commits into
base: main
Choose a base branch
from

Conversation

hannahkm
Copy link
Contributor

@hannahkm hannahkm commented Dec 10, 2024

What does this PR do?

Add a integration tag to the existing datadog.tracer.spans_started and datadog.tracer.spans_finished metrics. The value of the tag will be the name of the component from which the span was started. For example, for a contrib, it will be the name of the contrib package (chi, net/http, etc). For spans that were created manually, the tag will say manual.

For the purpose of adding tags, we move the logic of counting finished spans to span.finish() rather than trace.finishChunk(). Since we must read the integration data from each individual span, we would rather increment our counter each time one span finishes. Counting in finishChunk() as we did previously would require a for loop, which might impact efficiency.

Motivation

We want to know, in addition to when a span is started, where the span originated from. This could be a contrib or a manual implementation.

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.
  • For internal contributors, a matching PR should be created to the v2-dev branch and reviewed by @DataDog/apm-go.

Unsure? Have a question? Request a review!

@datadog-datadog-prod-us1
Copy link

datadog-datadog-prod-us1 bot commented Dec 10, 2024

Datadog Report

Branch report: apm-rd/span-source-health-metric
Commit report: 2756596
Test service: dd-trace-go

✅ 0 Failed, 5211 Passed, 72 Skipped, 2m 46.57s Total Time

@pr-commenter
Copy link

pr-commenter bot commented Dec 10, 2024

Benchmarks

Benchmark execution time: 2025-01-21 22:23:34

Comparing candidate commit 3e8b1df in PR branch apm-rd/span-source-health-metric with baseline commit e394045 in branch main.

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

scenario:BenchmarkInjectW3C-24

  • 🟥 execution_time [+171.144ns; +209.856ns] or [+4.241%; +5.200%]

scenario:BenchmarkSingleSpanRetention/no-rules-24

  • 🟥 execution_time [+9.279µs; +10.036µs] or [+3.961%; +4.284%]

scenario:BenchmarkSingleSpanRetention/with-rules/match-all-24

  • 🟥 execution_time [+9.726µs; +10.871µs] or [+4.126%; +4.611%]

scenario:BenchmarkSingleSpanRetention/with-rules/match-half-24

  • 🟥 execution_time [+9.122µs; +10.036µs] or [+3.857%; +4.244%]

scenario:BenchmarkTracerAddSpans-24

  • 🟥 execution_time [+162.275ns; +249.325ns] or [+4.212%; +6.472%]

@hannahkm hannahkm changed the title ddtrace/tracer: add source tag to spans_started health metric ddtrace/tracer: add integration tag to spans_started/finished Dec 12, 2024
@github-actions github-actions bot added the apm:ecosystem contrib/* related feature requests or bugs label Dec 12, 2024
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.

Ok, so you're reporting spansStarted/spansFinished on span.Start/span.Finished if the integration is not empty, and leaving the chunk reporting to any spans that are manual... I understand why you did this but not totally sure about the approach.

span.Start and span.Stop are typically called quite frequently, so if a majority of the spans are from automatic integrations, this will be very noisy (and defeats the purpose of reporting the metrics at a specified interval, to reduce noise)

One alternative idea:
Change the way we track spansStarted and spansFinished to be some kind of counter map that includes the integration name, e.g. map[string]uint32 where the key is the integration name and the value is the count of spans started/finished that integration name. Then, in this goroutine, we'll have to iterate over the map and report the spans started/finished per integration
(or some other idea I haven't thought of?)

ddtrace/mocktracer/mockspan.go Outdated Show resolved Hide resolved
ddtrace/mocktracer/mockspan.go Outdated Show resolved Hide resolved
ddtrace/mocktracer/mockspan_test.go Outdated Show resolved Hide resolved
@hannahkm
Copy link
Contributor Author

@mtoffl01 Good points! A map would probably work better; I was hesitant at first since I didn't want to change too much of what already exists, but knowing that these metrics are pretty old... I'm more down to change it up now.

@hannahkm hannahkm marked this pull request as ready for review December 19, 2024 21:27
@hannahkm hannahkm requested review from a team as code owners December 19, 2024 21:27
@darccio
Copy link
Member

darccio commented Dec 20, 2024

@hannahkm I'm approving this but we should investigate why the benchmarks report the increased allocations.

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.

Overall, I definitely have some concerns 🤔 Maybe you can write some additional tests to provide peace of mind....

  1. Tests designed to try and make the system fail -- what happens when you have multiple goroutines access a start span / finish span method, can we prove that we've protected against a race condition?
  2. Maybe you want to write dedicated benchmarks to show how much performance is impacted

ddtrace/tracer/tracer.go Outdated Show resolved Hide resolved
ddtrace/tracer/tracer.go Outdated Show resolved Hide resolved
ddtrace/tracer/metrics.go Outdated Show resolved Hide resolved
ddtrace/tracer/tracer.go Outdated Show resolved Hide resolved
ddtrace/tracer/tracer.go Show resolved Hide resolved
ddtrace/tracer/spancontext.go Outdated Show resolved Hide resolved
ddtrace/tracer/metrics.go Outdated Show resolved Hide resolved
ddtrace/tracer/metrics.go Outdated Show resolved Hide resolved
ddtrace/tracer/metrics.go Outdated Show resolved Hide resolved
ddtrace/tracer/metrics.go Outdated Show resolved Hide resolved
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.

It lgtm, BUT I would recommend we have more complex tests for SpansStarted and SpansFinished; like, generating multiple spans of different integrations and checking the metrics reported (rather than just 1 span).

ddtrace/mocktracer/mockspan.go Outdated Show resolved Hide resolved
ddtrace/tracer/metrics.go Outdated Show resolved Hide resolved
Copy link
Member

@felixge felixge left a comment

Choose a reason for hiding this comment

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

Overall LGTM, but I think there is one small bug, see below. I might have some more feedback on the tests and benchmarks, but not enough time for it right now. But I think these comments should already be useful 🙇 .

@@ -523,7 +520,6 @@ func (t *trace) finishedOne(s *span) {
}

func (t *trace) finishChunk(tr *tracer, ch *chunk) {
atomic.AddUint32(&tr.spansFinished, uint32(len(ch.spans)))
Copy link
Member

Choose a reason for hiding this comment

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

NIT: Why did we relocate the trigger point for the span start/finish tracking from this location to the new locations? Would be great to capture the answer to this question in the PR description.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point! I'll add that. tl;dr is finishChunk reports a slice of spans at a time, which would require looping over this slice to get each span's integration value and report that as a tag. Rather than introduce this for loop, we move the logic somewhere else.

spansStarted, spansFinished, tracesDropped uint32
// These maps count the spans started and finished from
// each component, including contribs and "manual" spans.
spansStarted, spansFinished *xsync.MapOf[string, *atomic.Int64]
Copy link
Member

Choose a reason for hiding this comment

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

NIT: It would be nice to abstract the concept of a counting map into a dedicated type that lives in an internal package. We have a use for this in profiling as well.

However, I don't think this needs to be done in this PR. I can do it as a follow-up change for profiling.

ddtrace/tracer/metrics.go Outdated Show resolved Hide resolved
value.Swap(0)
if err != nil {
log.Debug("Error while reporting spans started from integration %s: %s", key, err.Error())
}
Copy link
Member

Choose a reason for hiding this comment

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

Why are we logging the error here? I mean, in general it's a good idea, but it wasn't being done in the old code.

What kind of errors could happen here? Could we end up flooding the debug log? Are we concerned about locking the xmaps for too long while doing the logging?

Copy link
Contributor Author

@hannahkm hannahkm Jan 21, 2025

Choose a reason for hiding this comment

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

Generally, the idea was so that we can track down a reason for potentially missing span metrics. Though looking at the code again, it doesn't seem that t.statsd.Count() actually ever returns an error (see the function it calls, addMetric() here). So the easy answer is that perhaps we are not worried at all about flooding the log or locking for too long.

It seems that previously existing calls to this function did not check for err. For the time being, I will also remove it to match these instances.

@@ -48,7 +52,7 @@ func TestReportHealthMetricsAtInterval(t *testing.T) {
var tg statsdtest.TestStatsdClient

defer func(old time.Duration) { statsInterval = old }(statsInterval)
statsInterval = time.Nanosecond
statsInterval = time.Millisecond
Copy link
Member

Choose a reason for hiding this comment

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

753232f mentions this was done to fix flakiness. Was this flakiness pre-existing, or introduced by this PR? How did you verify that this fixes the problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

[New commit is at bb7be55. Rebase accidentally moved some things around].

The flake was introduced with this PR. It was caused by issues in tg.Wait(), which waits for n (in this case 4) reported metrics. But since we are newly reporting the number of spans started/finished even if this value is 0, we are reporting at least two metrics for every statsInterval, which is why I decided to increase it here.

I verified it by simply running it 1000+ times locally. It doesn't fail at all, which made me believe that it was no longer flaking.

@hannahkm hannahkm requested review from a team as code owners January 21, 2025 19:21
@hannahkm hannahkm requested a review from liashenko January 21, 2025 19:21
@hannahkm hannahkm force-pushed the apm-rd/span-source-health-metric branch from bc2c799 to 9425178 Compare January 21, 2025 19:41
@hannahkm hannahkm force-pushed the apm-rd/span-source-health-metric branch from 9425178 to 4cc70c1 Compare January 21, 2025 19:58
@hannahkm hannahkm removed request for a team and liashenko January 21, 2025 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
apm:ecosystem contrib/* related feature requests or bugs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants