Skip to content

Commit

Permalink
fix: correlation context propagation extract for a single entry
Browse files Browse the repository at this point in the history
Signed-off-by: Ruben Vargas <ruben.vp8510@gmail.com>
  • Loading branch information
rubenvp8510 committed Jul 28, 2020
1 parent 0b6dc93 commit 986f631
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,15 @@ export class HttpCorrelationContext implements HttpTextPropagator {
return context;
}
const pairs = headerValue.split(ITEMS_SEPARATOR);
if (pairs.length == 1) return context;
pairs.forEach(entry => {
const keyPair = this._parsePairKeyValue(entry);
if (keyPair) {
correlationContext[keyPair.key] = { value: keyPair.value };
}
});
if (Object.entries(correlationContext).length <= 0) {
return context;
}
return setCorrelationContext(context, correlationContext);
}

Expand All @@ -107,7 +109,7 @@ export class HttpCorrelationContext implements HttpTextPropagator {
const keyPairPart = valueProps.shift();
if (!keyPairPart) return;
const keyPair = keyPairPart.split(KEY_PAIR_SEPARATOR);
if (keyPair.length <= 1) return;
if (keyPair.length != 2) return;
const key = decodeURIComponent(keyPair[0].trim());
let value = decodeURIComponent(keyPair[1].trim());
if (valueProps.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,49 @@ describe('HttpCorrelationContext', () => {
});

it('should gracefully handle an invalid header', () => {
const testCases: Record<string, string> = {
invalidNoKeyValuePair: '289371298nekjh2939299283jbk2b',
invalidDoubleEqual: 'key1==value;key2=value2',
invalidWrongKeyValueFormat: 'key1:value;key2=value2',
invalidDoubleSemicolon: 'key1:value;;key2=value2',
const testCases: Record<
string,
{
header: string;
correlationContext: CorrelationContext | undefined;
}
> = {
invalidNoKeyValuePair: {
header: '289371298nekjh2939299283jbk2b',
correlationContext: undefined,
},
invalidDoubleEqual: {
header: 'key1==value;key2=value2',
correlationContext: undefined,
},
invalidWrongKeyValueFormat: {
header: 'key1:value;key2=value2',
correlationContext: undefined,
},
invalidDoubleSemicolon: {
header: 'key1:value;;key2=value2',
correlationContext: undefined,
},
mixInvalidAndValidKeys: {
header: 'key1==value,key2=value2',
correlationContext: {
key2: {
value: 'value2',
},
},
},
};
Object.getOwnPropertyNames(testCases).forEach(testCase => {
carrier[CORRELATION_CONTEXT_HEADER] = testCases[testCase];
carrier[CORRELATION_CONTEXT_HEADER] = testCases[testCase].header;

const extractedSpanContext = getCorrelationContext(
httpTraceContext.extract(Context.ROOT_CONTEXT, carrier, defaultGetter)
);
assert.deepStrictEqual(extractedSpanContext, undefined, testCase);
assert.deepStrictEqual(
extractedSpanContext,
testCases[testCase].correlationContext,
testCase
);
});
});
});

0 comments on commit 986f631

Please sign in to comment.