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

Export SamplingPriority method on spanContext [SVLS-3934] #2291

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ddtrace/tracer/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ func obfuscatedResource(o *obfuscate.Obfuscator, typ, resource string) string {
// shouldKeep reports whether the trace should be kept.
// a single span being kept implies the whole trace being kept.
func shouldKeep(s *span) bool {
if p, ok := s.context.samplingPriority(); ok && p > 0 {
if p, ok := s.context.SamplingPriority(); ok && p > 0 {
// positive sampling priorities stay
return true
}
Expand Down
2 changes: 1 addition & 1 deletion ddtrace/tracer/spancontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (c *spanContext) setSamplingPriority(p int, sampler samplernames.SamplerNam
c.trace.setSamplingPriority(p, sampler)
}

func (c *spanContext) samplingPriority() (p int, ok bool) {
func (c *spanContext) SamplingPriority() (p int, ok bool) {
if c.trace == nil {
return 0, false
}
Expand Down
2 changes: 1 addition & 1 deletion ddtrace/tracer/sqlcomment.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (c *SQLCommentCarrier) Inject(spanCtx ddtrace.SpanContext) error {
traceID uint64
)
if ctx, ok := spanCtx.(*spanContext); ok {
if sp, ok := ctx.samplingPriority(); ok && sp > 0 {
if sp, ok := ctx.SamplingPriority(); ok && sp > 0 {
sampled = 1
}
traceID = ctx.TraceID()
Expand Down
6 changes: 3 additions & 3 deletions ddtrace/tracer/sqlcomment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func TestSQLCommentCarrier(t *testing.T) {
assert.Equal(t, carrier.SpanID, xctx.spanID)
assert.Equal(t, traceID, xctx.traceID.Lower())

p, ok := xctx.samplingPriority()
p, ok := xctx.SamplingPriority()
assert.True(t, ok)
assert.Equal(t, tc.samplingPriority, p)
}
Expand Down Expand Up @@ -168,7 +168,7 @@ func TestExtractOpenTelemetryTraceInformation(t *testing.T) {
assert.Equal(t, lower, xctx.traceID.Lower())
assert.Equal(t, upper, xctx.traceID.Upper())

p, ok := xctx.samplingPriority()
p, ok := xctx.SamplingPriority()
assert.True(t, ok)
assert.Equal(t, priority, p)
}
Expand Down Expand Up @@ -246,7 +246,7 @@ func FuzzSpanContextFromTraceComment(f *testing.F) {
wanted: %d`, xctx.traceID.Upper(), traceIDUpper)
}

p, ok := xctx.samplingPriority()
p, ok := xctx.SamplingPriority()
if !ok {
t.Fatalf("Error retrieving sampling priority")
}
Expand Down
10 changes: 5 additions & 5 deletions ddtrace/tracer/textmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func (p *propagator) injectTextMap(spanCtx ddtrace.SpanContext, writer TextMapWr
}
writer.Set(p.cfg.TraceHeader, strconv.FormatUint(ctx.traceID.Lower(), 10))
writer.Set(p.cfg.ParentHeader, strconv.FormatUint(ctx.spanID, 10))
if sp, ok := ctx.samplingPriority(); ok {
if sp, ok := ctx.SamplingPriority(); ok {
writer.Set(p.cfg.PriorityHeader, strconv.Itoa(sp))
}
if ctx.origin != "" {
Expand Down Expand Up @@ -502,7 +502,7 @@ func (*propagatorB3) injectTextMap(spanCtx ddtrace.SpanContext, writer TextMapWr
writer.Set(b3TraceIDHeader, w3Cctx.TraceID128())
}
writer.Set(b3SpanIDHeader, fmt.Sprintf("%016x", ctx.spanID))
if p, ok := ctx.samplingPriority(); ok {
if p, ok := ctx.SamplingPriority(); ok {
if p >= ext.PriorityAutoKeep {
writer.Set(b3SampledHeader, "1")
} else {
Expand Down Expand Up @@ -585,7 +585,7 @@ func (*propagatorB3SingleHeader) injectTextMap(spanCtx ddtrace.SpanContext, writ
traceID = w3Cctx.TraceID128()
}
sb.WriteString(fmt.Sprintf("%s-%016x", traceID, ctx.spanID))
if p, ok := ctx.samplingPriority(); ok {
if p, ok := ctx.SamplingPriority(); ok {
if p >= ext.PriorityAutoKeep {
sb.WriteString("-1")
} else {
Expand Down Expand Up @@ -681,7 +681,7 @@ func (*propagatorW3c) injectTextMap(spanCtx ddtrace.SpanContext, writer TextMapW
return ErrInvalidSpanContext
}
flags := ""
p, ok := ctx.samplingPriority()
p, ok := ctx.SamplingPriority()
if ok && p >= ext.PriorityAutoKeep {
flags = "01"
} else {
Expand Down Expand Up @@ -982,7 +982,7 @@ func parseTracestate(ctx *spanContext, header string) {
// The sampling priority and decision maker values are set based on
// the specification in the internal W3C context propagation RFC.
// See the document for more details.
parentP, _ := ctx.samplingPriority()
parentP, _ := ctx.SamplingPriority()
if (parentP == 1 && stateP > 0) || (parentP == 0 && stateP <= 0) {
// As extracted from tracestate
ctx.setSamplingPriority(stateP, samplernames.Unknown)
Expand Down
10 changes: 5 additions & 5 deletions ddtrace/tracer/textmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ func TestEnvVars(t *testing.T) {
// assert.Equal(test.traceID128Full, id128FromSpan(assert, ctx)) // add when 128-bit trace id support is enabled
assert.Equal(tc.out[0], sctx.traceID.Lower())
assert.Equal(tc.out[1], sctx.spanID)
p, ok := sctx.samplingPriority()
p, ok := sctx.SamplingPriority()
assert.True(ok)
assert.Equal(int(tc.out[2]), p)
})
Expand Down Expand Up @@ -1108,7 +1108,7 @@ func TestEnvVars(t *testing.T) {
assert.Equal(tc.tid, sctx.traceID)
assert.Equal(tc.out[0], sctx.spanID)
assert.Equal(tc.origin, sctx.origin)
p, ok := sctx.samplingPriority()
p, ok := sctx.SamplingPriority()
assert.True(ok)
assert.Equal(int(tc.out[1]), p)

Expand Down Expand Up @@ -1217,7 +1217,7 @@ func TestEnvVars(t *testing.T) {

assert.Equal(tc.tid, sctx.traceID)
assert.Equal(tc.sid, sctx.spanID)
p, ok := sctx.samplingPriority()
p, ok := sctx.SamplingPriority()
assert.True(ok)
assert.Equal(tc.priority, p)

Expand Down Expand Up @@ -1703,7 +1703,7 @@ func TestEnvVars(t *testing.T) {

assert.Equal(tc.tid, sctx.traceID)
assert.Equal(tc.out[0], sctx.spanID)
p, ok := sctx.samplingPriority()
p, ok := sctx.SamplingPriority()
assert.True(ok)
assert.Equal(int(tc.out[1]), p)
})
Expand Down Expand Up @@ -2032,7 +2032,7 @@ func FuzzParseTraceparent(f *testing.F) {
if parseTraceparent(ctx, header) != nil {
t.Skipf("Error parsing parent")
}
parsedSamplingPriority, ok := ctx.samplingPriority()
parsedSamplingPriority, ok := ctx.SamplingPriority()
if !ok {
t.Skipf("Error retrieving sampling priority")
}
Expand Down
8 changes: 4 additions & 4 deletions ddtrace/tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ type chunk struct {
// sampleChunk applies single-span sampling to the provided trace.
func (t *tracer) sampleChunk(c *chunk) {
if len(c.spans) > 0 {
if p, ok := c.spans[0].context.samplingPriority(); ok && p > 0 {
if p, ok := c.spans[0].context.SamplingPriority(); ok && p > 0 {
// The trace is kept, no need to run single span sampling rules.
return
}
Expand Down Expand Up @@ -501,7 +501,7 @@ func (t *tracer) StartSpan(operationName string, options ...ddtrace.StartSpanOpt
// this is a child span
span.TraceID = context.traceID.Lower()
span.ParentID = context.spanID
if p, ok := context.samplingPriority(); ok {
if p, ok := context.SamplingPriority(); ok {
span.setMetric(keySamplingPriority, float64(p))
}
if context.span != nil {
Expand Down Expand Up @@ -552,7 +552,7 @@ func (t *tracer) StartSpan(operationName string, options ...ddtrace.StartSpanOpt
if t.config.env != "" {
span.setMeta(ext.Environment, t.config.env)
}
if _, ok := span.context.samplingPriority(); !ok {
if _, ok := span.context.SamplingPriority(); !ok {
// if not already sampled or a brand new trace, sample it
t.sample(span)
}
Expand Down Expand Up @@ -660,7 +660,7 @@ const sampleRateMetricKey = "_sample_rate"

// Sample samples a span with the internal sampler.
func (t *tracer) sample(span *span) {
if _, ok := span.context.samplingPriority(); ok {
if _, ok := span.context.SamplingPriority(); ok {
// sampling decision was already made
return
}
Expand Down
6 changes: 3 additions & 3 deletions ddtrace/tracer/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ func TestSamplingDecision(t *testing.T) {
span := tracer.StartSpan("name_1").(*span)
child := tracer.StartSpan("name_2", ChildOf(span.context))
child.SetTag(ext.EventSampleRate, 1)
p, ok := span.context.samplingPriority()
p, ok := span.context.SamplingPriority()
require.True(t, ok)
assert.Equal(t, ext.PriorityAutoReject, p)
child.Finish()
Expand Down Expand Up @@ -1143,7 +1143,7 @@ func TestTracerPrioritySampler(t *testing.T) {
assert.Equal(1., s.Metrics[keySamplingPriorityRate])
assert.Equal(1., s.Metrics[keySamplingPriority])
assert.Equal("-1", s.context.trace.propagatingTags[keyDecisionMaker])
p, ok := s.context.samplingPriority()
p, ok := s.context.SamplingPriority()
assert.True(ok)
assert.EqualValues(p, s.Metrics[keySamplingPriority])
s.Finish()
Expand Down Expand Up @@ -1185,7 +1185,7 @@ func TestTracerPrioritySampler(t *testing.T) {
}
assert.True(ok)
assert.Contains([]float64{0, 1}, prio)
p, ok := s.context.samplingPriority()
p, ok := s.context.SamplingPriority()
assert.True(ok)
assert.EqualValues(p, prio)

Expand Down
Loading