Skip to content

Commit

Permalink
fix: add type checking in propagators (open-telemetry#904)
Browse files Browse the repository at this point in the history
  • Loading branch information
dyladan committed Mar 27, 2020
1 parent 83fca25 commit 1e643a8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/JaegerHttpTracePropagator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ export class JaegerHttpTracePropagator implements HttpTextPropagator {

extract(context: Context, carrier: unknown, getter: GetterFunction): Context {
const uberTraceIdHeader = getter(carrier, this._jaegerTraceHeader);
if (!uberTraceIdHeader) return context;
const uberTraceId = Array.isArray(uberTraceIdHeader)
? uberTraceIdHeader[0]
: uberTraceIdHeader;

if (typeof uberTraceId !== 'string') return context;

const spanContext = deserializeSpanContext(uberTraceId);
if (!spanContext) return context;

Expand Down
22 changes: 22 additions & 0 deletions test/JaegerHttpTracePropagator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,26 @@ describe('JaegerHttpTracePropagator', () => {
);
});
});

it('should fail gracefully on bad responses from getter', () => {
const ctx1 = jaegerHttpTracePropagator.extract(
Context.ROOT_CONTEXT,
carrier,
(c, k) => 1 // not a number
);
const ctx2 = jaegerHttpTracePropagator.extract(
Context.ROOT_CONTEXT,
carrier,
(c, k) => [] // empty array
);
const ctx3 = jaegerHttpTracePropagator.extract(
Context.ROOT_CONTEXT,
carrier,
(c, k) => undefined // missing value
);

assert.ok(ctx1 === Context.ROOT_CONTEXT);
assert.ok(ctx2 === Context.ROOT_CONTEXT);
assert.ok(ctx3 === Context.ROOT_CONTEXT);
});
});

0 comments on commit 1e643a8

Please sign in to comment.