Skip to content

Commit 860d5d8

Browse files
Aneurysm9MrAlias
andauthored
Add flag to determine whether SpanContext is remote (#1701)
* Add remote property to SpanContext * Set SpanContext.remote when extracting context in TraceContext propagator * Ensure remote flag is set when inserting remote SpanContext into context * Ensure tests are expecting remote flag in SpanContext where appropriate * Update CHANGELOG.md * Apply PR feedback Co-authored-by: Tyler Yahn <[email protected]> Co-authored-by: Tyler Yahn <[email protected]>
1 parent 0fe65e6 commit 860d5d8

File tree

8 files changed

+49
-3
lines changed

8 files changed

+49
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1414
- A `ForceFlush` method to the `"go.opentelemetry.io/otel/sdk/trace".TracerProvider` to flush all registered `SpanProcessor`s. (#1608)
1515
- Added `WithDefaultSampler` and `WithSpanLimits` to tracer provider. (#1633)
1616
- Jaeger exporter falls back to `resource.Default`'s `service.name` if the exported Span does not have one. (#1673)
17+
- `"go.opentelemetry.io/otel/trace".SpanContext` now has a `remote` property, and `IsRemote()` predicate, that is true when the `SpanContext` has been extracted from remote context data. (#1701)
1718
- A `Valid` method to the `"go.opentelemetry.io/otel/attribute".KeyValue` type. (#1703)
1819

1920
### Changed

exporters/stdout/trace_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func TestExporter_ExportSpan(t *testing.T) {
8383
`{` +
8484
`"Key":"key",` +
8585
`"Value":{"Type":"STRING","Value":"val"}` +
86-
`}]},` +
86+
`}],"Remote":false},` +
8787
`"ParentSpanID":"0000000000000000",` +
8888
`"SpanKind":1,` +
8989
`"Name":"/foo",` +

oteltest/tracer_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ func TestTracer(t *testing.T) {
184184
parentSpanContext := parentSpan.SpanContext()
185185
remoteParentSpanContext := remoteParentSpan.SpanContext()
186186
parentCtx = trace.ContextWithRemoteSpanContext(parentCtx, remoteParentSpanContext)
187+
// remote SpanContexts will be marked as remote
188+
remoteParentSpanContext = remoteParentSpanContext.WithRemote(true)
187189

188190
_, span := subject.Start(parentCtx, "child", trace.WithNewRoot())
189191

propagation/trace_context.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ func (tc TraceContext) extract(carrier TextMapCarrier) trace.SpanContext {
133133
scc.TraceFlags = opts[0] & trace.FlagsSampled
134134

135135
scc.TraceState = parseTraceState(carrier.Get(tracestateHeader))
136+
scc.Remote = true
136137

137138
sc := trace.NewSpanContext(scc)
138139
if !sc.IsValid() {

propagation/trace_context_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func TestExtractValidTraceContextFromHTTPReq(t *testing.T) {
4040
wantSc: trace.NewSpanContext(trace.SpanContextConfig{
4141
TraceID: traceID,
4242
SpanID: spanID,
43+
Remote: true,
4344
}),
4445
},
4546
{
@@ -49,6 +50,7 @@ func TestExtractValidTraceContextFromHTTPReq(t *testing.T) {
4950
TraceID: traceID,
5051
SpanID: spanID,
5152
TraceFlags: trace.FlagsSampled,
53+
Remote: true,
5254
}),
5355
},
5456
{
@@ -58,6 +60,7 @@ func TestExtractValidTraceContextFromHTTPReq(t *testing.T) {
5860
TraceID: traceID,
5961
SpanID: spanID,
6062
TraceFlags: trace.FlagsSampled,
63+
Remote: true,
6164
}),
6265
},
6366
{
@@ -67,6 +70,7 @@ func TestExtractValidTraceContextFromHTTPReq(t *testing.T) {
6770
TraceID: traceID,
6871
SpanID: spanID,
6972
TraceFlags: trace.FlagsSampled,
73+
Remote: true,
7074
}),
7175
},
7276
{
@@ -75,6 +79,7 @@ func TestExtractValidTraceContextFromHTTPReq(t *testing.T) {
7579
wantSc: trace.NewSpanContext(trace.SpanContextConfig{
7680
TraceID: traceID,
7781
SpanID: spanID,
82+
Remote: true,
7883
}),
7984
},
8085
{
@@ -84,6 +89,7 @@ func TestExtractValidTraceContextFromHTTPReq(t *testing.T) {
8489
TraceID: traceID,
8590
SpanID: spanID,
8691
TraceFlags: trace.FlagsSampled,
92+
Remote: true,
8793
}),
8894
},
8995
{
@@ -93,6 +99,7 @@ func TestExtractValidTraceContextFromHTTPReq(t *testing.T) {
9399
TraceID: traceID,
94100
SpanID: spanID,
95101
TraceFlags: trace.FlagsSampled,
102+
Remote: true,
96103
}),
97104
},
98105
{
@@ -102,6 +109,7 @@ func TestExtractValidTraceContextFromHTTPReq(t *testing.T) {
102109
TraceID: traceID,
103110
SpanID: spanID,
104111
TraceFlags: trace.FlagsSampled,
112+
Remote: true,
105113
}),
106114
},
107115
}
@@ -302,6 +310,7 @@ func TestTraceStatePropagation(t *testing.T) {
302310
TraceID: traceID,
303311
SpanID: spanID,
304312
TraceState: state,
313+
Remote: true,
305314
}),
306315
},
307316
{
@@ -314,6 +323,7 @@ func TestTraceStatePropagation(t *testing.T) {
314323
wantSc: trace.NewSpanContext(trace.SpanContextConfig{
315324
TraceID: traceID,
316325
SpanID: spanID,
326+
Remote: true,
317327
}),
318328
},
319329
{
@@ -326,6 +336,7 @@ func TestTraceStatePropagation(t *testing.T) {
326336
wantSc: trace.NewSpanContext(trace.SpanContextConfig{
327337
TraceID: traceID,
328338
SpanID: spanID,
339+
Remote: true,
329340
}),
330341
},
331342
}

sdk/trace/trace_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,7 @@ func TestReadOnlySpan(t *testing.T) {
13851385
TraceID: tID,
13861386
SpanID: sID,
13871387
TraceFlags: 0x1,
1388+
Remote: true,
13881389
})
13891390
ctx := trace.ContextWithRemoteSpanContext(context.Background(), parent)
13901391

trace/trace.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ type SpanContextConfig struct {
326326
SpanID SpanID
327327
TraceFlags byte
328328
TraceState TraceState
329+
Remote bool
329330
}
330331

331332
// NewSpanContext constructs a SpanContext using values from the provided
@@ -336,6 +337,7 @@ func NewSpanContext(config SpanContextConfig) SpanContext {
336337
spanID: config.SpanID,
337338
traceFlags: config.TraceFlags,
338339
traceState: config.TraceState,
340+
remote: config.Remote,
339341
}
340342
}
341343

