Skip to content
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

chore: rename CorrelationContext to Baggage #1687

Merged
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Last updated March 2020
| Metrics | Beta | v0.3 | Beta |
| Context | Beta | v0.3 | Beta |
| Propagation | Beta | v0.3 | Beta |
| Correlation Context | Alpha | v0.3 | Development |
| Baggage | Alpha | v0.3 | Development |
| OpenTracing Bridge | N/A | v0.3 | Beta |
| Resources | N/A | v0.3 | Beta |

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
import { EntryValue } from './EntryValue';

/**
* CorrelationContext represents collection of entries. Each key of
* CorrelationContext is associated with exactly one value. CorrelationContext
* 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. CorrelationContext is used to annotate
* 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.
*/
export interface CorrelationContext {
export interface Baggage {
[entryKey: string]: EntryValue;
}
23 changes: 22 additions & 1 deletion packages/opentelemetry-api/src/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

import { NoopSpan, Span, SpanContext } from '../';
import { Context, createContextKey } from '@opentelemetry/context-base';
import { Baggage, NoopSpan, Span, SpanContext } from '../';

/**
* Active span key
Expand All @@ -32,6 +32,11 @@ const SUPPRESS_INSTRUMENTATION_KEY = createContextKey(
'OpenTelemetry Context Key SUPPRESS_INSTRUMENTATION'
);

/**
* Baggage key
*/
const BAGGAGE_KEY = createContextKey('OpenTelemetry Baggage Key');

/**
* Return the active span if one exists
*
Expand Down Expand Up @@ -107,3 +112,19 @@ export function unsuppressInstrumentation(context: Context): Context {
export function isInstrumentationSuppressed(context: Context): boolean {
return Boolean(context.getValue(SUPPRESS_INSTRUMENTATION_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);
}
4 changes: 2 additions & 2 deletions packages/opentelemetry-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export * from './common/Time';
export * from './context/context';
export * from './context/propagation/TextMapPropagator';
export * from './context/propagation/NoopTextMapPropagator';
export * from './correlation_context/CorrelationContext';
export * from './correlation_context/EntryValue';
export * from './baggage/Baggage';
export * from './baggage/EntryValue';
export * from './metrics/BatchObserverResult';
export * from './metrics/BoundInstrument';
export * from './metrics/Meter';
Expand Down
8 changes: 2 additions & 6 deletions packages/opentelemetry-api/src/metrics/NoopMeter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
BoundCounter,
BoundBaseObserver,
} from './BoundInstrument';
import { CorrelationContext } from '../correlation_context/CorrelationContext';
import { Baggage } from '../baggage/Baggage';
import { SpanContext } from '../trace/span_context';
import { ObserverResult } from './ObserverResult';

Expand Down Expand Up @@ -198,11 +198,7 @@ export class NoopBoundCounter implements BoundCounter {
}

export class NoopBoundValueRecorder implements BoundValueRecorder {
record(
_value: number,
_correlationContext?: CorrelationContext,
_spanContext?: SpanContext
): void {
record(_value: number, _baggage?: Baggage, _spanContext?: SpanContext): void {
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-api/src/trace/span_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { TraceState } from './trace_state';

/**
* A SpanContext represents the portion of a {@link Span} which must be
* serialized and propagated along side of a {@link CorrelationContext}.
* serialized and propagated along side of a {@link Baggage}.
*/
export interface SpanContext {
/**
Expand Down
10 changes: 5 additions & 5 deletions packages/opentelemetry-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ This package provides default implementations of the OpenTelemetry API for trace
- [Built-in Propagators](#built-in-propagators)
- [HttpTraceContext Propagator](#httptracecontext-propagator)
- [Composite Propagator](#composite-propagator)
- [Correlation Context Propagator](#correlation-context-propagator)
- [Baggage Propagator](#baggage-propagator)
- [Built-in Sampler](#built-in-sampler)
- [Always Sampler](#always-sampler)
- [Never Sampler](#never-sampler)
Expand Down Expand Up @@ -51,16 +51,16 @@ const { CompositePropagator } = require("@opentelemetry/core");
api.propagation.setGlobalPropagator(new CompositePropagator());
```

#### Correlation Context Propagator
#### Baggage Propagator

Provides a text-based approach to propagate [correlation context](https://w3c.github.io/correlation-context/) to remote services using the [OpenTelemetry CorrelationContext Propagation](https:/open-telemetry/opentelemetry-specification/blob/master/specification/correlationcontext/api.md#header-name) HTTP headers.
Provides a text-based approach to propagate [baggage](https://w3c.github.io/baggage/) to remote services using the [OpenTelemetry Baggage Propagation](https:/open-telemetry/opentelemetry-specification/blob/master/specification/baggage/api.md#baggage-propagation) HTTP headers.

```js
const api = require("@opentelemetry/api");
const { HttpCorrelationContext } = require("@opentelemetry/core");
const { HttpBaggage } = require("@opentelemetry/core");

/* Set Global Propagator */
api.propagation.setGlobalPropagator(new HttpCorrelationContext());
api.propagation.setGlobalPropagator(new HttpBaggage());
```

### Built-in Sampler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,21 @@
*/

import {
Baggage,
Context,
CorrelationContext,
getBaggage,
setBaggage,
TextMapGetter,
TextMapPropagator,
TextMapSetter,
} from '@opentelemetry/api';
import {
getCorrelationContext,
setCorrelationContext,
} from '../correlation-context';

const KEY_PAIR_SEPARATOR = '=';
const PROPERTIES_SEPARATOR = ';';
const ITEMS_SEPARATOR = ',';

// Name of the http header used to propagate the correlation context
export const CORRELATION_CONTEXT_HEADER = 'baggage';
// Name of the http header used to propagate the baggage
export const BAGGAGE_HEADER = 'baggage';
// Maximum number of name-value pairs allowed by w3c spec
export const MAX_NAME_VALUE_PAIRS = 180;
// Maximum number of bytes per a single name-value pair allowed by w3c spec
Expand All @@ -44,23 +42,23 @@ type KeyPair = {
};

/**
* Propagates {@link CorrelationContext} through Context format propagation.
* Propagates {@link Baggage} through Context format propagation.
*
* Based on the Correlation Context specification:
* https://w3c.github.io/correlation-context/
* Based on the Baggage specification:
* https://w3c.github.io/baggage/
*/
export class HttpCorrelationContext implements TextMapPropagator {
export class HttpBaggage implements TextMapPropagator {
inject(context: Context, carrier: unknown, setter: TextMapSetter) {
const correlationContext = getCorrelationContext(context);
if (!correlationContext) return;
const keyPairs = this._getKeyPairs(correlationContext)
const baggage = getBaggage(context);
if (!baggage) return;
const keyPairs = this._getKeyPairs(baggage)
.filter((pair: string) => {
return pair.length <= MAX_PER_NAME_VALUE_PAIRS;
})
.slice(0, MAX_NAME_VALUE_PAIRS);
const headerValue = this._serializeKeyPairs(keyPairs);
if (headerValue.length > 0) {
setter.set(carrier, CORRELATION_CONTEXT_HEADER, headerValue);
setter.set(carrier, BAGGAGE_HEADER, headerValue);
}
}

Expand All @@ -71,36 +69,31 @@ export class HttpCorrelationContext implements TextMapPropagator {
}, '');
}

private _getKeyPairs(correlationContext: CorrelationContext): string[] {
return Object.keys(correlationContext).map(
private _getKeyPairs(baggage: Baggage): string[] {
return Object.keys(baggage).map(
(key: string) =>
`${encodeURIComponent(key)}=${encodeURIComponent(
correlationContext[key].value
)}`
`${encodeURIComponent(key)}=${encodeURIComponent(baggage[key].value)}`
);
}

extract(context: Context, carrier: unknown, getter: TextMapGetter): Context {
const headerValue: string = getter.get(
carrier,
CORRELATION_CONTEXT_HEADER
) as string;
const headerValue: string = getter.get(carrier, BAGGAGE_HEADER) as string;
if (!headerValue) return context;
const correlationContext: CorrelationContext = {};
const baggage: Baggage = {};
if (headerValue.length == 0) {
return context;
}
const pairs = headerValue.split(ITEMS_SEPARATOR);
pairs.forEach(entry => {
const keyPair = this._parsePairKeyValue(entry);
if (keyPair) {
correlationContext[keyPair.key] = { value: keyPair.value };
baggage[keyPair.key] = { value: keyPair.value };
}
});
if (Object.entries(correlationContext).length === 0) {
if (Object.entries(baggage).length === 0) {
return context;
}
return setCorrelationContext(context, correlationContext);
return setBaggage(context, baggage);
}

private _parsePairKeyValue(entry: string): KeyPair | undefined {
Expand All @@ -120,6 +113,6 @@ export class HttpCorrelationContext implements TextMapPropagator {
}

fields(): string[] {
return [CORRELATION_CONTEXT_HEADER];
return [BAGGAGE_HEADER];
}
}

This file was deleted.

3 changes: 1 addition & 2 deletions packages/opentelemetry-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ export * from './version';
export * from './context/propagation/composite';
export * from './context/propagation/HttpTraceContext';
export * from './context/propagation/types';
export * from './correlation-context/correlation-context';
export * from './correlation-context/propagation/HttpCorrelationContext';
export * from './baggage/propagation/HttpBaggage';
export * from './platform';
export * from './trace/NoRecordingSpan';
export * from './trace/Plugin';
Expand Down
Loading