Skip to content

Commit

Permalink
feat: add IsRemote field to SpanContext, set by propagators (#451)
Browse files Browse the repository at this point in the history
* feat(opentelemetry-types): add isRemote field

* fix(opentelemetry-core): set isRemote true when extracting

* fix(opentelemetry-core): set isRemote on BinaryTraceContext

* fix(opentelemetry-types): adjust isRemote comment

Makes it more obvious when the flag changes and what the default value is.
  • Loading branch information
KaindlJulian authored and mayurkale22 committed Oct 24, 2019
1 parent f33dc7c commit 1975647
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export class B3Format implements HttpTextFormat {
return {
traceId,
spanId,
isRemote: true,
traceFlags: isNaN(Number(options))
? TraceFlags.UNSAMPLED
: Number(options),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ export class BinaryTraceContext implements BinaryFormat {
) {
return null;
}

result.isRemote = true;

// See serializeSpanContext for byte offsets.
result.traceId = toHex(buf.slice(TRACE_ID_OFFSET, SPAN_ID_FIELD_ID_OFFSET));
result.spanId = toHex(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ export class HttpTraceContext implements HttpTextFormat {
const spanContext = parse(traceParent);
if (!spanContext) return null;

spanContext.isRemote = true;

const traceStateHeader = carrier[TRACE_STATE_HEADER];
if (traceStateHeader) {
// If more than one `tracestate` header is found, we merge them into a
Expand Down
6 changes: 6 additions & 0 deletions packages/opentelemetry-core/test/context/B3Format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ describe('B3Format', () => {
spanId: '6e0c63257de34c92',
traceFlags: TraceFlags.UNSAMPLED,
traceState: new TraceState('foo=bar,baz=qux'),
isRemote: false,
};

b3Format.inject(spanContext, 'B3Format', carrier);
Expand Down Expand Up @@ -101,6 +102,7 @@ describe('B3Format', () => {
assert.deepStrictEqual(extractedSpanContext, {
spanId: 'b7ad6b7169203331',
traceId: '0af7651916cd43dd8448eb211c80319c',
isRemote: true,
traceFlags: TraceFlags.UNSAMPLED,
});
});
Expand All @@ -114,6 +116,7 @@ describe('B3Format', () => {
assert.deepStrictEqual(extractedSpanContext, {
spanId: 'b7ad6b7169203331',
traceId: '0af7651916cd43dd8448eb211c80319c',
isRemote: true,
traceFlags: TraceFlags.SAMPLED,
});
});
Expand All @@ -127,6 +130,7 @@ describe('B3Format', () => {
assert.deepStrictEqual(extractedSpanContext, {
spanId: 'b7ad6b7169203331',
traceId: '0af7651916cd43dd8448eb211c80319c',
isRemote: true,
traceFlags: TraceFlags.SAMPLED,
});
});
Expand All @@ -140,6 +144,7 @@ describe('B3Format', () => {
assert.deepStrictEqual(extractedSpanContext, {
spanId: 'b7ad6b7169203331',
traceId: '0af7651916cd43dd8448eb211c80319c',
isRemote: true,
traceFlags: TraceFlags.UNSAMPLED,
});
});
Expand Down Expand Up @@ -173,6 +178,7 @@ describe('B3Format', () => {
assert.deepStrictEqual(extractedSpanContext, {
spanId: 'b7ad6b7169203331',
traceId: '0af7651916cd43dd8448eb211c80319c',
isRemote: true,
traceFlags: TraceFlags.SAMPLED,
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ describe('BinaryTraceContext', () => {
binaryTraceContext.fromBytes(testCase.binary),
testCase.structured &&
Object.assign(
{ traceFlags: TraceFlags.UNSAMPLED },
{ isRemote: true, traceFlags: TraceFlags.UNSAMPLED },
testCase.structured
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ describe('HttpTraceContext', () => {
assert.deepStrictEqual(extractedSpanContext, {
spanId: 'b7ad6b7169203331',
traceId: '0af7651916cd43dd8448eb211c80319c',
isRemote: true,
traceFlags: TraceFlags.SAMPLED,
});
});
Expand Down Expand Up @@ -106,6 +107,7 @@ describe('HttpTraceContext', () => {
assert.deepStrictEqual(extractedSpanContext, {
spanId: 'b7ad6b7169203331',
traceId: '0af7651916cd43dd8448eb211c80319c',
isRemote: true,
traceFlags: TraceFlags.SAMPLED,
});
});
Expand Down Expand Up @@ -139,6 +141,7 @@ describe('HttpTraceContext', () => {
assert.deepStrictEqual(extractedSpanContext, {
spanId: 'b7ad6b7169203331',
traceId: '0af7651916cd43dd8448eb211c80319c',
isRemote: true,
traceFlags: TraceFlags.SAMPLED,
traceState: new TraceState('foo=bar,baz=qux,quux=quuz'),
});
Expand Down
4 changes: 4 additions & 0 deletions packages/opentelemetry-types/src/trace/span_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export interface SpanContext {
* lowercase hex characters corresponding to 64 bits.
*/
spanId: string;
/**
* Only true if the SpanContext was propagated from a remote parent.
*/
isRemote?: boolean;
/**
* Trace flags to propagate.
*
Expand Down

0 comments on commit 1975647

Please sign in to comment.