diff --git a/packages/opentelemetry-core/src/context/propagation/JaegerHttpTraceFormat.ts b/packages/opentelemetry-core/src/context/propagation/JaegerHttpTraceFormat.ts index a2f758b1ec..e1dbbf999c 100644 --- a/packages/opentelemetry-core/src/context/propagation/JaegerHttpTraceFormat.ts +++ b/packages/opentelemetry-core/src/context/propagation/JaegerHttpTraceFormat.ts @@ -14,9 +14,9 @@ * limitations under the License. */ -import { SpanContext, HttpTextFormat, TraceFlags } from '@opentelemetry/types'; +import { SpanContext, HttpTextFormat, TraceFlags } from "@opentelemetry/types"; -export const UBER_TRACE_ID_HEADER = 'uber-trace-id'; +export const UBER_TRACE_ID_HEADER = "uber-trace-id"; /** * Propagates {@link SpanContext} through Trace Context format propagation. @@ -24,24 +24,12 @@ export const UBER_TRACE_ID_HEADER = 'uber-trace-id'; * {trace-id} * 64-bit or 128-bit random number in base16 format * Can be variable length, shorter values are 0-padded on the left - * Clients in some languages support 128-bit, migration pending * Value of 0 is invalid * {span-id} * 64-bit random number in base16 format - * {parent-span-id} - * 64-bit value in base16 format representing parent span id - * Deprecated, most Jaeger clients ignore on the receiving side, but still include it on the sending side - * 0 value is valid and means “root span” (when not ignored) + * {parent-span-id} set to 0 because this field is deprecated * {flags} * One byte bitmap, as two hex digits - * Bit 1 (right-most, least significant) is “sampled” flag - * 1 means the trace is sampled and all downstream services are advised to respect that - * 0 means the trace is not sampled and all downstream services are advised to respect that - * We’re considering a new feature that allows downstream services to upsample if they find their tracing level is too low - * Bit 2 is “debug” flag - * Debug flag should only be set when the sampled flag is set - * Instructs the backend to try really hard not to drop this trace - * Other bits are unused. * Inspired by jaeger-client-node project */ export class JaegerHttpTraceFormat implements HttpTextFormat { @@ -52,12 +40,9 @@ export class JaegerHttpTraceFormat implements HttpTextFormat { ) { const hexTraceId = removeLeadingZeros(spanContext.traceId); const hexSpanId = removeLeadingZeros(spanContext.spanId); - const parentSpanId = '0'; const flags = TraceFlags.SAMPLED; - carrier[ - UBER_TRACE_ID_HEADER - ] = `${hexTraceId}:${hexSpanId}:${parentSpanId}:${flags}`; + carrier[UBER_TRACE_ID_HEADER] = `${hexTraceId}:${hexSpanId}:0:${flags}`; } extract( @@ -79,17 +64,7 @@ export class JaegerHttpTraceFormat implements HttpTextFormat { * @return {string} - returns the input string without leading zeros. **/ function removeLeadingZeros(input: string): string { - let counter = 0; - let length = input.length - 1; - for (let i = 0; i < length; i++) { - if (input.charAt(i) === '0') { - counter++; - } else { - break; - } - } - - return input.substring(counter); + return input.replace(/^0+/, ""); } /** @@ -97,16 +72,13 @@ function removeLeadingZeros(input: string): string { * @return {SpanContext} - returns a span context represented by the serializedString. **/ function deserializeSpanContext(serializedString: string): SpanContext | null { - let headers = serializedString.split(':'); + let headers = serializedString.split(":"); if (headers.length !== 4) { return null; } const [traceId, spanId, , flags] = headers; - const traceFlags = - Number('0x' + (isNaN(Number(flags)) ? 1 : Number(flags))) & 1; - - const isRemote = true; + const traceFlags = flags.match(/^[0-9a-f]{2}$/i) ? parseInt(flags) & 1 : 1; - return { traceId, spanId, isRemote, traceFlags }; + return { traceId, spanId, isRemote: true, traceFlags }; }