Skip to content

Commit 9f672fd

Browse files
committed
feat: add keys operation to getter (open-telemetry#1576)
* feat: add keys operation to getter * chore: lint * chore: rename getter/setter to TextMapGetter/Setter * chore: lint * chore: use setter in fetch plugin * chore: lint * chore: remove logs
1 parent 939709e commit 9f672fd

File tree

7 files changed

+92
-81
lines changed

7 files changed

+92
-81
lines changed

api/src/api/global-utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,4 @@ export function makeGetter<T>(
6565
* version. If the global API is not compatible with the API package
6666
* attempting to get it, a NOOP API implementation will be returned.
6767
*/
68-
export const API_BACKWARDS_COMPATIBILITY_VERSION = 0;
68+
export const API_BACKWARDS_COMPATIBILITY_VERSION = 1;

api/src/api/propagation.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515
*/
1616

1717
import { Context } from '@opentelemetry/context-base';
18-
import { defaultGetter, GetterFunction } from '../context/propagation/getter';
19-
import { TextMapPropagator } from '../context/propagation/TextMapPropagator';
2018
import { NOOP_TEXT_MAP_PROPAGATOR } from '../context/propagation/NoopTextMapPropagator';
21-
import { defaultSetter, SetterFunction } from '../context/propagation/setter';
19+
import {
20+
defaultTextMapGetter,
21+
defaultTextMapSetter,
22+
TextMapGetter,
23+
TextMapPropagator,
24+
TextMapSetter,
25+
} from '../context/propagation/TextMapPropagator';
2226
import { ContextAPI } from './context';
2327
import {
2428
API_BACKWARDS_COMPATIBILITY_VERSION,
@@ -74,7 +78,7 @@ export class PropagationAPI {
7478
*/
7579
public inject<Carrier>(
7680
carrier: Carrier,
77-
setter: SetterFunction<Carrier> = defaultSetter,
81+
setter: TextMapSetter<Carrier> = defaultTextMapSetter,
7882
context: Context = contextApi.active()
7983
): void {
8084
return this._getGlobalPropagator().inject(context, carrier, setter);
@@ -89,7 +93,7 @@ export class PropagationAPI {
8993
*/
9094
public extract<Carrier>(
9195
carrier: Carrier,
92-
getter: GetterFunction<Carrier> = defaultGetter,
96+
getter: TextMapGetter<Carrier> = defaultTextMapGetter,
9397
context: Context = contextApi.active()
9498
): Context {
9599
return this._getGlobalPropagator().extract(context, carrier, getter);

api/src/context/propagation/NoopTextMapPropagator.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import { TextMapPropagator } from './TextMapPropagator';
2222
*/
2323
export class NoopTextMapPropagator implements TextMapPropagator {
2424
/** Noop inject function does nothing */
25-
inject(context: Context, carrier: unknown, setter: Function): void {}
25+
inject(context: Context, carrier: unknown): void {}
2626
/** Noop extract function does nothing and returns the input context */
27-
extract(context: Context, carrier: unknown, getter: Function): Context {
27+
extract(context: Context, carrier: unknown): Context {
2828
return context;
2929
}
3030
}

api/src/context/propagation/TextMapPropagator.ts

+80-9
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616

1717
import { Context } from '@opentelemetry/context-base';
18-
import { SetterFunction } from './setter';
19-
import { GetterFunction } from './getter';
2018

2119
/**
2220
* Injects `Context` into and extracts it from carriers that travel
@@ -29,7 +27,7 @@ import { GetterFunction } from './getter';
2927
* usually implemented via library-specific request interceptors, where the
3028
* client-side injects values and the server-side extracts them.
3129
*/
32-
export interface TextMapPropagator {
30+
export interface TextMapPropagator<Carrier = any> {
3331
/**
3432
* Injects values from a given `Context` into a carrier.
3533
*
@@ -40,10 +38,14 @@ export interface TextMapPropagator {
4038
* the wire.
4139
* @param carrier the carrier of propagation fields, such as http request
4240
* 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.
4543
*/
46-
inject(context: Context, carrier: unknown, setter: SetterFunction): void;
44+
inject(
45+
context: Context,
46+
carrier: Carrier,
47+
setter: TextMapSetter<Carrier>
48+
): void;
4749

4850
/**
4951
* Given a `Context` and a carrier, extract context values from a
@@ -54,8 +56,77 @@ export interface TextMapPropagator {
5456
* the wire.
5557
* @param carrier the carrier of propagation fields, such as http request
5658
* 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.
5961
*/
60-
extract(context: Context, carrier: unknown, getter: GetterFunction): Context;
62+
extract(
63+
context: Context,
64+
carrier: Carrier,
65+
getter: TextMapGetter<Carrier>
66+
): Context;
6167
}
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+
};

api/src/context/propagation/getter.ts

-31
This file was deleted.

api/src/context/propagation/setter.ts

-31
This file was deleted.

api/src/index.ts

-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ export * from './common/Exception';
1818
export * from './common/Logger';
1919
export * from './common/Time';
2020
export * from './context/context';
21-
export * from './context/propagation/getter';
2221
export * from './context/propagation/TextMapPropagator';
2322
export * from './context/propagation/NoopTextMapPropagator';
24-
export * from './context/propagation/setter';
2523
export * from './correlation_context/CorrelationContext';
2624
export * from './correlation_context/EntryValue';
2725
export * from './metrics/BatchObserverResult';

0 commit comments

Comments
 (0)