diff --git a/api/src/api/propagation.ts b/api/src/api/propagation.ts index a554d2bef1..ab2a811831 100644 --- a/api/src/api/propagation.ts +++ b/api/src/api/propagation.ts @@ -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'; diff --git a/api/src/baggage/Entry.ts b/api/src/baggage/Entry.ts deleted file mode 100644 index d249c85cec..0000000000 --- a/api/src/baggage/Entry.ts +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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 { baggageEntryMetadataSymbol } from './internal/symbol'; - -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; -}; diff --git a/api/src/baggage/context-helpers.ts b/api/src/baggage/context-helpers.ts new file mode 100644 index 0000000000..05b24a82dd --- /dev/null +++ b/api/src/baggage/context-helpers.ts @@ -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); +} diff --git a/api/src/baggage/internal/baggage.ts b/api/src/baggage/internal/baggage-impl.ts similarity index 95% rename from api/src/baggage/internal/baggage.ts rename to api/src/baggage/internal/baggage-impl.ts index f9331f411a..8bd45dd4b9 100644 --- a/api/src/baggage/internal/baggage.ts +++ b/api/src/baggage/internal/baggage-impl.ts @@ -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; diff --git a/api/src/baggage/Baggage.ts b/api/src/baggage/types.ts similarity index 60% rename from api/src/baggage/Baggage.ts rename to api/src/baggage/types.ts index 2876f5bd47..ab6d82e651 100644 --- a/api/src/baggage/Baggage.ts +++ b/api/src/baggage/types.ts @@ -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. diff --git a/api/src/baggage/index.ts b/api/src/baggage/utils.ts similarity index 59% rename from api/src/baggage/index.ts rename to api/src/baggage/utils.ts index 03eda42083..2b378b7a22 100644 --- a/api/src/baggage/index.ts +++ b/api/src/baggage/utils.ts @@ -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 @@ -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. * @@ -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 = ''; } diff --git a/api/src/index.ts b/api/src/index.ts index 29e6e7f522..79f1270d87 100644 --- a/api/src/index.ts +++ b/api/src/index.ts @@ -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';