Skip to content

Commit

Permalink
Add getActiveBaggage API (#3385)
Browse files Browse the repository at this point in the history
Co-authored-by: Valentin Marchaud <[email protected]>
Resolves #3354
  • Loading branch information
cartermp authored Nov 20, 2022
1 parent b29deee commit b9f5561
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/

### :rocket: (Enhancement)

* feat(api): add `getActiveBaggage` API [#3385](https://github.com/open-telemetry/opentelemetry-js/pull/3385)

### :bug: (Bug Fix)

* fix(sdk-metrics): use default Resource to comply with semantic conventions [#3411](https://github.com/open-telemetry/opentelemetry-js/pull/3411) @pichlermarc
Expand Down
3 changes: 3 additions & 0 deletions api/src/api/propagation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from '../propagation/TextMapPropagator';
import {
getBaggage,
getActiveBaggage,
setBaggage,
deleteBaggage,
} from '../baggage/context-helpers';
Expand Down Expand Up @@ -112,6 +113,8 @@ export class PropagationAPI {

public getBaggage = getBaggage;

public getActiveBaggage = getActiveBaggage;

public setBaggage = setBaggage;

public deleteBaggage = deleteBaggage;
Expand Down
10 changes: 10 additions & 0 deletions api/src/baggage/context-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { ContextAPI } from '../api/context';
import { createContextKey } from '../context/context';
import { Context } from '../context/types';
import { Baggage } from './types';
Expand All @@ -33,6 +34,15 @@ export function getBaggage(context: Context): Baggage | undefined {
return (context.getValue(BAGGAGE_KEY) as Baggage) || undefined;
}

/**
* Retrieve the current baggage from the active/current context
*
* @returns {Baggage} Extracted baggage from the context
*/
export function getActiveBaggage(): Baggage | undefined {
return getBaggage(ContextAPI.getInstance().active());
}

/**
* Store a baggage in the given context
*
Expand Down
15 changes: 15 additions & 0 deletions api/test/common/baggage/Baggage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import * as assert from 'assert';
import {
context,
ROOT_CONTEXT,
propagation,
baggageEntryMetadataFromString,
Expand Down Expand Up @@ -131,6 +132,20 @@ describe('Baggage', () => {

assert.strictEqual(bag, propagation.getBaggage(ctx));
});

it('should get the current baggage', () => {
const entries = {
'banana': {value: 'boats'}
};
const bag = propagation.createBaggage(entries);
const ctx = propagation.setBaggage(ROOT_CONTEXT, bag);

context.setGlobalContextManager({ active: () => ctx, disable: () => {} } as any);

assert.strictEqual(bag, propagation.getActiveBaggage());

context.disable();
});
});

describe('metadata', () => {
Expand Down

0 comments on commit b9f5561

Please sign in to comment.