Skip to content
This repository was archived by the owner on Nov 17, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/api/propagation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import {
registerGlobal,
unregisterGlobal,
} from '../internal/global-utils';
import { getBaggage, createBaggage, setBaggage } from '../baggage/index';
import { getBaggage, setBaggage } from '../baggage/context-helpers';
import { createBaggage } from '../baggage/utils';

const API_NAME = 'propagation';

Expand Down
35 changes: 0 additions & 35 deletions src/baggage/Entry.ts

This file was deleted.

40 changes: 40 additions & 0 deletions src/baggage/context-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { createContextKey } from '../context/context';
import { Context } from '../context/types';
import { Baggage } from './types';

/**
* Baggage key
*/
const BAGGAGE_KEY = createContextKey('OpenTelemetry Baggage Key');

/**
* @param {Context} Context that manage all context values
* @returns {Baggage} Extracted baggage from the context
*/
export function getBaggage(context: Context): Baggage | undefined {
return (context.getValue(BAGGAGE_KEY) as Baggage) || undefined;
}

/**
* @param {Context} Context that manage all context values
* @param {Baggage} baggage that will be set in the actual context
*/
export function setBaggage(context: Context, baggage: Baggage): Context {
return context.setValue(BAGGAGE_KEY, baggage);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
* limitations under the License.
*/

import type { Baggage } from '../Baggage';
import type { BaggageEntry } from '../Entry';
import type { Baggage, BaggageEntry } from '../types';

export class BaggageImpl implements Baggage {
private _entries: Map<string, BaggageEntry>;
Expand Down
36 changes: 35 additions & 1 deletion src/baggage/Baggage.ts → src/baggage/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,41 @@
* limitations under the License.
*/

import { BaggageEntry } from './Entry';
import { baggageEntryMetadataSymbol } from './internal/symbol';

/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export interface BaggageEntry {
/** `String` value of the `BaggageEntry`. */
value: string;
/**
* Metadata is an optional string property defined by the W3C baggage specification.
* It currently has no special meaning defined by the specification.
*/
metadata?: BaggageEntryMetadata;
}

/**
* Serializable Metadata defined by the W3C baggage specification.
* It currently has no special meaning defined by the OpenTelemetry or W3C.
*/
export type BaggageEntryMetadata = { toString(): string } & {
__TYPE__: typeof baggageEntryMetadataSymbol;
};

/**
* Baggage represents collection of key-value pairs with optional metadata.
Expand Down
36 changes: 6 additions & 30 deletions src/baggage/index.ts → src/baggage/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,10 @@
* limitations under the License.
*/

import { Baggage } from './Baggage';
import { BaggageEntry, BaggageEntryMetadata } from './Entry';
import { BaggageImpl } from './internal/baggage';
import { diag } from '..';
import { BaggageImpl } from './internal/baggage-impl';
import { baggageEntryMetadataSymbol } from './internal/symbol';
import { Context } from '../context/types';
import { createContextKey } from '../context/context';

export * from './Baggage';
export * from './Entry';

/**
* Baggage key
*/
const BAGGAGE_KEY = createContextKey('OpenTelemetry Baggage Key');
import { Baggage, BaggageEntry, BaggageEntryMetadata } from './types';

/**
* Create a new Baggage with optional entries
Expand All @@ -40,22 +30,6 @@ export function createBaggage(
return new BaggageImpl(new Map(Object.entries(entries)));
}

/**
* @param {Context} Context that manage all context values
* @returns {Baggage} Extracted baggage from the context
*/
export function getBaggage(context: Context): Baggage | undefined {
return (context.getValue(BAGGAGE_KEY) as Baggage) || undefined;
}

/**
* @param {Context} Context that manage all context values
* @param {Baggage} baggage that will be set in the actual context
*/
export function setBaggage(context: Context, baggage: Baggage): Context {
return context.setValue(BAGGAGE_KEY, baggage);
}

/**
* Create a serializable BaggageEntryMetadata object from a string.
*
Expand All @@ -66,7 +40,9 @@ export function baggageEntryMetadataFromString(
str: string
): BaggageEntryMetadata {
if (typeof str !== 'string') {
// @TODO log diagnostic
diag.error(
`Cannot create baggage metadata from unknown type: ${typeof str}`
);
str = '';
}

Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
*/

export { baggageEntryMetadataFromString } from './baggage';
export * from './baggage/types';
export { baggageEntryMetadataFromString } from './baggage/utils';
export * from './common/Exception';
export * from './common/Time';
export * from './diag';
Expand Down