15
15
*/
16
16
17
17
import { Context } from '@opentelemetry/context-base' ;
18
- import { SetterFunction } from './setter' ;
19
- import { GetterFunction } from './getter' ;
20
18
21
19
/**
22
20
* Injects `Context` into and extracts it from carriers that travel
@@ -29,7 +27,7 @@ import { GetterFunction } from './getter';
29
27
* usually implemented via library-specific request interceptors, where the
30
28
* client-side injects values and the server-side extracts them.
31
29
*/
32
- export interface TextMapPropagator {
30
+ export interface TextMapPropagator < Carrier = any > {
33
31
/**
34
32
* Injects values from a given `Context` into a carrier.
35
33
*
@@ -40,10 +38,14 @@ export interface TextMapPropagator {
40
38
* the wire.
41
39
* @param carrier the carrier of propagation fields, such as http request
42
40
* headers.
43
- * @param setter a function which accepts a carrier, key, and value, which
44
- * sets the key on the carrier to the value .
41
+ * @param setter an optional { @link TextMapSetter}. If undefined, values will be
42
+ * set by direct object assignment .
45
43
*/
46
- inject ( context : Context , carrier : unknown , setter : SetterFunction ) : void ;
44
+ inject (
45
+ context : Context ,
46
+ carrier : Carrier ,
47
+ setter : TextMapSetter < Carrier >
48
+ ) : void ;
47
49
48
50
/**
49
51
* Given a `Context` and a carrier, extract context values from a
@@ -54,8 +56,77 @@ export interface TextMapPropagator {
54
56
* the wire.
55
57
* @param carrier the carrier of propagation fields, such as http request
56
58
* headers.
57
- * @param getter a function which accepts a carrier and a key, and returns
58
- * the value from the carrier identified by the key .
59
+ * @param getter an optional { @link TextMapGetter}. If undefined, keys will be all
60
+ * own properties, and keys will be accessed by direct object access .
59
61
*/
60
- extract ( context : Context , carrier : unknown , getter : GetterFunction ) : Context ;
62
+ extract (
63
+ context : Context ,
64
+ carrier : Carrier ,
65
+ getter : TextMapGetter < Carrier >
66
+ ) : Context ;
61
67
}
68
+
69
+ /**
70
+ * A setter is specified by the caller to define a specific method
71
+ * to set key/value pairs on the carrier within a propagator.
72
+ */
73
+ export interface TextMapSetter < Carrier = any > {
74
+ /**
75
+ * Callback used to set a key/value pair on an object.
76
+ *
77
+ * Should be called by the propagator each time a key/value pair
78
+ * should be set, and should set that key/value pair on the propagator.
79
+ *
80
+ * @param carrier object or class which carries key/value pairs
81
+ * @param key string key to modify
82
+ * @param value value to be set to the key on the carrier
83
+ */
84
+ set ( carrier : Carrier , key : string , value : string ) : void ;
85
+ }
86
+
87
+ /**
88
+ * A getter is specified by the caller to define a specific method
89
+ * to get the value of a key from a carrier.
90
+ */
91
+ export interface TextMapGetter < Carrier = any > {
92
+ /**
93
+ * Get a list of all keys available on the carrier.
94
+ *
95
+ * @param carrier
96
+ */
97
+ keys ( carrier : Carrier ) : string [ ] ;
98
+
99
+ /**
100
+ * Get the value of a specific key from the carrier.
101
+ *
102
+ * @param carrier
103
+ * @param key
104
+ */
105
+ get ( carrier : Carrier , key : string ) : undefined | string | string [ ] ;
106
+ }
107
+
108
+ export const defaultTextMapGetter : TextMapGetter = {
109
+ get ( carrier , key ) {
110
+ if ( carrier == null ) {
111
+ return undefined ;
112
+ }
113
+ return carrier [ key ] ;
114
+ } ,
115
+
116
+ keys ( carrier ) {
117
+ if ( carrier == null ) {
118
+ return [ ] ;
119
+ }
120
+ return Object . keys ( carrier ) ;
121
+ } ,
122
+ } ;
123
+
124
+ export const defaultTextMapSetter : TextMapSetter = {
125
+ set ( carrier , key , value ) {
126
+ if ( carrier == null ) {
127
+ return ;
128
+ }
129
+
130
+ carrier [ key ] = value ;
131
+ } ,
132
+ } ;
0 commit comments