-
Notifications
You must be signed in to change notification settings - Fork 825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor!: specification compliant baggage #1876
Merged
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fcbb29e
refactor!: specification compliant baggage
dyladan 63b6a6e
chore: use map in baggage
dyladan cac13ad
Merge remote-tracking branch 'origin/main' into baggage
dyladan 747262f
test: add baggage tests
dyladan cc7eb4b
Merge remote-tracking branch 'origin/main' into baggage
dyladan 9a29a53
chore: review comments
dyladan b1f53e4
chore: baggage propagation test improvements
dyladan 9821a68
chore: improve test for baggage key length
dyladan a188f9f
chore: add tests for clear and removeEntries
dyladan 9e3b3a6
Merge remote-tracking branch 'origin/main' into baggage
dyladan 56eb777
Merge remote-tracking branch 'origin/main' into baggage
dyladan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
125 changes: 125 additions & 0 deletions
125
packages/opentelemetry-api/test/baggage/Baggage.test.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,125 @@ | ||
/* | ||
* 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 * as assert from 'assert'; | ||
import { | ||
createBaggage, | ||
setBaggage, | ||
getBaggage, | ||
ROOT_CONTEXT, | ||
baggageEntryMetadataFromString, | ||
} from '../../src'; | ||
|
||
describe('Baggage', () => { | ||
describe('create', () => { | ||
it('should create an empty bag', () => { | ||
const bag = createBaggage(); | ||
|
||
assert.deepStrictEqual(bag.getAllEntries(), []); | ||
}); | ||
|
||
it('should create a bag with entries', () => { | ||
const meta = baggageEntryMetadataFromString('opaque string'); | ||
const bag = createBaggage({ | ||
key1: { value: 'value1' }, | ||
key2: { value: 'value2', metadata: meta }, | ||
}); | ||
|
||
assert.deepStrictEqual(bag.getAllEntries(), [ | ||
['key1', { value: 'value1' }], | ||
['key2', { value: 'value2', metadata: meta }], | ||
]); | ||
}); | ||
}); | ||
|
||
describe('get', () => { | ||
it('should not allow modification of returned entries', () => { | ||
const bag = createBaggage().setEntry('key', { value: 'value' }); | ||
|
||
const entry = bag.getEntry('key'); | ||
assert.ok(entry); | ||
entry.value = 'mutated'; | ||
|
||
assert.strictEqual(bag.getEntry('key')?.value, 'value'); | ||
}); | ||
}); | ||
|
||
describe('set', () => { | ||
it('should create a new bag when an entry is added', () => { | ||
const bag = createBaggage(); | ||
|
||
const bag2 = bag.setEntry('key', { value: 'value' }); | ||
|
||
assert.notStrictEqual(bag, bag2); | ||
assert.deepStrictEqual(bag.getAllEntries(), []); | ||
assert.deepStrictEqual(bag2.getAllEntries(), [ | ||
['key', { value: 'value' }], | ||
]); | ||
}); | ||
}); | ||
|
||
describe('remove', () => { | ||
it('should create a new bag when an entry is removed', () => { | ||
const bag = createBaggage({ | ||
key: { value: 'value' }, | ||
}); | ||
|
||
const bag2 = bag.removeEntry('key'); | ||
|
||
assert.deepStrictEqual(bag.getAllEntries(), [ | ||
['key', { value: 'value' }], | ||
]); | ||
|
||
assert.deepStrictEqual(bag2.getAllEntries(), []); | ||
}); | ||
}); | ||
|
||
describe('context', () => { | ||
it('should set and get a baggage from a context', () => { | ||
const bag = createBaggage(); | ||
|
||
const ctx = setBaggage(ROOT_CONTEXT, bag); | ||
|
||
assert.strictEqual(bag, getBaggage(ctx)); | ||
}); | ||
}); | ||
|
||
describe('metadata', () => { | ||
it('should create an opaque object which returns the string unchanged', () => { | ||
const meta = baggageEntryMetadataFromString('this is a string'); | ||
|
||
assert.strictEqual(meta.toString(), 'this is a string'); | ||
}); | ||
|
||
it('should return an empty string if input is invalid', () => { | ||
//@ts-expect-error | ||
const meta = baggageEntryMetadataFromString(1); | ||
|
||
assert.strictEqual(meta.toString(), ''); | ||
}); | ||
|
||
it('should retain metadata', () => { | ||
const bag = createBaggage({ | ||
key: { | ||
value: 'value', | ||
metadata: baggageEntryMetadataFromString('meta'), | ||
}, | ||
}); | ||
|
||
assert.deepStrictEqual(bag.getEntry('key')?.metadata?.toString(), 'meta'); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Imho
getAllEntries
should return map ofgetEntry
. BecausegetEntry
returnsBaggageEntry
it should returnMap<key, BaggageEntry>
not[string, BaggageEntry][]
. Or name the function for examplegetEntriesAsArray
etc. WDYT ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just thought the most common reason to use getAllEntries would be to iterate over all of them for serialization. I can change it to map if you prefer. This is especially easy since order doesn't matter in baggage.