@@ -345,6 +347,7 @@ type SpanContext struct {
345347
spanID SpanID
346348
traceFlags byte
347349
traceState TraceState
350+
remote bool
348351
}
349352

350353
// IsValid returns if the SpanContext is valid. A valid span context has a
@@ -353,6 +356,22 @@ func (sc SpanContext) IsValid() bool {
353356
return sc.HasTraceID() && sc.HasSpanID()
354357
}
355358

359+
// IsRemote indicates whether the SpanContext represents a remotely-created Span.
360+
func (sc SpanContext) IsRemote() bool {
361+
return sc.remote
362+
}
363+
364+
// WithRemote returns a copy of sc with the Remote property set to remote.
365+
func (sc SpanContext) WithRemote(remote bool) SpanContext {
366+
return SpanContext{
367+
traceID: sc.traceID,
368+
spanID: sc.spanID,
369+
traceFlags: sc.traceFlags,
370+
traceState: sc.traceState,
371+
remote: remote,
372+
}
373+
}
374+
356375
// TraceID returns the TraceID from the SpanContext.
357376
func (sc SpanContext) TraceID() TraceID {
358377
return sc.traceID
@@ -370,6 +389,7 @@ func (sc SpanContext) WithTraceID(traceID TraceID) SpanContext {
370389
spanID: sc.spanID,
371390
traceFlags: sc.traceFlags,
372391
traceState: sc.traceState,
392+
remote: sc.remote,
373393
}
374394
}
375395

