Skip to content

Commit b9f5561

Browse files
cartermpvmarchaud
andauthored
Add getActiveBaggage API (#3385)
Co-authored-by: Valentin Marchaud <contact@vmarchaud.fr> Resolves #3354
1 parent b29deee commit b9f5561

4 files changed

Lines changed: 30 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/
1111

1212
### :rocket: (Enhancement)
1313

14+
* feat(api): add `getActiveBaggage` API [#3385](https://github.com/open-telemetry/opentelemetry-js/pull/3385)
15+
1416
### :bug: (Bug Fix)
1517

1618
* fix(sdk-metrics): use default Resource to comply with semantic conventions [#3411](https://github.com/open-telemetry/opentelemetry-js/pull/3411) @pichlermarc

api/src/api/propagation.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
} from '../propagation/TextMapPropagator';
3131
import {
3232
getBaggage,
33+
getActiveBaggage,
3334
setBaggage,
3435
deleteBaggage,
3536
} from '../baggage/context-helpers';
@@ -112,6 +113,8 @@ export class PropagationAPI {
112113

113114
public getBaggage = getBaggage;
114115

116+
public getActiveBaggage = getActiveBaggage;
117+
115118
public setBaggage = setBaggage;
116119

117120
public deleteBaggage = deleteBaggage;

api/src/baggage/context-helpers.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { ContextAPI } from '../api/context';
1718
import { createContextKey } from '../context/context';
1819
import { Context } from '../context/types';
1920
import { Baggage } from './types';
@@ -33,6 +34,15 @@ export function getBaggage(context: Context): Baggage | undefined {
3334
return (context.getValue(BAGGAGE_KEY) as Baggage) || undefined;
3435
}
3536

37+
/**
38+
* Retrieve the current baggage from the active/current context
39+
*
40+
* @returns {Baggage} Extracted baggage from the context
41+
*/
42+
export function getActiveBaggage(): Baggage | undefined {
43+
return getBaggage(ContextAPI.getInstance().active());
44+
}
45+
3646
/**
3747
* Store a baggage in the given context
3848
*

api/test/common/baggage/Baggage.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import * as assert from 'assert';
1818
import {
19+
context,
1920
ROOT_CONTEXT,
2021
propagation,
2122
baggageEntryMetadataFromString,
@@ -131,6 +132,20 @@ describe('Baggage', () => {
131132

132133
assert.strictEqual(bag, propagation.getBaggage(ctx));
133134
});
135+
136+
it('should get the current baggage', () => {
137+
const entries = {
138+
'banana': {value: 'boats'}
139+
};
140+
const bag = propagation.createBaggage(entries);
141+
const ctx = propagation.setBaggage(ROOT_CONTEXT, bag);
142+
143+
context.setGlobalContextManager({ active: () => ctx, disable: () => {} } as any);
144+
145+
assert.strictEqual(bag, propagation.getActiveBaggage());
146+
147+
context.disable();
148+
});
134149
});
135150

136151
describe('metadata', () => {

0 commit comments

Comments
 (0)