From e50852b4839c37168c6abd05973b4692bc16d695 Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Tue, 18 Feb 2020 15:45:58 -0500 Subject: [PATCH] Separate context propagation (OTEP 66) (#769) * feat: separate context propagation --- api/package.json | 3 +++ api/src/context/propagation/HttpTextFormat.ts | 27 ++++++++----------- .../context/propagation/NoopHttpTextFormat.ts | 12 ++++----- api/src/index.ts | 2 ++ .../noop-implementations/noop-tracer.test.ts | 9 +++++-- 5 files changed, 29 insertions(+), 24 deletions(-) diff --git a/api/package.json b/api/package.json index d05e4f54fc..369e56079e 100644 --- a/api/package.json +++ b/api/package.json @@ -45,6 +45,9 @@ "publishConfig": { "access": "public" }, + "dependencies": { + "@opentelemetry/scope-base": "^0.4.0" + }, "devDependencies": { "@types/mocha": "^5.2.7", "@types/node": "^12.6.8", diff --git a/api/src/context/propagation/HttpTextFormat.ts b/api/src/context/propagation/HttpTextFormat.ts index 273d545220..fef9293ecf 100644 --- a/api/src/context/propagation/HttpTextFormat.ts +++ b/api/src/context/propagation/HttpTextFormat.ts @@ -14,40 +14,35 @@ * limitations under the License. */ -import { SpanContext } from '../../trace/span_context'; +import { Context } from '@opentelemetry/scope-base'; import { Carrier } from './carrier'; /** - * Injects and extracts a value as text into carriers that travel in-band - * across process boundaries. Encoding is expected to conform to the HTTP + * Injects {@link Context} into and extracts it from carriers that travel + * in-band across process boundaries. Encoding is expected to conform to the HTTP * Header Field semantics. Values are often encoded as RPC/HTTP request headers. * * The carrier of propagated data on both the client (injector) and server - * (extractor) side is usually an http request. Propagation is usually - * implemented via library- specific request interceptors, where the - * client-side injects values and the server-side extracts them. + * (extractor) side is usually an object such as http headers. */ export interface HttpTextFormat { /** - * Injects the given {@link SpanContext} instance to transmit over the wire. + * Injects values from a given {@link Context} into a carrier. * * OpenTelemetry defines a common set of format values (BinaryFormat and * HTTPTextFormat), and each has an expected `carrier` type. * - * @param spanContext the SpanContext to transmit over the wire. - * @param format the format of the carrier. + * @param context the Context from which to extract values to transmit over the wire. * @param carrier the carrier of propagation fields, such as http request headers. */ - inject(spanContext: SpanContext, format: string, carrier: Carrier): void; + inject(context: Context, carrier: Carrier): void; /** - * Returns a {@link SpanContext} instance extracted from `carrier` in the - * given format from upstream. + * Given a {@link Context} and a carrier, extract context values from a carrier and + * return a new context, created from the old context, with the extracted values. * - * @param format the format of the carrier. + * @param context the Context from which to extract values to transmit over the wire. * @param carrier the carrier of propagation fields, such as http request headers. - * @returns SpanContext The extracted SpanContext, or null if no such - * SpanContext could be found in carrier. */ - extract(format: string, carrier: Carrier): SpanContext | null; + extract(context: Context, carrier: Carrier): Context; } diff --git a/api/src/context/propagation/NoopHttpTextFormat.ts b/api/src/context/propagation/NoopHttpTextFormat.ts index fb7df57604..1f38729eb5 100644 --- a/api/src/context/propagation/NoopHttpTextFormat.ts +++ b/api/src/context/propagation/NoopHttpTextFormat.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { SpanContext } from '../../trace/span_context'; +import { Context } from '@opentelemetry/scope-base'; import { Carrier } from './carrier'; import { HttpTextFormat } from './HttpTextFormat'; @@ -22,11 +22,11 @@ import { HttpTextFormat } from './HttpTextFormat'; * No-op implementations of {@link HttpTextFormat}. */ export class NoopHttpTextFormat implements HttpTextFormat { - // By default does nothing - inject(spanContext: SpanContext, format: string, carrier: Carrier): void {} - // By default does nothing - extract(format: string, carrier: Carrier): SpanContext | null { - return null; + /** Noop inject function does nothing */ + inject(context: Context, carrier: Carrier): void {} + /** Noop extract function does nothing and returns the input context */ + extract(context: Context, carrier: Carrier): Context { + return context; } } diff --git a/api/src/index.ts b/api/src/index.ts index a453987499..4fcd63f6f7 100644 --- a/api/src/index.ts +++ b/api/src/index.ts @@ -46,6 +46,8 @@ export * from './trace/NoopTracerProvider'; export * from './metrics/NoopMeterProvider'; export * from './metrics/NoopMeter'; +export { Context } from '@opentelemetry/scope-base'; + import { TraceAPI } from './api/trace'; /** Entrypoint for trace API */ export const trace = TraceAPI.getInstance(); diff --git a/api/test/noop-implementations/noop-tracer.test.ts b/api/test/noop-implementations/noop-tracer.test.ts index 32dd6813ed..ed017dda99 100644 --- a/api/test/noop-implementations/noop-tracer.test.ts +++ b/api/test/noop-implementations/noop-tracer.test.ts @@ -16,6 +16,7 @@ import * as assert from 'assert'; import { NoopTracer, NOOP_SPAN, SpanKind } from '../../src'; +import { Context } from '@opentelemetry/scope-base'; describe('NoopTracer', () => { it('should not crash', () => { @@ -38,8 +39,12 @@ describe('NoopTracer', () => { assert.deepStrictEqual(tracer.getCurrentSpan(), NOOP_SPAN); const httpTextFormat = tracer.getHttpTextFormat(); assert.ok(httpTextFormat); - httpTextFormat.inject(spanContext, 'HttpTextFormat', {}); - assert.deepStrictEqual(httpTextFormat.extract('HttpTextFormat', {}), null); + + httpTextFormat.inject(Context.ROOT_CONTEXT, {}); + assert.deepStrictEqual( + httpTextFormat.extract(Context.ROOT_CONTEXT, {}), + Context.ROOT_CONTEXT + ); const binaryFormat = tracer.getBinaryFormat(); assert.ok(binaryFormat);