@@ -21,7 +21,6 @@ import {
21
21
HttpTextPropagator ,
22
22
SetterFunction ,
23
23
} from '@opentelemetry/api' ;
24
-
25
24
import {
26
25
getCorrelationContext ,
27
26
setCorrelationContext ,
@@ -46,29 +45,26 @@ export const MAX_TOTAL_LENGTH = 8192;
46
45
*/
47
46
export class HttpCorrelationContext implements HttpTextPropagator {
48
47
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 ) ;
72
68
}
73
69
}
74
70
@@ -78,7 +74,7 @@ export class HttpCorrelationContext implements HttpTextPropagator {
78
74
CORRELATION_CONTEXT_HEADER
79
75
) as string ;
80
76
if ( ! headerValue ) return context ;
81
- const distributedContext : CorrelationContext = { } ;
77
+ const correlationContext : CorrelationContext = { } ;
82
78
if ( headerValue . length > 0 ) {
83
79
const pairs = headerValue . split ( ITEMS_SEPARATOR ) ;
84
80
if ( pairs . length == 1 ) return context ;
@@ -97,12 +93,12 @@ export class HttpCorrelationContext implements HttpTextPropagator {
97
93
PROPERTIES_SEPARATOR +
98
94
valueProps . join ( PROPERTIES_SEPARATOR ) ;
99
95
}
100
- distributedContext [ key ] = { value } ;
96
+ correlationContext [ key ] = { value } ;
101
97
}
102
98
}
103
99
}
104
100
} ) ;
105
101
}
106
- return setCorrelationContext ( context , distributedContext ) ;
102
+ return setCorrelationContext ( context , correlationContext ) ;
107
103
}
108
104
}
0 commit comments