Skip to content

Commit

Permalink
Add Tracestate into the SamplingResult struct (#1432)
Browse files Browse the repository at this point in the history
* Add Tracestate into the SamplingResult struct

Add `trace.Tracestate` field into the SDK `trace.SamplingResult` struct.

Use ParentContext from SamplingParameters to return Tracestate in
`traceIDRatioSampler`, `alwaysOnSampler` and `alwaysOffSampler`.

Add a new test to check that Tracestate is passed.

* Updated CHANGELOG.md for #1432 PR

Added changes description for #1432.

* Update sdk/trace/sampling_test.go

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
  • Loading branch information
ozerovandrei and MrAlias authored Jan 6, 2021
1 parent db06c8d commit 40f1c00
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Many OTLP Exporter options became gRPC ProtocolDriver options. (#1369)
- Unify endpoint API that related to OTel exporter. (#1401)
- Metric aggregator Count() and histogram Bucket.Counts are consistently `uint64`. (1430)
- `SamplingResult` now passed a `Tracestate` from the parent `SpanContext` (#1432)

### Removed

Expand Down
23 changes: 18 additions & 5 deletions sdk/trace/sampling.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ const (
RecordAndSample
)

// SamplingResult conveys a SamplingDecision and a set of Attributes.
// SamplingResult conveys a SamplingDecision, set of Attributes and a Tracestate.
type SamplingResult struct {
Decision SamplingDecision
Attributes []label.KeyValue
Tracestate trace.TraceState
}

type traceIDRatioSampler struct {
Expand All @@ -70,9 +71,15 @@ type traceIDRatioSampler struct {
func (ts traceIDRatioSampler) ShouldSample(p SamplingParameters) SamplingResult {
x := binary.BigEndian.Uint64(p.TraceID[0:8]) >> 1
if x < ts.traceIDUpperBound {
return SamplingResult{Decision: RecordAndSample}
return SamplingResult{
Decision: RecordAndSample,
Tracestate: p.ParentContext.TraceState,
}
}
return SamplingResult{
Decision: Drop,
Tracestate: p.ParentContext.TraceState,
}
return SamplingResult{Decision: Drop}
}

func (ts traceIDRatioSampler) Description() string {
Expand Down Expand Up @@ -102,7 +109,10 @@ func TraceIDRatioBased(fraction float64) Sampler {
type alwaysOnSampler struct{}

func (as alwaysOnSampler) ShouldSample(p SamplingParameters) SamplingResult {
return SamplingResult{Decision: RecordAndSample}
return SamplingResult{
Decision: RecordAndSample,
Tracestate: p.ParentContext.TraceState,
}
}

func (as alwaysOnSampler) Description() string {
Expand All @@ -120,7 +130,10 @@ func AlwaysSample() Sampler {
type alwaysOffSampler struct{}

func (as alwaysOffSampler) ShouldSample(p SamplingParameters) SamplingResult {
return SamplingResult{Decision: Drop}
return SamplingResult{
Decision: Drop,
Tracestate: p.ParentContext.TraceState,
}
}

func (as alwaysOffSampler) Description() string {
Expand Down
45 changes: 45 additions & 0 deletions sdk/trace/sampling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/stretchr/testify/require"

"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/trace"
)

Expand Down Expand Up @@ -189,3 +190,47 @@ func TestTraceIdRatioSamplesInclusively(t *testing.T) {
}
}
}

func TestTracestateIsPassed(t *testing.T) {
testCases := []struct {
name string
sampler Sampler
}{
{
"notSampled",
NeverSample(),
},
{
"sampled",
AlwaysSample(),
},
{
"parentSampled",
ParentBased(AlwaysSample()),
},
{
"parentNotSampled",
ParentBased(NeverSample()),
},
{
"traceIDRatioSampler",
TraceIDRatioBased(.5),
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
traceState, err := trace.TraceStateFromKeyValues(label.String("k", "v"))
if err != nil {
t.Error(err)
}

parentCtx := trace.SpanContext{
TraceState: traceState,
}
params := SamplingParameters{ParentContext: parentCtx}

require.Equal(t, traceState, tc.sampler.ShouldSample(params).Tracestate, "TraceState is not equal")
})
}
}

0 comments on commit 40f1c00

Please sign in to comment.