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

feat: add composite propagator #821

Merged
45 changes: 45 additions & 0 deletions packages/opentelemetry-core/src/context/propagation/composite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*!
* Copyright 2019, OpenTelemetry Authors
dyladan marked this conversation as resolved.
Show resolved Hide resolved
*
* 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 { Carrier, Context, HttpTextFormat } from '@opentelemetry/api';

/** Combines multiple propagators into a single propagator. */
export class CompositePropagator implements HttpTextFormat {
private _propagators: HttpTextFormat[];
dyladan marked this conversation as resolved.
Show resolved Hide resolved
constructor(...propagators: HttpTextFormat[]) {
dyladan marked this conversation as resolved.
Show resolved Hide resolved
this._propagators = propagators;
}

inject(context: Context, carrier: Carrier) {
for (const propagator of this._propagators) {
try {
propagator.inject(context, carrier);
} catch { }
}
}

extract(context: Context, carrier: Carrier): Context {
return this._propagators.reduce(
(ctx, propagator) => {
try {
return propagator.extract(ctx, carrier)
dyladan marked this conversation as resolved.
Show resolved Hide resolved
} catch { }
return ctx;
},
context
);
}
}
1 change: 1 addition & 0 deletions packages/opentelemetry-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export * from './common/types';
export * from './version';
export * from './context/context';
export * from './context/propagation/B3Format';
export * from './context/propagation/composite';
export * from './context/propagation/HttpTraceContext';
export * from './platform';
export * from './trace/instrumentation/BasePlugin';
Expand Down
123 changes: 123 additions & 0 deletions packages/opentelemetry-core/test/context/composite.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*!
* Copyright 2019, 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 { Context } from '@opentelemetry/scope-base';
import * as assert from 'assert';
import { CompositePropagator, HttpTraceContext, randomSpanId, randomTraceId } from '../../src';
import { setExtractedSpanContext, getExtractedSpanContext } from '../../src/context/context';
import { B3Format, X_B3_SPAN_ID, X_B3_TRACE_ID, X_B3_SAMPLED } from '../../src/context/propagation/B3Format';
import { TRACE_PARENT_HEADER, TRACE_STATE_HEADER } from '../../src/context/propagation/HttpTraceContext';
import { TraceState } from '../../src/trace/TraceState';
import { HttpTextFormat, SpanContext } from '@opentelemetry/api';

describe('Composite Propagator', () => {
let traceId: string;
let spanId: string;

beforeEach(() => {
traceId = randomTraceId();
spanId = randomSpanId();
});

describe('inject', () => {
let carrier: { [key: string]: unknown };
let spanContext: SpanContext;
let ctxWithSpanContext: Context;

beforeEach(() => {
carrier = {};
spanContext = {
spanId,
traceId,
traceFlags: 1,
traceState: new TraceState("foo=bar")
};
ctxWithSpanContext = setExtractedSpanContext(Context.ROOT_CONTEXT, spanContext)
});

it('should inject context using all configured propagators', () => {
const composite = new CompositePropagator(new B3Format(), new HttpTraceContext());
composite.inject(ctxWithSpanContext, carrier);

assert.strictEqual(carrier[X_B3_TRACE_ID], traceId)
assert.strictEqual(carrier[X_B3_SPAN_ID], spanId)
assert.strictEqual(carrier[X_B3_SAMPLED], 1)
assert.strictEqual(carrier[TRACE_PARENT_HEADER], `00-${traceId}-${spanId}-01`)
assert.strictEqual(carrier[TRACE_STATE_HEADER], 'foo=bar')
});

it('should not throw', () => {
const composite = new CompositePropagator(new ThrowingPropagator(), new HttpTraceContext());
composite.inject(ctxWithSpanContext, carrier);

assert.strictEqual(carrier[TRACE_PARENT_HEADER], `00-${traceId}-${spanId}-01`);
});
});

describe('extract', () => {
let carrier: { [key: string]: unknown };

beforeEach(() => {
carrier = {
[X_B3_TRACE_ID]: traceId,
[X_B3_SPAN_ID]: spanId,
[X_B3_SAMPLED]: 1,
[TRACE_PARENT_HEADER]: `00-${traceId}-${spanId}-01`,
[TRACE_STATE_HEADER]: 'foo=bar',
}
});

it('should extract context using all configured propagators', () => {
const composite = new CompositePropagator(new B3Format(), new HttpTraceContext());
const spanContext = getExtractedSpanContext(composite.extract(Context.ROOT_CONTEXT, carrier));

if (!spanContext) {
throw new Error("no extracted context");
}

assert.strictEqual(spanContext.traceId, traceId)
assert.strictEqual(spanContext.spanId, spanId)
assert.strictEqual(spanContext.traceFlags, 1)
assert.strictEqual(spanContext.isRemote, true)
assert.strictEqual(spanContext.traceState!.get("foo"), "bar")
});

it('should not throw', () => {
const composite = new CompositePropagator(new ThrowingPropagator(), new HttpTraceContext());
const spanContext = getExtractedSpanContext(composite.extract(Context.ROOT_CONTEXT, carrier));

if (!spanContext) {
throw new Error("no extracted context");
}

assert.strictEqual(spanContext.traceId, traceId)
assert.strictEqual(spanContext.spanId, spanId)
assert.strictEqual(spanContext.traceFlags, 1)
assert.strictEqual(spanContext.isRemote, true)
assert.strictEqual(spanContext.traceState!.get("foo"), "bar")
});
})
});

class ThrowingPropagator implements HttpTextFormat {
inject(context: Context, carrier: unknown) {
throw new Error('this propagator throws')
}

extract(context: Context, carrier: unknown): Context {
throw new Error('This propagator throws')
}
}