Skip to content
This repository was archived by the owner on Nov 10, 2022. It is now read-only.

Commit 77b8c34

Browse files
authored
chore: export baggage (#71)
* chore: reorganize baggage folder
1 parent 8bd1c6e commit 77b8c34

File tree

7 files changed

+86
-70
lines changed

7 files changed

+86
-70
lines changed

src/api/propagation.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ import {
2828
registerGlobal,
2929
unregisterGlobal,
3030
} from '../internal/global-utils';
31-
import { getBaggage, createBaggage, setBaggage } from '../baggage/index';
31+
import { getBaggage, setBaggage } from '../baggage/context-helpers';
32+
import { createBaggage } from '../baggage/utils';
3233

3334
const API_NAME = 'propagation';
3435

src/baggage/Entry.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/baggage/context-helpers.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { createContextKey } from '../context/context';
18+
import { Context } from '../context/types';
19+
import { Baggage } from './types';
20+
21+
/**
22+
* Baggage key
23+
*/
24+
const BAGGAGE_KEY = createContextKey('OpenTelemetry Baggage Key');
25+
26+
/**
27+
* @param {Context} Context that manage all context values
28+
* @returns {Baggage} Extracted baggage from the context
29+
*/
30+
export function getBaggage(context: Context): Baggage | undefined {
31+
return (context.getValue(BAGGAGE_KEY) as Baggage) || undefined;
32+
}
33+
34+
/**
35+
* @param {Context} Context that manage all context values
36+
* @param {Baggage} baggage that will be set in the actual context
37+
*/
38+
export function setBaggage(context: Context, baggage: Baggage): Context {
39+
return context.setValue(BAGGAGE_KEY, baggage);
40+
}

src/baggage/internal/baggage.ts renamed to src/baggage/internal/baggage-impl.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import type { Baggage } from '../Baggage';
18-
import type { BaggageEntry } from '../Entry';
17+
import type { Baggage, BaggageEntry } from '../types';
1918

2019
export class BaggageImpl implements Baggage {
2120
private _entries: Map<string, BaggageEntry>;

src/baggage/Baggage.ts renamed to src/baggage/types.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,41 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { BaggageEntry } from './Entry';
17+
import { baggageEntryMetadataSymbol } from './internal/symbol';
18+
19+
/*
20+
* Copyright The OpenTelemetry Authors
21+
*
22+
* Licensed under the Apache License, Version 2.0 (the "License");
23+
* you may not use this file except in compliance with the License.
24+
* You may obtain a copy of the License at
25+
*
26+
* https://www.apache.org/licenses/LICENSE-2.0
27+
*
28+
* Unless required by applicable law or agreed to in writing, software
29+
* distributed under the License is distributed on an "AS IS" BASIS,
30+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31+
* See the License for the specific language governing permissions and
32+
* limitations under the License.
33+
*/
34+
35+
export interface BaggageEntry {
36+
/** `String` value of the `BaggageEntry`. */
37+
value: string;
38+
/**
39+
* Metadata is an optional string property defined by the W3C baggage specification.
40+
* It currently has no special meaning defined by the specification.
41+
*/
42+
metadata?: BaggageEntryMetadata;
43+
}
44+
45+
/**
46+
* Serializable Metadata defined by the W3C baggage specification.
47+
* It currently has no special meaning defined by the OpenTelemetry or W3C.
48+
*/
49+
export type BaggageEntryMetadata = { toString(): string } & {
50+
__TYPE__: typeof baggageEntryMetadataSymbol;
51+
};
1852

1953
/**
2054
* Baggage represents collection of key-value pairs with optional metadata.

src/baggage/index.ts renamed to src/baggage/utils.ts

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

17-
import { Baggage } from './Baggage';
18-
import { BaggageEntry, BaggageEntryMetadata } from './Entry';
19-
import { BaggageImpl } from './internal/baggage';
17+
import { diag } from '..';
18+
import { BaggageImpl } from './internal/baggage-impl';
2019
import { baggageEntryMetadataSymbol } from './internal/symbol';
21-
import { Context } from '../context/types';
22-
import { createContextKey } from '../context/context';
23-
24-
export * from './Baggage';
25-
export * from './Entry';
26-
27-
/**
28-
* Baggage key
29-
*/
30-
const BAGGAGE_KEY = createContextKey('OpenTelemetry Baggage Key');
20+
import { Baggage, BaggageEntry, BaggageEntryMetadata } from './types';
3121

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

43-
/**
44-
* @param {Context} Context that manage all context values
45-
* @returns {Baggage} Extracted baggage from the context
46-
*/
47-
export function getBaggage(context: Context): Baggage | undefined {
48-
return (context.getValue(BAGGAGE_KEY) as Baggage) || undefined;
49-
}
50-
51-
/**
52-
* @param {Context} Context that manage all context values
53-
* @param {Baggage} baggage that will be set in the actual context
54-
*/
55-
export function setBaggage(context: Context, baggage: Baggage): Context {
56-
return context.setValue(BAGGAGE_KEY, baggage);
57-
}
58-
5933
/**
6034
* Create a serializable BaggageEntryMetadata object from a string.
6135
*
@@ -66,7 +40,9 @@ export function baggageEntryMetadataFromString(
6640
str: string
6741
): BaggageEntryMetadata {
6842
if (typeof str !== 'string') {
69-
// @TODO log diagnostic
43+
diag.error(
44+
`Cannot create baggage metadata from unknown type: ${typeof str}`
45+
);
7046
str = '';
7147
}
7248

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
export { baggageEntryMetadataFromString } from './baggage';
17+
export * from './baggage/types';
18+
export { baggageEntryMetadataFromString } from './baggage/utils';
1819
export * from './common/Exception';
1920
export * from './common/Time';
2021
export * from './diag';

0 commit comments

Comments
 (0)