Skip to content

Commit

Permalink
feat(shim-opentracing): add propagator configuration (open-telemetry#…
Browse files Browse the repository at this point in the history
…2153)

Co-authored-by: Valentin Marchaud <[email protected]>
  • Loading branch information
vreynolds and vmarchaud authored Apr 29, 2021
1 parent 677727b commit 7b1ed39
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 100 deletions.
14 changes: 12 additions & 2 deletions packages/opentelemetry-shim-opentracing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

OpenTracing shim allows existing OpenTracing instrumentation to report to OpenTelemetry

Note: Baggage is currently not propagated, see [issues/329](https://github.com/open-telemetry/opentelemetry-js/issues/329).

## Installation

```bash
Expand All @@ -34,6 +32,18 @@ opentracing.initGlobalTracer(new TracerShim(tracer));

```

Optionally, you can specify propagators to be used for the OpenTracing `TextMap` and `HttpHeaders` formats:

```javascript
var b3Propagator = new B3Propagator();
new TracerShim(tracer, {
textMapPropagator: b3Propagator,
httpHeadersPropagator: b3Propagator
})
```

If propagators are not specified, OpenTelemetry's global propagator will be used.

See [examples/opentracing-shim](https://github.com/open-telemetry/opentelemetry-js/tree/main/examples/opentracing-shim) for a short example.

## License
Expand Down
2 changes: 2 additions & 0 deletions packages/opentelemetry-shim-opentracing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
"devDependencies": {
"@opentelemetry/api": "^1.0.0-rc.0",
"@opentelemetry/tracing": "0.19.0",
"@opentelemetry/propagator-b3": "0.19.0",
"@opentelemetry/propagator-jaeger": "0.19.0",
"@types/mocha": "8.2.2",
"@types/node": "14.14.41",
"codecov": "3.8.1",
Expand Down
105 changes: 59 additions & 46 deletions packages/opentelemetry-shim-opentracing/src/shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
createBaggage,
SpanAttributes,
SpanAttributeValue,
TextMapPropagator,
} from '@opentelemetry/api';

function translateReferences(references: opentracing.Reference[]): api.Link[] {
Expand Down Expand Up @@ -65,7 +66,7 @@ function getContextWithParent(options: opentracing.SpanOptions) {
}

/**
* SpanContextShim wraps a {@link types.SpanContext} and implements the
* SpanContextShim wraps a {@link api.SpanContext} and implements the
* OpenTracing span context API.
*/
export class SpanContextShim extends opentracing.SpanContext {
Expand All @@ -79,7 +80,7 @@ export class SpanContextShim extends opentracing.SpanContext {
}

/**
* Returns the underlying {@link types.SpanContext}
* Returns the underlying {@link api.SpanContext}
*/
getSpanContext(): api.SpanContext {
return this._spanContext;
Expand Down Expand Up @@ -116,16 +117,18 @@ export class SpanContextShim extends opentracing.SpanContext {
}

/**
* TracerShim wraps a {@link types.Tracer} and implements the
* TracerShim wraps a {@link api.Tracer} and implements the
* OpenTracing tracer API.
*/
export class TracerShim extends opentracing.Tracer {
private readonly _tracer: api.Tracer;
private readonly _propagators: ShimPropagators | undefined;

constructor(tracer: api.Tracer) {
constructor(tracer: api.Tracer, propagators?: ShimPropagators) {
super();

this._tracer = tracer;
this._propagators = propagators;
}

startSpan(
Expand Down Expand Up @@ -163,60 +166,62 @@ export class TracerShim extends opentracing.Tracer {
const oTelSpanBaggage: api.Baggage = spanContextShim.getBaggage();

if (!carrier || typeof carrier !== 'object') return;
switch (format) {
case opentracing.FORMAT_HTTP_HEADERS:
case opentracing.FORMAT_TEXT_MAP: {
api.propagation.inject(
api.setBaggage(
api.setSpanContext(api.ROOT_CONTEXT, oTelSpanContext),
oTelSpanBaggage
),
carrier
);
return;
}
case opentracing.FORMAT_BINARY: {
api.diag.warn(
'OpentracingShim.inject() does not support FORMAT_BINARY'
);
// @todo: Implement binary formats
return;
}
default:

if (format === opentracing.FORMAT_BINARY) {
api.diag.warn('OpentracingShim.inject() does not support FORMAT_BINARY');
// @todo: Implement binary format
return;
}

const propagator = this._getPropagator(format);
if (propagator !== undefined) {
const context = api.setBaggage(
api.setSpanContext(api.ROOT_CONTEXT, oTelSpanContext),
oTelSpanBaggage
);
propagator.inject(context, carrier, api.defaultTextMapSetter);
}
}

_extract(format: string, carrier: unknown): opentracing.SpanContext | null {
switch (format) {
case opentracing.FORMAT_HTTP_HEADERS:
case opentracing.FORMAT_TEXT_MAP: {
const context: api.Context = api.propagation.extract(
api.ROOT_CONTEXT,
carrier
);
const spanContext = api.getSpanContext(context);
const baggage = api.getBaggage(context);

if (!spanContext) {
return null;
}
return new SpanContextShim(spanContext, baggage || createBaggage());
}
case opentracing.FORMAT_BINARY: {
// @todo: Implement binary format
api.diag.warn(
'OpentracingShim.extract() does not support FORMAT_BINARY'
);
if (format === opentracing.FORMAT_BINARY) {
api.diag.warn('OpentracingShim.extract() does not support FORMAT_BINARY');
// @todo: Implement binary format
return null;
}

const propagator = this._getPropagator(format);
if (propagator !== undefined) {
const context: api.Context = propagator.extract(
api.ROOT_CONTEXT,
carrier,
api.defaultTextMapGetter
);
const spanContext = api.getSpanContext(context);
const baggage = api.getBaggage(context);

if (!spanContext) {
return null;
}
default:
return new SpanContextShim(spanContext, baggage || createBaggage());
}
return null;
}

private _getPropagator(format: string): TextMapPropagator | undefined {
switch (format) {
case opentracing.FORMAT_TEXT_MAP:
return this._propagators?.textMapPropagator ?? api.propagation;
case opentracing.FORMAT_HTTP_HEADERS:
return this._propagators?.httpHeadersPropagator ?? api.propagation;
default:
return;
}
}
}

/**
* SpanShim wraps an {@link types.Span} and implements the OpenTracing Span API
* SpanShim wraps an {@link api.Span} and implements the OpenTracing Span API
* around it.
*
**/
Expand Down Expand Up @@ -334,3 +339,11 @@ export class SpanShim extends opentracing.Span {
return this._span;
}
}

/**
* Propagator configuration for the {@link TracerShim}
*/
export interface ShimPropagators {
textMapPropagator?: TextMapPropagator;
httpHeadersPropagator?: TextMapPropagator;
}
Loading

0 comments on commit 7b1ed39

Please sign in to comment.