@@ -390,6 +410,7 @@ func (sc SpanContext) WithSpanID(spanID SpanID) SpanContext {
390410
spanID: spanID,
391411
traceFlags: sc.traceFlags,
392412
traceState: sc.traceState,
413+
remote: sc.remote,
393414
}
394415
}
395416

@@ -405,6 +426,7 @@ func (sc SpanContext) WithTraceFlags(flags byte) SpanContext {
405426
spanID: sc.spanID,
406427
traceFlags: flags,
407428
traceState: sc.traceState,
429+
remote: sc.remote,
408430
}
409431
}
410432

@@ -435,6 +457,7 @@ func (sc SpanContext) WithTraceState(state TraceState) SpanContext {
435457
spanID: sc.spanID,
436458
traceFlags: sc.traceFlags,
437459
traceState: state,
460+
remote: sc.remote,
438461
}
439462
}
440463

@@ -443,7 +466,8 @@ func (sc SpanContext) Equal(other SpanContext) bool {
443466
return sc.traceID == other.traceID &&
444467
sc.spanID == other.spanID &&
445468
sc.traceFlags == other.traceFlags &&
446-
sc.traceState.String() == other.traceState.String()
469+
sc.traceState.String() == other.traceState.String() &&
470+
sc.remote == other.remote
447471
}
448472

449473
// MarshalJSON implements a custom marshal function to encode a SpanContext.
@@ -453,6 +477,7 @@ func (sc SpanContext) MarshalJSON() ([]byte, error) {
453477
SpanID: sc.spanID,
454478
TraceFlags: sc.traceFlags,
455479
TraceState: sc.traceState,
480+
Remote: sc.remote,
456481
})
457482
}
458483

@@ -487,7 +512,7 @@ func SpanContextFromContext(ctx context.Context) SpanContext {
487512
// ContextWithRemoteSpanContext returns a copy of parent with a remote set as
488513
// the remote span context.
489514
func ContextWithRemoteSpanContext(parent context.Context, remote SpanContext) context.Context {
490-
return context.WithValue(parent, remoteContextKey, remote)
515+
return context.WithValue(parent, remoteContextKey, remote.WithRemote(true))
491516
}
492517

493518
// RemoteSpanContextFromContext returns the remote span context from ctx.

trace/trace_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ func TestContextRemoteSpanContext(t *testing.T) {
8080

8181
want := SpanContext{traceID: [16]byte{1}, spanID: [8]byte{42}}
8282
ctx = ContextWithRemoteSpanContext(ctx, want)
83+
want = want.WithRemote(true)
84+
8385
if got, ok := ctx.Value(remoteContextKey).(SpanContext); !ok {
8486
t.Errorf("failed to set SpanContext with %#v", want)
8587
} else if !assertSpanContextEqual(got, want) {
@@ -92,6 +94,8 @@ func TestContextRemoteSpanContext(t *testing.T) {
9294

9395
want = SpanContext{traceID: [16]byte{1}, spanID: [8]byte{43}}
9496
ctx = ContextWithRemoteSpanContext(ctx, want)
97+
want = want.WithRemote(true)
98+
9599
if got, ok := ctx.Value(remoteContextKey).(SpanContext); !ok {
96100
t.Errorf("failed to set SpanContext with %#v", want)
97101
} else if !assertSpanContextEqual(got, want) {
@@ -982,6 +986,7 @@ func assertSpanContextEqual(got SpanContext, want SpanContext) bool {
982986
return got.spanID == want.spanID &&
983987
got.traceID == want.traceID &&
984988
got.traceFlags == want.traceFlags &&
989+
got.remote == want.remote &&
985990
assertTraceStateEqual(got.traceState, want.traceState)
986991
}
987992

0 commit comments

Comments
 (0)