Skip to content

Commit

Permalink
fix: address review comments and use context
Browse files Browse the repository at this point in the history
  • Loading branch information
srjames90 committed Jul 25, 2020
1 parent bfaa63c commit 30a8b86
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 99 deletions.
19 changes: 0 additions & 19 deletions packages/opentelemetry-api/src/trace/span_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,6 @@ export interface SpanContext {
* SAMPLED = 0x1 and NONE = 0x0;
*/
traceFlags: TraceFlags;
/**
* Debug flag to propagate.
*
* This flag is relevant to b3. According to the b3 spec,
* Debug is encoded as X-B3-Flags: 1. Absent or any other value can be ignored.
* Debug is a production troubleshooting aid used in tools like curl or chrome debug.
* Debug is an emphasized accept decision that implies accept (or setting traceFlags to accept),
* additionally reporting Span.debug = true for each span in the trace. Adding this
* to the span context because the b3 spec says when this flag is set properly,
* do not send the sampled header as well.
*/
debug?: boolean;
/**
*
* The ID of the parent span propagated by b3. Encoded as a 16
* lowercase hex characters corresponding to 64 bits.
* May be present on a child span and must be absent on the root span
*/
parentSpanId?: string;
/**
* Tracing-system-specific info to propagate.
*
Expand Down
24 changes: 13 additions & 11 deletions packages/opentelemetry-core/src/context/propagation/B3Propagator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export const X_B3_SPAN_ID = 'x-b3-spanid';
export const X_B3_SAMPLED = 'x-b3-sampled';
export const X_B3_PARENT_SPAN_ID = 'x-b3-parentspanid';
export const X_B3_FLAGS = 'x-b3-flags';
export const PARENT_SPAN_ID_KEY = Context.createKey(X_B3_PARENT_SPAN_ID);
export const DEBUG_FLAG_KEY = Context.createKey(X_B3_FLAGS);
const VALID_TRACEID_REGEX = /^([0-9a-f]{16}){1,2}$/i;
const VALID_SPANID_REGEX = /^[0-9a-f]{16}$/i;
const INVALID_ID_REGEX = /^0+$/i;
Expand All @@ -48,22 +50,23 @@ export class B3Propagator implements HttpTextPropagator {
inject(context: Context, carrier: unknown, setter: SetterFunction) {
const spanContext = getParentSpanContext(context);
if (!spanContext) return;
const parentSpanId = context.getValue(PARENT_SPAN_ID_KEY);
if (
isValidTraceId(spanContext.traceId) &&
isValidSpanId(spanContext.spanId)
) {
const parentSpanExists = !!spanContext.parentSpanId;
if (parentSpanExists) {
if (isValidTraceId(spanContext.parentSpanId || ''))
setter(carrier, X_B3_PARENT_SPAN_ID, spanContext.parentSpanId);
if (parentSpanId) {
if (isValidTraceId(parentSpanId as string))
setter(carrier, X_B3_PARENT_SPAN_ID, parentSpanId);
else return;
}
const debug = context.getValue(DEBUG_FLAG_KEY);
setter(carrier, X_B3_TRACE_ID, spanContext.traceId);
setter(carrier, X_B3_SPAN_ID, spanContext.spanId);
// According to the B3 spec, if the debug flag is set,
// the sampled flag shouldn't be propagated as well.
if (spanContext.debug) {
setter(carrier, X_B3_FLAGS, '1');
if (debug === '1') {
setter(carrier, X_B3_FLAGS, debug);
}
// We set the header only if there is an existing sampling decision.
// Otherwise we will omit it => Absent.
Expand Down Expand Up @@ -101,9 +104,7 @@ export class B3Propagator implements HttpTextPropagator {
const debugHeaderValue = Array.isArray(flagsHeader)
? flagsHeader[0]
: flagsHeader;
const debug = isNaN(Number(debugHeaderValue))
? false
: debugHeaderValue === '1';
const debug = debugHeaderValue === '1';
const traceFlagsOrDebug = Number(debug) || Number(options);

if (
Expand All @@ -114,6 +115,9 @@ export class B3Propagator implements HttpTextPropagator {
return context;
}

context = context.setValue(PARENT_SPAN_ID_KEY, parentSpanId);
context = context.setValue(DEBUG_FLAG_KEY, debug ? '1' : undefined);

const traceId = traceIdHeaderValue.padStart(32, '0');

if (isValidTraceId(traceId) && isValidSpanId(spanId)) {
Expand All @@ -123,8 +127,6 @@ export class B3Propagator implements HttpTextPropagator {
isRemote: true,
// Set traceFlags as 1 if debug is 1
traceFlags: traceFlagsOrDebug || TraceFlags.NONE,
debug,
parentSpanId,
});
}
return context;
Expand Down
Loading

0 comments on commit 30a8b86

Please sign in to comment.