Skip to content

Commit

Permalink
refactor!: specification compliant baggage (#1876)
Browse files Browse the repository at this point in the history
  • Loading branch information
dyladan authored Feb 5, 2021
1 parent 7e423a9 commit 83343ef
Show file tree
Hide file tree
Showing 11 changed files with 508 additions and 94 deletions.
50 changes: 42 additions & 8 deletions packages/opentelemetry-api/src/baggage/Baggage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,50 @@
* limitations under the License.
*/

import { BaggageEntryValue } from './EntryValue';
import { BaggageEntry } from './Entry';

/**
* Baggage represents collection of entries. Each key of
* Baggage is associated with exactly one value. Baggage
* is serializable, to facilitate propagating it not only inside the process
* but also across process boundaries. Baggage is used to annotate
* telemetry with the name:value pair Entry. Those values can be used to add
* dimension to the metric or additional contest properties to logs and traces.
* Baggage represents collection of key-value pairs with optional metadata.
* Each key of Baggage is associated with exactly one value.
* Baggage may be used to annotate and enrich telemetry data.
*/
export interface Baggage {
[entryKey: string]: BaggageEntryValue;
/**
* Get an entry from Baggage if it exists
*
* @param key The key which identifies the BaggageEntry
*/
getEntry(key: string): BaggageEntry | undefined;

/**
* Get a list of all entries in the Baggage
*/
getAllEntries(): [string, BaggageEntry][];

/**
* Returns a new baggage with the entries from the current bag and the specified entry
*
* @param key string which identifies the baggage entry
* @param entry BaggageEntry for the given key
*/
setEntry(key: string, entry: BaggageEntry): Baggage;

/**
* Returns a new baggage with the entries from the current bag except the removed entry
*
* @param key key identifying the entry to be removed
*/
removeEntry(key: string): Baggage;

/**
* Returns a new baggage with the entries from the current bag except the removed entries
*
* @param key keys identifying the entries to be removed
*/
removeEntries(...key: string[]): Baggage;

/**
* Returns a new baggage with no entries
*/
clear(): Baggage;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,23 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export interface BaggageEntryValue {
/** `String` value of the `BaggageEntryValue`. */

import { baggageEntryMetadataSymbol } from './internal/symbol';

export interface BaggageEntry {
/** `String` value of the `BaggageEntry`. */
value: string;
/**
* ttl is an integer that represents number of hops an entry can
* propagate.
* Metadata is an optional string property defined by the W3C baggage specification.
* It currently has no special meaning defined by the specification.
*/
ttl?: BaggageEntryTtl;
metadata?: BaggageEntryMetadata;
}

/**
* BaggageEntryTtl is an integer that represents number of hops an entry can propagate.
*
* For now, ONLY special values (0 and -1) are supported.
* Serializable Metadata defined by the W3C baggage specification.
* It currently has no special meaning defined by the OpenTelemetry or W3C.
*/
export enum BaggageEntryTtl {
/**
* NO_PROPAGATION is considered to have local context and is used within the
* process it created.
*/
NO_PROPAGATION = 0,

/** UNLIMITED_PROPAGATION can propagate unlimited hops. */
UNLIMITED_PROPAGATION = -1,
}
export type BaggageEntryMetadata = { toString(): string } & {
__TYPE__: typeof baggageEntryMetadataSymbol;
};
56 changes: 56 additions & 0 deletions packages/opentelemetry-api/src/baggage/index.ts
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 packages/opentelemetry-api/src/baggage/internal/baggage.ts
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();
}
}
20 changes: 20 additions & 0 deletions packages/opentelemetry-api/src/baggage/internal/symbol.ts
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');
3 changes: 1 addition & 2 deletions packages/opentelemetry-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ export * from './common/Time';
export * from './context/context';
export * from './context/propagation/TextMapPropagator';
export * from './context/propagation/NoopTextMapPropagator';
export * from './baggage/Baggage';
export * from './baggage/EntryValue';
export * from './baggage';
export * from './trace/attributes';
export * from './trace/Event';
export * from './trace/link_context';
Expand Down
Loading

0 comments on commit 83343ef

Please sign in to comment.