Skip to content

Commit

Permalink
feat: add baggage support to the opentracing shim
Browse files Browse the repository at this point in the history
Signed-off-by: Ruben Vargas <ruben.vp8510@gmail.com>
  • Loading branch information
rubenvp8510 committed Jul 17, 2020
1 parent 0b6dc93 commit 449063c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
53 changes: 44 additions & 9 deletions packages/opentelemetry-shim-opentracing/src/shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
setActiveSpan,
} from '@opentelemetry/core';
import * as opentracing from 'opentracing';
import { defaultSetter } from '@opentelemetry/api';
import { CorrelationContext, defaultSetter } from '@opentelemetry/api';

function translateReferences(references: opentracing.Reference[]): api.Link[] {
const links: api.Link[] = [];
Expand Down Expand Up @@ -72,10 +72,15 @@ function getContextWithParent(options: opentracing.SpanOptions) {
*/
export class SpanContextShim extends opentracing.SpanContext {
private readonly _spanContext: api.SpanContext;
private _correlationContext: api.CorrelationContext;

constructor(spanContext: api.SpanContext) {
constructor(
spanContext: api.SpanContext,
correlationContext: CorrelationContext
) {
super();
this._spanContext = spanContext;
this._correlationContext = correlationContext;
}

/**
Expand All @@ -85,6 +90,13 @@ export class SpanContextShim extends opentracing.SpanContext {
return this._spanContext;
}

/**
* Returns the underlying {@link api.CorrelationContext}
*/
getCorrelationContext(): api.CorrelationContext {
return this._correlationContext;
}

/**
* Returns the trace ID as a string.
*/
Expand All @@ -98,6 +110,18 @@ export class SpanContextShim extends opentracing.SpanContext {
toSpanId(): string {
return this._spanContext.spanId;
}

getBaggageItem(key: string): string | undefined {
const entry: api.EntryValue = this._correlationContext[key];
return entry === undefined ? undefined : entry.value;
}

setBaggageItem(key: string, value: string) {
this._correlationContext = Object.assign(
{ [key]: { value } },
this._correlationContext
);
}
}

/**
Expand Down Expand Up @@ -125,11 +149,19 @@ export class TracerShim extends opentracing.Tracer {
getContextWithParent(options)
);

let correlationContext: CorrelationContext = {};
if (options.childOf) {
if (options.childOf instanceof SpanShim) {
const shimContext = options.childOf.context() as SpanContextShim;
correlationContext = shimContext.getCorrelationContext();
}
}

if (options.tags) {
span.setAttributes(options.tags);
}

return new SpanShim(this, span);
return new SpanShim(this, span, correlationContext);
}

_inject(
Expand All @@ -156,7 +188,7 @@ export class TracerShim extends opentracing.Tracer {
this._logger.warn(
'OpentracingShim.inject() does not support FORMAT_BINARY'
);
// @todo: Implement binary format
// @todo: Implement binary formats
return;
}
default:
Expand Down Expand Up @@ -200,10 +232,14 @@ export class SpanShim extends opentracing.Span {
private readonly _contextShim: SpanContextShim;
private readonly _tracerShim: TracerShim;

constructor(tracerShim: TracerShim, span: api.Span) {
constructor(
tracerShim: TracerShim,
span: api.Span,
correlationalCtx: CorrelationContext
) {
super();
this._span = span;
this._contextShim = new SpanContextShim(span.context());
this._contextShim = new SpanContextShim(span.context(), correlationalCtx);
this._tracerShim = tracerShim;
}

Expand Down Expand Up @@ -295,12 +331,11 @@ export class SpanShim extends opentracing.Span {
}

getBaggageItem(key: string): string | undefined {
// TODO: should this go into the context?
return undefined;
return this._contextShim.getBaggageItem(key);
}

setBaggageItem(key: string, value: string): this {
// TODO: should this go into the context?
this._contextShim.setBaggageItem(key, value);
return this;
}

Expand Down
15 changes: 14 additions & 1 deletion packages/opentelemetry-shim-opentracing/test/Shim.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,20 @@ describe('OpenTracing Shim', () => {

it('can set and retrieve baggage', () => {
span.setBaggageItem('baggage', 'item');
// TODO: baggage
const value = span.getBaggageItem('baggage');
assert.equal('item', value);

const childSpan = shimTracer.startSpan('child-span1', {
childOf: span,
});
childSpan.setBaggageItem('key2', 'item2');

// child should have parent baggage items.
assert.equal('item', childSpan.getBaggageItem('baggage'));
assert.equal('item2', childSpan.getBaggageItem('key2'));

// Parent shouldn't have the childr baggage item.
assert.equal(span.getBaggageItem('key2'), undefined);
});
});
});

0 comments on commit 449063c

Please sign in to comment.