-
Notifications
You must be signed in to change notification settings - Fork 821
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor!: specification compliant baggage (#1876)
- Loading branch information
Showing
11 changed files
with
508 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* 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 { Baggage } from './Baggage'; | ||
import { BaggageEntry, BaggageEntryMetadata } from './Entry'; | ||
import { BaggageImpl } from './internal/baggage'; | ||
import { baggageEntryMetadataSymbol } from './internal/symbol'; | ||
|
||
export * from './Baggage'; | ||
export * from './Entry'; | ||
|
||
/** | ||
* Create a new Baggage with optional entries | ||
* | ||
* @param entries An array of baggage entries the new baggage should contain | ||
*/ | ||
export function createBaggage( | ||
entries: Record<string, BaggageEntry> = {} | ||
): Baggage { | ||
return new BaggageImpl(new Map(Object.entries(entries))); | ||
} | ||
|
||
/** | ||
* Create a serializable BaggageEntryMetadata object from a string. | ||
* | ||
* @param str string metadata. Format is currently not defined by the spec and has no special meaning. | ||
* | ||
*/ | ||
export function baggageEntryMetadataFromString( | ||
str: string | ||
): BaggageEntryMetadata { | ||
if (typeof str !== 'string') { | ||
// @TODO log diagnostic | ||
str = ''; | ||
} | ||
|
||
return { | ||
__TYPE__: baggageEntryMetadataSymbol, | ||
toString() { | ||
return str; | ||
}, | ||
}; | ||
} |
63 changes: 63 additions & 0 deletions
63
packages/opentelemetry-api/src/baggage/internal/baggage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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 type { Baggage } from '../Baggage'; | ||
import type { BaggageEntry } from '../Entry'; | ||
|
||
export class BaggageImpl implements Baggage { | ||
private _entries: Map<string, BaggageEntry>; | ||
|
||
constructor(entries?: Map<string, BaggageEntry>) { | ||
this._entries = entries ? new Map(entries) : new Map(); | ||
} | ||
|
||
getEntry(key: string): BaggageEntry | undefined { | ||
const entry = this._entries.get(key); | ||
if (!entry) { | ||
return undefined; | ||
} | ||
|
||
return Object.assign({}, entry); | ||
} | ||
|
||
getAllEntries(): [string, BaggageEntry][] { | ||
return Array.from(this._entries.entries()).map(([k, v]) => [k, v]); | ||
} | ||
|
||
setEntry(key: string, entry: BaggageEntry): BaggageImpl { | ||
const newBaggage = new BaggageImpl(this._entries); | ||
newBaggage._entries.set(key, entry); | ||
return newBaggage; | ||
} | ||
|
||
removeEntry(key: string): BaggageImpl { | ||
const newBaggage = new BaggageImpl(this._entries); | ||
newBaggage._entries.delete(key); | ||
return newBaggage; | ||
} | ||
|
||
removeEntries(...keys: string[]): BaggageImpl { | ||
const newBaggage = new BaggageImpl(this._entries); | ||
for (const key of keys) { | ||
newBaggage._entries.delete(key); | ||
} | ||
return newBaggage; | ||
} | ||
|
||
clear(): BaggageImpl { | ||
return new BaggageImpl(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* Symbol used to make BaggageEntryMetadata an opaque type | ||
*/ | ||
export const baggageEntryMetadataSymbol = Symbol('BaggageEntryMetadata'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.