Skip to content

Commit

Permalink
feat: propagate correlational context on shim
Browse files Browse the repository at this point in the history
Signed-off-by: Ruben Vargas <[email protected]>
  • Loading branch information
rubenvp8510 committed Jul 17, 2020
1 parent 449063c commit 82fa8d2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
27 changes: 17 additions & 10 deletions packages/opentelemetry-shim-opentracing/src/shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ import {
getExtractedSpanContext,
NoopLogger,
setExtractedSpanContext,
setCorrelationContext,
setActiveSpan,
getCorrelationContext,
} from '@opentelemetry/core';
import * as opentracing from 'opentracing';
import { CorrelationContext, defaultSetter } from '@opentelemetry/api';
import { Context, CorrelationContext, defaultSetter } from '@opentelemetry/api';

function translateReferences(references: opentracing.Reference[]): api.Link[] {
const links: api.Link[] = [];
Expand Down Expand Up @@ -169,17 +171,19 @@ export class TracerShim extends opentracing.Tracer {
format: string,
carrier: unknown
): void {
const opentelemSpanContext: api.SpanContext = (spanContext as SpanContextShim).getSpanContext();
const oTelSpanContext: api.SpanContext = (spanContext as SpanContextShim).getSpanContext();
const oTelSpanCorrelationContext: api.CorrelationContext = (spanContext as SpanContextShim).getCorrelationContext();

if (!carrier || typeof carrier !== 'object') return;
switch (format) {
case opentracing.FORMAT_HTTP_HEADERS:
case opentracing.FORMAT_TEXT_MAP: {
api.propagation.inject(
carrier,
defaultSetter,
setExtractedSpanContext(
api.Context.ROOT_CONTEXT,
opentelemSpanContext
setCorrelationContext(
setExtractedSpanContext(api.Context.ROOT_CONTEXT, oTelSpanContext),
oTelSpanCorrelationContext
)
);
return;
Expand All @@ -199,13 +203,16 @@ export class TracerShim extends opentracing.Tracer {
switch (format) {
case opentracing.FORMAT_HTTP_HEADERS:
case opentracing.FORMAT_TEXT_MAP: {
const context = getExtractedSpanContext(
const context: Context = api.propagation.extract(carrier);
const spanContext = getExtractedSpanContext(context);
const correlationContext = getCorrelationContext(
api.propagation.extract(carrier)
);
if (!context) {

if (!spanContext) {
return null;
}
return new SpanContextShim(context);
return new SpanContextShim(spanContext, correlationContext || {});
}
case opentracing.FORMAT_BINARY: {
// @todo: Implement binary format
Expand All @@ -223,8 +230,8 @@ export class TracerShim extends opentracing.Tracer {
/**
* SpanShim wraps an {@link types.Span} and implements the OpenTracing Span API
* around it.
* @todo: Out of band baggage propagation is not currently supported.
*/
*
* */
export class SpanShim extends opentracing.Span {
// _span is the original OpenTelemetry span that we are wrapping with
// an opentracing interface.
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-shim-opentracing/test/Shim.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('OpenTracing Shim', () => {

describe('SpanContextShim', () => {
it('returns the correct context', () => {
const shim = new SpanContextShim(INVALID_SPAN_CONTEXT);
const shim = new SpanContextShim(INVALID_SPAN_CONTEXT, {});
assert.strictEqual(shim.getSpanContext(), INVALID_SPAN_CONTEXT);
assert.strictEqual(shim.toTraceId(), INVALID_SPAN_CONTEXT.traceId);
assert.strictEqual(shim.toSpanId(), INVALID_SPAN_CONTEXT.spanId);
Expand Down

0 comments on commit 82fa8d2

Please sign in to comment.