Skip to content

Commit

Permalink
Separate context propagation (OTEP 66) (open-telemetry#769)
Browse files Browse the repository at this point in the history
* feat: separate context propagation
  • Loading branch information
dyladan committed Feb 18, 2021
1 parent 40ea1b2 commit e50852b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 24 deletions.
3 changes: 3 additions & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
27 changes: 11 additions & 16 deletions api/src/context/propagation/HttpTextFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
12 changes: 6 additions & 6 deletions api/src/context/propagation/NoopHttpTextFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@
* limitations under the License.
*/

import { SpanContext } from '../../trace/span_context';
import { Context } from '@opentelemetry/scope-base';
import { Carrier } from './carrier';
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;
}
}

Expand Down
2 changes: 2 additions & 0 deletions api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 7 additions & 2 deletions api/test/noop-implementations/noop-tracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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);
Expand Down

0 comments on commit e50852b

Please sign in to comment.