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: add fields operation to TextMapPropagator #1615

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export class NoopTextMapPropagator implements TextMapPropagator {
extract(context: Context, carrier: unknown): Context {
return context;
}
fields(): string[] {
return [];
}
}

export const NOOP_TEXT_MAP_PROPAGATOR = new NoopTextMapPropagator();
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ export interface TextMapPropagator<Carrier = any> {
carrier: Carrier,
getter: TextMapGetter<Carrier>
): Context;

/**
* Return a list of all fields which may be used by the propagator.
*
* This list should be used to clear fields before calling inject if a carrier is
* used more than once.
*/
fields(): string[];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,14 @@ export class B3MultiPropagator implements TextMapPropagator {
}
return context;
}

fields(): string[] {
return [
X_B3_TRACE_ID,
X_B3_SPAN_ID,
X_B3_FLAGS,
X_B3_SAMPLED,
X_B3_PARENT_SPAN_ID,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,15 @@ export class B3Propagator implements TextMapPropagator {
carrier: unknown,
setter: TextMapSetter
) => void;
public readonly _fields: string[];

constructor(config: B3PropagatorConfig = {}) {
if (config.injectEncoding === B3InjectEncoding.MULTI_HEADER) {
this._inject = this._b3MultiPropagator.inject;
this._fields = this._b3MultiPropagator.fields();
} else {
this._inject = this._b3SinglePropagator.inject;
this._fields = this._b3SinglePropagator.fields();
}
}

Expand All @@ -69,4 +72,8 @@ export class B3Propagator implements TextMapPropagator {
return this._b3MultiPropagator.extract(context, carrier, getter);
}
}

fields(): string[] {
return this._fields;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,8 @@ export class B3SinglePropagator implements TextMapPropagator {
traceFlags,
});
}

fields(): string[] {
return [B3_CONTEXT_HEADER];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,8 @@ export class HttpTraceContext implements TextMapPropagator {
}
return setExtractedSpanContext(context, spanContext);
}

fields(): string[] {
return [TRACE_PARENT_HEADER, TRACE_STATE_HEADER];
}
}
14 changes: 14 additions & 0 deletions packages/opentelemetry-core/src/context/propagation/composite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { CompositePropagatorConfig } from './types';
export class CompositePropagator implements TextMapPropagator {
private readonly _propagators: TextMapPropagator[];
private readonly _logger: Logger;
private readonly _fields: string[];

/**
* Construct a composite propagator from a list of propagators.
Expand All @@ -37,6 +38,14 @@ export class CompositePropagator implements TextMapPropagator {
constructor(config: CompositePropagatorConfig = {}) {
this._propagators = config.propagators ?? [];
this._logger = config.logger ?? new NoopLogger();
this._fields = Array.from(
new Set(
this._propagators
// older propagators may not have fields function, null check to be sure
.map(p => p?.fields() ?? [])
.reduce((x, y) => x.concat(y))
)
);
}

/**
Expand Down Expand Up @@ -81,4 +90,9 @@ export class CompositePropagator implements TextMapPropagator {
return ctx;
}, context);
}

fields(): string[] {
// return a new array so our fields cannot be modified
return this._fields.slice();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,8 @@ export class HttpCorrelationContext implements TextMapPropagator {
}
return { key, value };
}

fields(): string[] {
return [CORRELATION_CONTEXT_HEADER];
}
}
4 changes: 4 additions & 0 deletions packages/opentelemetry-core/test/context/composite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,8 @@ class ThrowingPropagator implements TextMapPropagator {
extract(context: Context, carrier: unknown): Context {
throw new Error('This propagator throws');
}

fields(): string[] {
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@ export class DummyPropagation implements TextMapPropagator {
headers[DummyPropagation.TRACE_CONTEXT_KEY] = spanContext.traceId;
headers[DummyPropagation.SPAN_CONTEXT_KEY] = spanContext.spanId;
}
fields(): string[] {
return [
DummyPropagation.TRACE_CONTEXT_KEY,
DummyPropagation.SPAN_CONTEXT_KEY,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@ export class DummyPropagation implements TextMapPropagator {
headers[DummyPropagation.TRACE_CONTEXT_KEY] = spanContext.traceId;
headers[DummyPropagation.SPAN_CONTEXT_KEY] = spanContext.spanId;
}
fields(): string[] {
return [
DummyPropagation.TRACE_CONTEXT_KEY,
DummyPropagation.SPAN_CONTEXT_KEY,
];
}
}