From ac11b40ee1a2c859be1e32d8dbe1597a0e82cda4 Mon Sep 17 00:00:00 2001 From: Ruben Vargas Date: Sun, 17 May 2020 22:00:06 -0500 Subject: [PATCH] chore: remove nested conditions on correlation context extract Signed-off-by: Ruben Vargas --- .../propagation/HttpCorrelationContext.ts | 42 +++++++++---------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/packages/opentelemetry-core/src/correlation-context/propagation/HttpCorrelationContext.ts b/packages/opentelemetry-core/src/correlation-context/propagation/HttpCorrelationContext.ts index 54220266c27..917aae39c8f 100644 --- a/packages/opentelemetry-core/src/correlation-context/propagation/HttpCorrelationContext.ts +++ b/packages/opentelemetry-core/src/correlation-context/propagation/HttpCorrelationContext.ts @@ -75,30 +75,26 @@ export class HttpCorrelationContext implements HttpTextPropagator { ) as string; if (!headerValue) return context; const correlationContext: CorrelationContext = {}; - if (headerValue.length > 0) { - const pairs = headerValue.split(ITEMS_SEPARATOR); - if (pairs.length == 1) return context; - pairs.forEach(entry => { - const valueProps = entry.split(PROPERTIES_SEPARATOR); - if (valueProps.length > 0) { - const keyPairPart = valueProps.shift(); - if (keyPairPart) { - const keyPair = keyPairPart.split(KEY_PAIR_SEPARATOR); - if (keyPair.length > 1) { - const key = decodeURIComponent(keyPair[0].trim()); - let value = decodeURIComponent(keyPair[1].trim()); - if (valueProps.length > 0) { - value = - value + - PROPERTIES_SEPARATOR + - valueProps.join(PROPERTIES_SEPARATOR); - } - correlationContext[key] = { value }; - } - } - } - }); + if (headerValue.length == 0) { + return context; } + const pairs = headerValue.split(ITEMS_SEPARATOR); + if (pairs.length == 1) return context; + pairs.forEach(entry => { + const valueProps = entry.split(PROPERTIES_SEPARATOR); + if (valueProps.length <= 0) return; + const keyPairPart = valueProps.shift(); + if (!keyPairPart) return; + const keyPair = keyPairPart.split(KEY_PAIR_SEPARATOR); + if (keyPair.length <= 1) return; + const key = decodeURIComponent(keyPair[0].trim()); + let value = decodeURIComponent(keyPair[1].trim()); + if (valueProps.length > 0) { + value = + value + PROPERTIES_SEPARATOR + valueProps.join(PROPERTIES_SEPARATOR); + } + correlationContext[key] = { value }; + }); return setCorrelationContext(context, correlationContext); } }