Skip to content

Commit

Permalink
feat: add jaeger http trace format (open-telemetry#696)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladislav-kiva committed Jan 16, 2020
1 parent 4b2a9d3 commit 1a206cb
Showing 1 changed file with 8 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,22 @@
* 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.
* {trace-id}:{span-id}:{parent-span-id}:{flags}
* {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 {
Expand All @@ -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(
Expand All @@ -79,34 +64,21 @@ 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+/, "");
}

/**
* @param {string} serializedString - a serialized span context.
* @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 };
}

0 comments on commit 1a206cb

Please sign in to comment.