From c8aede8b77142b451f092bad3e25ec351c63a183 Mon Sep 17 00:00:00 2001 From: Rey Abolofia Date: Thu, 28 Sep 2023 13:29:36 -0700 Subject: [PATCH] Export SamplingPriority method on spanContext. --- ddtrace/tracer/span.go | 2 +- ddtrace/tracer/spancontext.go | 2 +- ddtrace/tracer/sqlcomment.go | 2 +- ddtrace/tracer/sqlcomment_test.go | 6 +++--- ddtrace/tracer/textmap.go | 10 +++++----- ddtrace/tracer/textmap_test.go | 10 +++++----- ddtrace/tracer/tracer.go | 8 ++++---- ddtrace/tracer/tracer_test.go | 6 +++--- 8 files changed, 23 insertions(+), 23 deletions(-) diff --git a/ddtrace/tracer/span.go b/ddtrace/tracer/span.go index c4a0530409..18876e7576 100644 --- a/ddtrace/tracer/span.go +++ b/ddtrace/tracer/span.go @@ -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 } diff --git a/ddtrace/tracer/spancontext.go b/ddtrace/tracer/spancontext.go index 0f71e3f225..934fe62f88 100644 --- a/ddtrace/tracer/spancontext.go +++ b/ddtrace/tracer/spancontext.go @@ -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 } diff --git a/ddtrace/tracer/sqlcomment.go b/ddtrace/tracer/sqlcomment.go index b220d5c6fa..ce275a5ec0 100644 --- a/ddtrace/tracer/sqlcomment.go +++ b/ddtrace/tracer/sqlcomment.go @@ -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() diff --git a/ddtrace/tracer/sqlcomment_test.go b/ddtrace/tracer/sqlcomment_test.go index 024081b609..faf37c0b5a 100644 --- a/ddtrace/tracer/sqlcomment_test.go +++ b/ddtrace/tracer/sqlcomment_test.go @@ -133,7 +133,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) } @@ -166,7 +166,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) } @@ -244,7 +244,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") } diff --git a/ddtrace/tracer/textmap.go b/ddtrace/tracer/textmap.go index bff595e154..1e1e103741 100644 --- a/ddtrace/tracer/textmap.go +++ b/ddtrace/tracer/textmap.go @@ -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 != "" { @@ -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 { @@ -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 { @@ -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 { @@ -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) diff --git a/ddtrace/tracer/textmap_test.go b/ddtrace/tracer/textmap_test.go index 9b67ef1ac3..9969c0b33b 100644 --- a/ddtrace/tracer/textmap_test.go +++ b/ddtrace/tracer/textmap_test.go @@ -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) }) @@ -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) @@ -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) @@ -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) }) @@ -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") } diff --git a/ddtrace/tracer/tracer.go b/ddtrace/tracer/tracer.go index 0c333d2d5c..bf3e3d0fa7 100644 --- a/ddtrace/tracer/tracer.go +++ b/ddtrace/tracer/tracer.go @@ -394,7 +394,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 } @@ -497,7 +497,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 { @@ -548,7 +548,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) } @@ -656,7 +656,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 } diff --git a/ddtrace/tracer/tracer_test.go b/ddtrace/tracer/tracer_test.go index 91829844d6..287e806d94 100644 --- a/ddtrace/tracer/tracer_test.go +++ b/ddtrace/tracer/tracer_test.go @@ -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() @@ -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() @@ -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)