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: propagate _dd.p.upstream_services tags #1082

Merged
merged 5 commits into from
Jan 24, 2022

Conversation

Barbayar
Copy link
Contributor

@Barbayar Barbayar commented Dec 6, 2021

Changes

  • Introduced a notion of trace level tags.
  • Introduced a notion of propagated trace level tags.
  • Introduced a new trace level tag _dd.p.upstream_services that holds the services changed sampling decisions.

@Barbayar Barbayar changed the title [Draft] Propagates _dd.p.upstream_services [WIP] Propagates _dd.p.upstream_services Dec 6, 2021
ddtrace/tracer/span.go Outdated Show resolved Hide resolved
ddtrace/tracer/payload.go Outdated Show resolved Hide resolved
ddtrace/tracer/textmap.go Outdated Show resolved Hide resolved
ddtrace/tracer/payload.go Outdated Show resolved Hide resolved
@Barbayar Barbayar added this to the 1.35.0 milestone Dec 6, 2021
internal/appsec/waf.go Outdated Show resolved Hide resolved
ddtrace/tracer/sampler.go Outdated Show resolved Hide resolved
@@ -217,7 +235,13 @@ func (t *trace) setSamplingPriorityLocked(p float64) {
if t.priority == nil {
t.priority = new(float64)
}
*t.priority = p
*t.priority = float64(p)
encodedService := b64Encode(service)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should only keep of the current priority, rate and the sampler name, and avoid re-encoding this every time.

At the very end, we just append to this tag.

ddtrace/ext/tags.go Outdated Show resolved Hide resolved
ddtrace/tracer/payload.go Outdated Show resolved Hide resolved
ddtrace/tracer/spancontext.go Show resolved Hide resolved
ddtrace/tracer/spancontext.go Show resolved Hide resolved
ddtrace/tracer/textmap.go Outdated Show resolved Hide resolved
ddtrace/tracer/util.go Show resolved Hide resolved
@Barbayar Barbayar force-pushed the barbayar.dashzeveg/trace-tags-propagation branch from 8294fc2 to 1fb68a2 Compare December 8, 2021 13:49
ddtrace/tracer/util.go Outdated Show resolved Hide resolved
internal/appsec/waf.go Outdated Show resolved Hide resolved
@Barbayar Barbayar force-pushed the barbayar.dashzeveg/trace-tags-propagation branch from 035ca27 to 197acb8 Compare December 13, 2021 12:49
ddtrace/tracer/textmap.go Outdated Show resolved Hide resolved
@Barbayar Barbayar force-pushed the barbayar.dashzeveg/trace-tags-propagation branch from afa8ecc to 6dff09c Compare December 13, 2021 16:09
@Barbayar Barbayar changed the title [WIP] Propagates _dd.p.upstream_services Propagates _dd.p.upstream_services Dec 13, 2021
@Barbayar Barbayar marked this pull request as ready for review December 13, 2021 19:36
@Barbayar Barbayar requested a review from gbbr December 13, 2021 19:37
internal/appsec/waf.go Outdated Show resolved Hide resolved
@felixge felixge modified the milestones: 1.35.0, 1.36.0 Dec 14, 2021
@Julio-Guerra Julio-Guerra self-requested a review December 14, 2021 16:15
@Julio-Guerra Julio-Guerra dismissed their stale review December 14, 2021 16:16

won't break the current appsec spec

@Barbayar Barbayar force-pushed the barbayar.dashzeveg/trace-tags-propagation branch from bfc09a1 to 3573402 Compare December 15, 2021 23:14
@gbbr gbbr changed the title Propagates _dd.p.upstream_services ddtrace/tracer: propagate _dd.p.upstream_services tags Dec 16, 2021
@Barbayar Barbayar force-pushed the barbayar.dashzeveg/trace-tags-propagation branch from b333743 to 93efac7 Compare December 16, 2021 14:40
Copy link
Contributor

@gbbr gbbr left a comment

Choose a reason for hiding this comment

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

I think we're getting closer to a final implementation.

ddtrace/tracer/option.go Outdated Show resolved Hide resolved
ddtrace/tracer/option.go Outdated Show resolved Hide resolved
ddtrace/tracer/sampler.go Outdated Show resolved Hide resolved
ddtrace/tracer/sampler.go Outdated Show resolved Hide resolved
ddtrace/tracer/span.go Outdated Show resolved Hide resolved
@@ -283,10 +304,14 @@ func (s *span) setMetric(key string, v float64) {
}
delete(s.Meta, key)
switch key {
case ext.ManualKeep:
if v == float64(samplerAppSec) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is quite nasty. But I can't think of a better approach.

ddtrace/tracer/span.go Outdated Show resolved Hide resolved
ddtrace/tracer/span.go Show resolved Hide resolved
ddtrace/tracer/spancontext.go Show resolved Hide resolved
ddtrace/tracer/option.go Outdated Show resolved Hide resolved
Copy link
Contributor

@gbbr gbbr left a comment

Choose a reason for hiding this comment

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

Looks good to me, just a few nits. Would love it if @knusbaum could also take a look, just in case he notices something I didn't.

ddtrace/tracer/option.go Outdated Show resolved Hide resolved
ddtrace/tracer/option.go Outdated Show resolved Hide resolved
ddtrace/tracer/sampler.go Outdated Show resolved Hide resolved
ddtrace/tracer/sampler.go Outdated Show resolved Hide resolved
// and it's noop except propagating the decision.
samplerNone samplerName = math.MinInt8
// samplerUnknown specifies that the span was sampled
// but, the tracer was unable to identify the sampler.
Copy link
Contributor

Choose a reason for hiding this comment

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

How is this possible? In what scenarios?

Copy link
Contributor Author

@Barbayar Barbayar Jan 12, 2022

Choose a reason for hiding this comment

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

This is not used in Go tracer. This is reserved value for other tracers in case they are not able to find out who triggered the sampling decision. I can remove it to avoid confusions. Wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like it's never used in the code, but it is in the RFC. I'm OK keeping it here for completeness. The RFC document doesn't say anything about when it can happen...

// samplerUnknown specifies that the span was sampled
// but, the tracer was unable to identify the sampler.
samplerUnknown samplerName = -1
// samplerDefault specifies that the span was sampled without any sampler.
Copy link
Contributor

Choose a reason for hiding this comment

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

How is this different from samplerUnknown? These 3 are still confusing and the documentation doesn't clarify the confusion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was supposed to be used until the sampling rates come from the agent for the first time.
But, I didn't implement that logic in this PR. We can do it later.
I can remove samplerDefault from this PR now, then add back when we implement that logic.

ddtrace/tracer/sampler.go Outdated Show resolved Hide resolved
ddtrace/tracer/spancontext.go Outdated Show resolved Hide resolved
Copy link
Contributor

@knusbaum knusbaum left a comment

Choose a reason for hiding this comment

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

Looks mostly good. I have a few comments.

Comment on lines 314 to 318
case ext.ManualKeep:
if v == float64(samplerAppSec) {
s.setSamplingPriorityLocked(ext.PriorityUserKeep, samplerAppSec, math.NaN())
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like this is here so we can set ManualKeep with a particular sampler (span.SetTag(ext.ManualKeep, samplerAppSec))

Doesn't this case cause ext.ManualKeep to not be set unless by the samplerAppSec? Do we want ext.ManualKeep to work for other samplers? That would be a lot more consistent rather than having this special case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Doesn't this case cause ext.ManualKeep to not be set unless by the samplerAppSec?

No, span.SetTag(ext.ManualKeep, true) still works as before.

Do we want ext.ManualKeep to work for other samplers?

I think we shouldn't support/allow customer specifying the sampler freely. This was a small hack for AppSec, because AppSec library cannot access private properties/methods of the tracer.

Copy link
Contributor

Choose a reason for hiding this comment

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

No, span.SetTag(ext.ManualKeep, true) still works as before.

I know that still works. What I mean is that setting manual keep with any other sampler value will silently fail, and unless I read the setMetric code, I have no way to know that. This will be surprising behavior if I see appsec setting the sampler with this mechanism.

If we want to do this hack, we should expose samplerAppSec as some internal constant so that appsec can use it. I guess it would have to be in github.com/DataDog/dd-trace-go.v1/internal. We should also document the mechanism (not in the public docs) so we know what works (only samplerAppSec)

ddtrace/tracer/spancontext.go Outdated Show resolved Hide resolved
ddtrace/tracer/spancontext.go Outdated Show resolved Hide resolved
ddtrace/tracer/option.go Show resolved Hide resolved
ddtrace/tracer/span_test.go Show resolved Hide resolved
ddtrace/tracer/textmap.go Show resolved Hide resolved
internal/appsec/waf.go Outdated Show resolved Hide resolved
@Barbayar Barbayar requested a review from a team as a code owner January 12, 2022 12:09
@Barbayar Barbayar requested review from a team, knusbaum and gbbr January 12, 2022 12:09
ddtrace/tracer/sampler.go Outdated Show resolved Hide resolved
ddtrace/tracer/sampler.go Outdated Show resolved Hide resolved
// and it's noop except propagating the decision.
samplerNone samplerName = math.MinInt8
// samplerUnknown specifies that the span was sampled
// but, the tracer was unable to identify the sampler.
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like it's never used in the code, but it is in the RFC. I'm OK keeping it here for completeness. The RFC document doesn't say anything about when it can happen...

ddtrace/tracer/textmap.go Show resolved Hide resolved
Comment on lines 314 to 318
case ext.ManualKeep:
if v == float64(samplerAppSec) {
s.setSamplingPriorityLocked(ext.PriorityUserKeep, samplerAppSec, math.NaN())
}
Copy link
Contributor

Choose a reason for hiding this comment

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

No, span.SetTag(ext.ManualKeep, true) still works as before.

I know that still works. What I mean is that setting manual keep with any other sampler value will silently fail, and unless I read the setMetric code, I have no way to know that. This will be surprising behavior if I see appsec setting the sampler with this mechanism.

If we want to do this hack, we should expose samplerAppSec as some internal constant so that appsec can use it. I guess it would have to be in github.com/DataDog/dd-trace-go.v1/internal. We should also document the mechanism (not in the public docs) so we know what works (only samplerAppSec)

Copy link
Contributor

@knusbaum knusbaum left a comment

Choose a reason for hiding this comment

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

Just one more nitpick from me.

Comment on lines 25 to 27
// samplerAppSec specifies that the span was sampled by AppSec.
samplerAppSec int8 = 5

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's point at ddtrace/tracer/sampler.go here if we're not going to share the definitions. Otherwise I have no idea of the context of 5.

@Barbayar Barbayar force-pushed the barbayar.dashzeveg/trace-tags-propagation branch 2 times, most recently from 9293b0b to 44f2399 Compare January 14, 2022 20:08
knusbaum
knusbaum previously approved these changes Jan 14, 2022
internal/appsec/dyngo/instrumentation/grpcsec/tags.go Outdated Show resolved Hide resolved
// samplerManual specifies that the span was sampled manually by user.
samplerManual samplerName = 4
// samplerAppSec specifies that the span was sampled by AppSec.
samplerAppSec samplerName = 5
Copy link
Contributor

Choose a reason for hiding this comment

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

@knusbaum, @Barbayar: given this value is used by AppSec but is currently hard-coded there, WDYT of exporting it /internal/something so that /contrib/**/appsec.go can use the same const value?
I fear this value changes at some point without changing them in appsec, and conversely.

Copy link
Contributor

Choose a reason for hiding this comment

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

👍 I suggested the same thing. If others agree, let's go ahead and move all of these.

Copy link
Contributor

@Julio-Guerra Julio-Guerra left a comment

Choose a reason for hiding this comment

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

Changing the state of the PR to a non-approved state.

Copy link
Contributor

@Julio-Guerra Julio-Guerra left a comment

Choose a reason for hiding this comment

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

All good regarding internal/appsec/* 👍

Copy link
Contributor

@knusbaum knusbaum left a comment

Choose a reason for hiding this comment

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

Just one comment, but not a blocker (it's not something you changed).

Everything looks good. 👍

Comment on lines +33 to +34
case int8:
return float64(i), true
Copy link
Contributor

Choose a reason for hiding this comment

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

It's surprising that this was missing. We should probably go ahead and also add uint8 for completeness.

@knusbaum knusbaum merged commit e52ef96 into v1 Jan 24, 2022
@knusbaum knusbaum deleted the barbayar.dashzeveg/trace-tags-propagation branch January 24, 2022 22:38
knusbaum pushed a commit that referenced this pull request Feb 25, 2022
This commit reverts part of:
ddtrace/tracer: propagate _dd.p.upstream_services tags #1082
knusbaum pushed a commit that referenced this pull request Mar 1, 2022
This commit reverts part of:
ddtrace/tracer: propagate _dd.p.upstream_services tags #1082
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.

5 participants