Skip to content

Commit

Permalink
chore: export baggage (open-telemetry#71)
Browse files Browse the repository at this point in the history
* chore: reorganize baggage folder
  • Loading branch information
dyladan authored May 20, 2021
1 parent c7a4cb1 commit ee577dc
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 70 deletions.
3 changes: 2 additions & 1 deletion api/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 api/src/baggage/Entry.ts

This file was deleted.

40 changes: 40 additions & 0 deletions api/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 api/src/baggage/Baggage.ts → api/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 api/src/baggage/index.ts → api/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 api/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

0 comments on commit ee577dc

Please sign in to comment.