Skip to content

Commit

Permalink
feat(@opentelemetry/propagator-jaeger): support custom baggage prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
sschegolev committed Dec 16, 2021
1 parent 10cd916 commit fd18720
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/opentelemetry-propagator-jaeger/src/JaegerPropagator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import { isTracingSuppressed } from '@opentelemetry/core';

export const UBER_TRACE_ID_HEADER = 'uber-trace-id';
export const UBER_BAGGAGE_HEADER_PREFIX = 'uberctx';
const UBER_BAGGAGE_HEADER_REGEX = /^uberctx-(.+)/i;

/**
* Propagates {@link SpanContext} through Trace Context format propagation.
Expand All @@ -47,12 +46,15 @@ const UBER_BAGGAGE_HEADER_REGEX = /^uberctx-(.+)/i;
*/
export class JaegerPropagator implements TextMapPropagator {
private readonly _jaegerTraceHeader: string;
private readonly _jaegerBaggageHeaderPrefix: string;

/**
* @param {string} [customTraceHeader="uber-trace-id"] - HTTP header to inject\extract trace from.
* @param {string} [customBaggageHeaderPrefix="uberctx"] - Baggage header prefix.
**/
constructor(customTraceHeader?: string) {
constructor(customTraceHeader?: string, customBaggageHeaderPrefix?: string) {
this._jaegerTraceHeader = customTraceHeader || UBER_TRACE_ID_HEADER;
this._jaegerBaggageHeaderPrefix = customBaggageHeaderPrefix || UBER_BAGGAGE_HEADER_PREFIX;
}

inject(context: Context, carrier: unknown, setter: TextMapSetter): void {
Expand All @@ -74,7 +76,7 @@ export class JaegerPropagator implements TextMapPropagator {
for (const [key, entry] of baggage.getAllEntries()) {
setter.set(
carrier,
`${UBER_BAGGAGE_HEADER_PREFIX}-${key}`,
`${this._jaegerBaggageHeaderPrefix}-${key}`,
encodeURIComponent(entry.value)
);
}
Expand All @@ -88,11 +90,11 @@ export class JaegerPropagator implements TextMapPropagator {
: uberTraceIdHeader;
const baggageValues = getter
.keys(carrier)
.filter(key => UBER_BAGGAGE_HEADER_REGEX.test(key))
.filter(key => key.startsWith(`${this._jaegerBaggageHeaderPrefix}-`))
.map(key => {
const value = getter.get(carrier, key);
return {
key: key.substring(UBER_BAGGAGE_HEADER_PREFIX.length + 1),
key: key.substring(this._jaegerBaggageHeaderPrefix.length + 1),
value: Array.isArray(value) ? value[0] : value,
};
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
describe('JaegerPropagator', () => {
const jaegerPropagator = new JaegerPropagator();
const customHeader = 'new-header';
const customBaggageHeaderPrefix = 'custom-baggage-header-prefix';
const customJaegerPropagator = new JaegerPropagator(customHeader);
const customBaggagePrefixJaegerPropagator = new JaegerPropagator(undefined, customBaggageHeaderPrefix);
let carrier: { [key: string]: unknown };

beforeEach(() => {
Expand Down Expand Up @@ -114,6 +116,28 @@ describe('JaegerPropagator', () => {
encodeURIComponent('%id%')
);
});

it('should propagate baggage with custom prefix with url encoded values', () => {
const baggage = propagation.createBaggage({
test: {
value: '1',
},
myuser: {
value: '%id%',
},
});

customBaggagePrefixJaegerPropagator.inject(
propagation.setBaggage(ROOT_CONTEXT, baggage),
carrier,
defaultTextMapSetter
);
assert.strictEqual(carrier[`${customBaggageHeaderPrefix}-test`], '1');
assert.strictEqual(
carrier[`${customBaggageHeaderPrefix}-myuser`],
encodeURIComponent('%id%')
);
});
});

describe('.extract()', () => {
Expand Down Expand Up @@ -215,6 +239,21 @@ describe('JaegerPropagator', () => {
assert(secondEntry.value === '%id%');
});

it('should extract baggage with custom prefix from carrier', () => {
carrier[`${customBaggageHeaderPrefix}-test`] = 'value';
carrier[`${customBaggageHeaderPrefix}-myuser`] = '%25id%25';
const extractedBaggage = propagation.getBaggage(
customBaggagePrefixJaegerPropagator.extract(ROOT_CONTEXT, carrier, defaultTextMapGetter)
);

const firstEntry = extractedBaggage?.getEntry('test');
assert(typeof firstEntry !== 'undefined');
assert(firstEntry.value === 'value');
const secondEntry = extractedBaggage?.getEntry('myuser');
assert(typeof secondEntry !== 'undefined');
assert(secondEntry.value === '%id%');
});

it('should extract baggage from carrier and not override current one', () => {
carrier[`${UBER_BAGGAGE_HEADER_PREFIX}-test`] = 'value';
carrier[`${UBER_BAGGAGE_HEADER_PREFIX}-myuser`] = '%25id%25';
Expand Down

0 comments on commit fd18720

Please sign in to comment.