Skip to content

Commit

Permalink
chore: add tests with spaces, fix style
Browse files Browse the repository at this point in the history
Signed-off-by: Ruben <ruben.vp8510@gmail.com>
  • Loading branch information
rubenvp8510 authored and Ruben committed May 18, 2020
1 parent 931ffaa commit 9839316
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
HttpTextPropagator,
SetterFunction,
} from '@opentelemetry/api';

import {
getCorrelationContext,
setCorrelationContext,
Expand All @@ -46,29 +45,26 @@ export const MAX_TOTAL_LENGTH = 8192;
*/
export class HttpCorrelationContext implements HttpTextPropagator {
inject(context: Context, carrier: unknown, setter: SetterFunction) {
const distContext = getCorrelationContext(context);
if (distContext) {
const all = Object.keys(distContext);
const values = all
.map(
(key: string) =>
`${encodeURIComponent(key)}=${encodeURIComponent(
distContext[key].value
)}`
)
.filter((pair: string) => {
return pair.length <= MAX_PER_NAME_VALUE_PAIRS;
})
.slice(0, MAX_NAME_VALUE_PAIRS);
const headerValue = values.reduce((hValue: String, current: String) => {
const value = `${hValue}${
hValue != '' ? ITEMS_SEPARATOR : ''
}${current}`;
return value.length > MAX_TOTAL_LENGTH ? hValue : value;
}, '');
if (headerValue.length > 0) {
setter(carrier, CORRELATION_CONTEXT_HEADER, headerValue);
}
const correlationContext = getCorrelationContext(context);
if (!correlationContext) return;
const all = Object.keys(correlationContext);
const values = all
.map(
(key: string) =>
`${encodeURIComponent(key)}=${encodeURIComponent(
correlationContext[key].value
)}`
)
.filter((pair: string) => {
return pair.length <= MAX_PER_NAME_VALUE_PAIRS;
})
.slice(0, MAX_NAME_VALUE_PAIRS);
const headerValue = values.reduce((hValue: String, current: String) => {
const value = `${hValue}${hValue != '' ? ITEMS_SEPARATOR : ''}${current}`;
return value.length > MAX_TOTAL_LENGTH ? hValue : value;
}, '');
if (headerValue.length > 0) {
setter(carrier, CORRELATION_CONTEXT_HEADER, headerValue);
}
}

Expand All @@ -78,7 +74,7 @@ export class HttpCorrelationContext implements HttpTextPropagator {
CORRELATION_CONTEXT_HEADER
) as string;
if (!headerValue) return context;
const distributedContext: CorrelationContext = {};
const correlationContext: CorrelationContext = {};
if (headerValue.length > 0) {
const pairs = headerValue.split(ITEMS_SEPARATOR);
if (pairs.length == 1) return context;
Expand All @@ -97,12 +93,12 @@ export class HttpCorrelationContext implements HttpTextPropagator {
PROPERTIES_SEPARATOR +
valueProps.join(PROPERTIES_SEPARATOR);
}
distributedContext[key] = { value };
correlationContext[key] = { value };
}
}
}
});
}
return setCorrelationContext(context, distributedContext);
return setCorrelationContext(context, correlationContext);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Copyright 2019, OpenTelemetry Authors
* Copyright 2020, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,12 +21,10 @@ import {
} from '@opentelemetry/api';
import { Context } from '@opentelemetry/context-base';
import * as assert from 'assert';

import {
getCorrelationContext,
setCorrelationContext,
} from '../../src/correlation-context/correlation-context';

import {
HttpCorrelationContext,
CORRELATION_CONTEXT_HEADER,
Expand All @@ -43,7 +41,7 @@ describe('HttpCorrelationContext', () => {
});

describe('.inject()', () => {
it('should set traceparent header', () => {
it('should set correlation context header', () => {
const correlationContext: CorrelationContext = {
key1: { value: 'd4cda95b652f4a1592b449d5929fda1b' },
key3: { value: 'c88815a7-0fa9-4d95-a1f1-cdccce3c5c2a' },
Expand Down Expand Up @@ -116,14 +114,17 @@ describe('HttpCorrelationContext', () => {

describe('.extract()', () => {
it('should extract context of a sampled span from carrier', () => {
carrier[CORRELATION_CONTEXT_HEADER] = 'key1=d4cda95b,key3=c88815a7';
carrier[CORRELATION_CONTEXT_HEADER] =
'key1=d4cda95b,key3=c88815a7, keyn = valn, keym =valm';
const extractedCorrelationContext = getCorrelationContext(
httpTraceContext.extract(Context.ROOT_CONTEXT, carrier, defaultGetter)
);

const expected: CorrelationContext = {
key1: { value: 'd4cda95b' },
key3: { value: 'c88815a7' },
keyn: { value: 'valn' },
keym: { value: 'valm' },
};
assert.deepStrictEqual(extractedCorrelationContext, expected);
});
Expand Down

0 comments on commit 9839316

Please sign in to comment.