|
| 1 | +/*! |
| 2 | + * Copyright 2019, OpenTelemetry Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { |
| 18 | + defaultGetter, |
| 19 | + defaultSetter, |
| 20 | + CorrelationContext, |
| 21 | +} from '@opentelemetry/api'; |
| 22 | +import { Context } from '@opentelemetry/context-base'; |
| 23 | +import * as assert from 'assert'; |
| 24 | + |
| 25 | +import { |
| 26 | + getCorrelationContext, |
| 27 | + setCorrelationContext, |
| 28 | +} from '../../src/correlation-context/correlation-context'; |
| 29 | + |
| 30 | +import { |
| 31 | + HttpCorrelationContext, |
| 32 | + CORRELATION_CONTEXT_HEADER, |
| 33 | + MAX_PER_NAME_VALUE_PAIRS, |
| 34 | +} from '../../src/correlation-context/propagation/HttpCorrelationContext'; |
| 35 | + |
| 36 | +describe('HttpCorrelationContext', () => { |
| 37 | + const httpTraceContext = new HttpCorrelationContext(); |
| 38 | + |
| 39 | + let carrier: { [key: string]: unknown }; |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + carrier = {}; |
| 43 | + }); |
| 44 | + |
| 45 | + describe('.inject()', () => { |
| 46 | + it('should set traceparent header', () => { |
| 47 | + const correlationContext: CorrelationContext = { |
| 48 | + key1: { value: 'd4cda95b652f4a1592b449d5929fda1b' }, |
| 49 | + key3: { value: 'c88815a7-0fa9-4d95-a1f1-cdccce3c5c2a' }, |
| 50 | + 'with/slash': { value: 'with spaces' }, |
| 51 | + }; |
| 52 | + |
| 53 | + httpTraceContext.inject( |
| 54 | + setCorrelationContext(Context.ROOT_CONTEXT, correlationContext), |
| 55 | + carrier, |
| 56 | + defaultSetter |
| 57 | + ); |
| 58 | + assert.deepStrictEqual( |
| 59 | + carrier[CORRELATION_CONTEXT_HEADER], |
| 60 | + 'key1=d4cda95b652f4a1592b449d5929fda1b,key3=c88815a7-0fa9-4d95-a1f1-cdccce3c5c2a,with%2Fslash=with%20spaces' |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should skip long key-value pairs', () => { |
| 65 | + const correlationContext: CorrelationContext = { |
| 66 | + key1: { value: 'd4cda95b' }, |
| 67 | + key3: { value: 'c88815a7' }, |
| 68 | + }; |
| 69 | + |
| 70 | + let value = ''; |
| 71 | + // Generate long value 2*MAX_PER_NAME_VALUE_PAIRS |
| 72 | + for (let i = 0; i < MAX_PER_NAME_VALUE_PAIRS; ++i) { |
| 73 | + value += '1a'; |
| 74 | + } |
| 75 | + correlationContext['longPair'] = { value }; |
| 76 | + |
| 77 | + httpTraceContext.inject( |
| 78 | + setCorrelationContext(Context.ROOT_CONTEXT, correlationContext), |
| 79 | + carrier, |
| 80 | + defaultSetter |
| 81 | + ); |
| 82 | + assert.deepStrictEqual( |
| 83 | + carrier[CORRELATION_CONTEXT_HEADER], |
| 84 | + 'key1=d4cda95b,key3=c88815a7' |
| 85 | + ); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should skip all keys that surpassed the max limit of the header', () => { |
| 89 | + const correlationContext: CorrelationContext = {}; |
| 90 | + |
| 91 | + const zeroPad = (num: number, places: number) => |
| 92 | + String(num).padStart(places, '0'); |
| 93 | + |
| 94 | + // key=value with same size , 1024 => 8 keys |
| 95 | + for (let i = 0; i < 9; ++i) { |
| 96 | + const index = zeroPad(i, 510); |
| 97 | + correlationContext[`k${index}`] = { value: `${index}` }; |
| 98 | + } |
| 99 | + |
| 100 | + // Build expected |
| 101 | + let expected = ''; |
| 102 | + for (let i = 0; i < 8; ++i) { |
| 103 | + const index = zeroPad(i, 510); |
| 104 | + expected += `k${index}=${index},`; |
| 105 | + } |
| 106 | + expected = expected.slice(0, -1); |
| 107 | + |
| 108 | + httpTraceContext.inject( |
| 109 | + setCorrelationContext(Context.ROOT_CONTEXT, correlationContext), |
| 110 | + carrier, |
| 111 | + defaultSetter |
| 112 | + ); |
| 113 | + assert.deepStrictEqual(carrier[CORRELATION_CONTEXT_HEADER], expected); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + describe('.extract()', () => { |
| 118 | + it('should extract context of a sampled span from carrier', () => { |
| 119 | + carrier[CORRELATION_CONTEXT_HEADER] = 'key1=d4cda95b,key3=c88815a7'; |
| 120 | + const extractedCorrelationContext = getCorrelationContext( |
| 121 | + httpTraceContext.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) |
| 122 | + ); |
| 123 | + |
| 124 | + const expected: CorrelationContext = { |
| 125 | + key1: { value: 'd4cda95b' }, |
| 126 | + key3: { value: 'c88815a7' }, |
| 127 | + }; |
| 128 | + assert.deepStrictEqual(extractedCorrelationContext, expected); |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + it('returns null if header is missing', () => { |
| 133 | + assert.deepStrictEqual( |
| 134 | + getCorrelationContext( |
| 135 | + httpTraceContext.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) |
| 136 | + ), |
| 137 | + undefined |
| 138 | + ); |
| 139 | + }); |
| 140 | + |
| 141 | + it('returns properties', () => { |
| 142 | + carrier[CORRELATION_CONTEXT_HEADER] = |
| 143 | + 'key1=d4cda95b,key3=c88815a7;prop1=value1'; |
| 144 | + const expected: CorrelationContext = { |
| 145 | + key1: { value: 'd4cda95b' }, |
| 146 | + key3: { value: 'c88815a7;prop1=value1' }, |
| 147 | + }; |
| 148 | + assert.deepStrictEqual( |
| 149 | + getCorrelationContext( |
| 150 | + httpTraceContext.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) |
| 151 | + ), |
| 152 | + expected |
| 153 | + ); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should gracefully handle an invalid header', () => { |
| 157 | + const testCases: Record<string, string> = { |
| 158 | + invalidNoKeyValuePair: '289371298nekjh2939299283jbk2b', |
| 159 | + invalidDoubleEqual: 'key1==value;key2=value2', |
| 160 | + invalidWrongKeyValueFormat: 'key1:value;key2=value2', |
| 161 | + invalidDoubleSemicolon: 'key1:value;;key2=value2', |
| 162 | + }; |
| 163 | + Object.getOwnPropertyNames(testCases).forEach(testCase => { |
| 164 | + carrier[CORRELATION_CONTEXT_HEADER] = testCases[testCase]; |
| 165 | + |
| 166 | + const extractedSpanContext = getCorrelationContext( |
| 167 | + httpTraceContext.extract(Context.ROOT_CONTEXT, carrier, defaultGetter) |
| 168 | + ); |
| 169 | + assert.deepStrictEqual(extractedSpanContext, undefined, testCase); |
| 170 | + }); |
| 171 | + }); |
| 172 | +}); |
0 commit comments