diff --git a/packages/opentelemetry-core/src/correlation-context/propagation/HttpCorrelationContext.ts b/packages/opentelemetry-core/src/correlation-context/propagation/HttpCorrelationContext.ts index 6bf7c9f2111..54220266c27 100644 --- a/packages/opentelemetry-core/src/correlation-context/propagation/HttpCorrelationContext.ts +++ b/packages/opentelemetry-core/src/correlation-context/propagation/HttpCorrelationContext.ts @@ -21,7 +21,6 @@ import { HttpTextPropagator, SetterFunction, } from '@opentelemetry/api'; - import { getCorrelationContext, setCorrelationContext, @@ -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); } } @@ -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; @@ -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); } } diff --git a/packages/opentelemetry-core/test/correlation-context/HttpCorrelationContext.test.ts b/packages/opentelemetry-core/test/correlation-context/HttpCorrelationContext.test.ts index d8bd9089936..b734f3ce934 100644 --- a/packages/opentelemetry-core/test/correlation-context/HttpCorrelationContext.test.ts +++ b/packages/opentelemetry-core/test/correlation-context/HttpCorrelationContext.test.ts @@ -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. @@ -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, @@ -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' }, @@ -116,7 +114,8 @@ 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) ); @@ -124,6 +123,8 @@ describe('HttpCorrelationContext', () => { const expected: CorrelationContext = { key1: { value: 'd4cda95b' }, key3: { value: 'c88815a7' }, + keyn: { value: 'valn' }, + keym: { value: 'valm' }, }; assert.deepStrictEqual(extractedCorrelationContext, expected); });