Skip to content

Commit 9839316

Browse files
rubenvp8510Ruben
authored and
Ruben
committed
chore: add tests with spaces, fix style
Signed-off-by: Ruben <[email protected]>
1 parent 931ffaa commit 9839316

File tree

2 files changed

+29
-32
lines changed

2 files changed

+29
-32
lines changed

packages/opentelemetry-core/src/correlation-context/propagation/HttpCorrelationContext.ts

+23-27
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121
HttpTextPropagator,
2222
SetterFunction,
2323
} from '@opentelemetry/api';
24-
2524
import {
2625
getCorrelationContext,
2726
setCorrelationContext,
@@ -46,29 +45,26 @@ export const MAX_TOTAL_LENGTH = 8192;
4645
*/
4746
export class HttpCorrelationContext implements HttpTextPropagator {
4847
inject(context: Context, carrier: unknown, setter: SetterFunction) {
49-
const distContext = getCorrelationContext(context);
50-
if (distContext) {
51-
const all = Object.keys(distContext);
52-
const values = all
53-
.map(
54-
(key: string) =>
55-
`${encodeURIComponent(key)}=${encodeURIComponent(
56-
distContext[key].value
57-
)}`
58-
)
59-
.filter((pair: string) => {
60-
return pair.length <= MAX_PER_NAME_VALUE_PAIRS;
61-
})
62-
.slice(0, MAX_NAME_VALUE_PAIRS);
63-
const headerValue = values.reduce((hValue: String, current: String) => {
64-
const value = `${hValue}${
65-
hValue != '' ? ITEMS_SEPARATOR : ''
66-
}${current}`;
67-
return value.length > MAX_TOTAL_LENGTH ? hValue : value;
68-
}, '');
69-
if (headerValue.length > 0) {
70-
setter(carrier, CORRELATION_CONTEXT_HEADER, headerValue);
71-
}
48+
const correlationContext = getCorrelationContext(context);
49+
if (!correlationContext) return;
50+
const all = Object.keys(correlationContext);
51+
const values = all
52+
.map(
53+
(key: string) =>
54+
`${encodeURIComponent(key)}=${encodeURIComponent(
55+
correlationContext[key].value
56+
)}`
57+
)
58+
.filter((pair: string) => {
59+
return pair.length <= MAX_PER_NAME_VALUE_PAIRS;
60+
})
61+
.slice(0, MAX_NAME_VALUE_PAIRS);
62+
const headerValue = values.reduce((hValue: String, current: String) => {
63+
const value = `${hValue}${hValue != '' ? ITEMS_SEPARATOR : ''}${current}`;
64+
return value.length > MAX_TOTAL_LENGTH ? hValue : value;
65+
}, '');
66+
if (headerValue.length > 0) {
67+
setter(carrier, CORRELATION_CONTEXT_HEADER, headerValue);
7268
}
7369
}
7470

@@ -78,7 +74,7 @@ export class HttpCorrelationContext implements HttpTextPropagator {
7874
CORRELATION_CONTEXT_HEADER
7975
) as string;
8076
if (!headerValue) return context;
81-
const distributedContext: CorrelationContext = {};
77+
const correlationContext: CorrelationContext = {};
8278
if (headerValue.length > 0) {
8379
const pairs = headerValue.split(ITEMS_SEPARATOR);
8480
if (pairs.length == 1) return context;
@@ -97,12 +93,12 @@ export class HttpCorrelationContext implements HttpTextPropagator {
9793
PROPERTIES_SEPARATOR +
9894
valueProps.join(PROPERTIES_SEPARATOR);
9995
}
100-
distributedContext[key] = { value };
96+
correlationContext[key] = { value };
10197
}
10298
}
10399
}
104100
});
105101
}
106-
return setCorrelationContext(context, distributedContext);
102+
return setCorrelationContext(context, correlationContext);
107103
}
108104
}

packages/opentelemetry-core/test/correlation-context/HttpCorrelationContext.test.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Copyright 2019, OpenTelemetry Authors
2+
* Copyright 2020, OpenTelemetry Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,12 +21,10 @@ import {
2121
} from '@opentelemetry/api';
2222
import { Context } from '@opentelemetry/context-base';
2323
import * as assert from 'assert';
24-
2524
import {
2625
getCorrelationContext,
2726
setCorrelationContext,
2827
} from '../../src/correlation-context/correlation-context';
29-
3028
import {
3129
HttpCorrelationContext,
3230
CORRELATION_CONTEXT_HEADER,
@@ -43,7 +41,7 @@ describe('HttpCorrelationContext', () => {
4341
});
4442

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

117115
describe('.extract()', () => {
118116
it('should extract context of a sampled span from carrier', () => {
119-
carrier[CORRELATION_CONTEXT_HEADER] = 'key1=d4cda95b,key3=c88815a7';
117+
carrier[CORRELATION_CONTEXT_HEADER] =
118+
'key1=d4cda95b,key3=c88815a7, keyn = valn, keym =valm';
120119
const extractedCorrelationContext = getCorrelationContext(
121120
httpTraceContext.extract(Context.ROOT_CONTEXT, carrier, defaultGetter)
122121
);
123122

124123
const expected: CorrelationContext = {
125124
key1: { value: 'd4cda95b' },
126125
key3: { value: 'c88815a7' },
126+
keyn: { value: 'valn' },
127+
keym: { value: 'valm' },
127128
};
128129
assert.deepStrictEqual(extractedCorrelationContext, expected);
129130
});

0 commit comments

Comments
 (0)