Skip to content

Commit 1d65b3e

Browse files
vreynoldsdyladanvmarchaud
authored
feat(shim-opentracing): update setTag based on new spec (#2194)
Co-authored-by: Daniel Dyla <[email protected]> Co-authored-by: Valentin Marchaud <[email protected]>
1 parent 8bb9752 commit 1d65b3e

File tree

2 files changed

+100
-14
lines changed

2 files changed

+100
-14
lines changed

packages/opentelemetry-shim-opentracing/src/shim.ts

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import * as opentracing from 'opentracing';
1919
import {
2020
SpanAttributes,
2121
SpanAttributeValue,
22+
SpanStatusCode,
2223
TextMapPropagator,
2324
} from '@opentelemetry/api';
2425

@@ -298,7 +299,14 @@ export class SpanShim extends opentracing.Span {
298299
* @param keyValueMap set of KV pairs representing tags
299300
*/
300301
override addTags(keyValueMap: SpanAttributes): this {
301-
this._span.setAttributes(keyValueMap);
302+
for (const [key, value] of Object.entries(keyValueMap)) {
303+
if (this._setErrorAsSpanStatusCode(key, value)) {
304+
continue;
305+
}
306+
if (value !== undefined) {
307+
this._span.setAttribute(key, value);
308+
}
309+
}
302310
return this;
303311
}
304312

@@ -309,11 +317,7 @@ export class SpanShim extends opentracing.Span {
309317
* @param value value for the tag
310318
*/
311319
override setTag(key: string, value: SpanAttributeValue): this {
312-
if (
313-
key === opentracing.Tags.ERROR &&
314-
(value === true || value === 'true')
315-
) {
316-
this._span.setStatus({ code: api.SpanStatusCode.ERROR });
320+
if (this._setErrorAsSpanStatusCode(key, value)) {
317321
return this;
318322
}
319323

@@ -330,13 +334,40 @@ export class SpanShim extends opentracing.Span {
330334
return this;
331335
}
332336

333-
/*
334-
* Returns the underlying {@link types.Span} that the shim
337+
/**
338+
* Returns the underlying {@link api.Span} that the shim
335339
* is wrapping.
336340
*/
337341
getSpan(): api.Span {
338342
return this._span;
339343
}
344+
345+
private _setErrorAsSpanStatusCode(
346+
key: string,
347+
value: SpanAttributeValue | undefined
348+
): boolean {
349+
if (key === opentracing.Tags.ERROR) {
350+
const statusCode = SpanShim._mapErrorTag(value);
351+
this._span.setStatus({ code: statusCode });
352+
return statusCode !== SpanStatusCode.UNSET;
353+
}
354+
return false;
355+
}
356+
357+
private static _mapErrorTag(
358+
value: SpanAttributeValue | undefined
359+
): SpanStatusCode {
360+
switch (value) {
361+
case true:
362+
case 'true':
363+
return SpanStatusCode.ERROR;
364+
case false:
365+
case 'false':
366+
return SpanStatusCode.OK;
367+
default:
368+
return SpanStatusCode.UNSET;
369+
}
370+
}
340371
}
341372

342373
/**

packages/opentelemetry-shim-opentracing/test/Shim.test.ts

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
INVALID_SPAN_CONTEXT,
3131
propagation,
3232
ROOT_CONTEXT,
33+
SpanStatusCode,
3334
trace,
3435
} from '@opentelemetry/api';
3536
import { performance } from 'perf_hooks';
@@ -291,13 +292,67 @@ describe('OpenTracing Shim', () => {
291292
otSpan = (span as SpanShim).getSpan() as Span;
292293
});
293294

294-
it('sets tags', () => {
295-
span.setTag('hello', 'world');
296-
assert.strictEqual(otSpan.attributes.hello, 'world');
295+
describe('tags', () => {
296+
it('sets tags', () => {
297+
span.setTag('hello', 'world');
298+
assert.strictEqual(otSpan.attributes.hello, 'world');
297299

298-
span.addTags({ hello: 'stars', from: 'earth' });
299-
assert.strictEqual(otSpan.attributes.hello, 'stars');
300-
assert.strictEqual(otSpan.attributes.from, 'earth');
300+
span.addTags({ hello: 'stars', from: 'earth' });
301+
assert.strictEqual(otSpan.attributes.hello, 'stars');
302+
assert.strictEqual(otSpan.attributes.from, 'earth');
303+
});
304+
305+
it('ignores undefined tags', () => {
306+
span.addTags({ hello: 'stars', from: undefined });
307+
assert.deepStrictEqual(otSpan.attributes, { hello: 'stars' });
308+
});
309+
310+
it('maps error tag to status code', () => {
311+
span.setTag('error', '');
312+
assert.strictEqual(otSpan.status.code, SpanStatusCode.UNSET);
313+
314+
span.setTag('error', true);
315+
assert.strictEqual(otSpan.status.code, SpanStatusCode.ERROR);
316+
317+
span.setTag('error', false);
318+
assert.strictEqual(otSpan.status.code, SpanStatusCode.OK);
319+
320+
span.setTag('error', 'true');
321+
assert.strictEqual(otSpan.status.code, SpanStatusCode.ERROR);
322+
323+
span.setTag('error', 'false');
324+
assert.strictEqual(otSpan.status.code, SpanStatusCode.OK);
325+
});
326+
327+
it('sets unknown error tag as attribute', () => {
328+
span.setTag('error', 'whoopsie');
329+
assert.strictEqual(otSpan.status.code, SpanStatusCode.UNSET);
330+
assert.strictEqual(otSpan.attributes.error, 'whoopsie');
331+
});
332+
333+
it('maps error tag to status code when adding multiple tags', () => {
334+
span.addTags({ hello: 'stars', error: '' });
335+
assert.strictEqual(otSpan.status.code, SpanStatusCode.UNSET);
336+
337+
span.addTags({ hello: 'stars', error: true });
338+
assert.strictEqual(otSpan.status.code, SpanStatusCode.ERROR);
339+
340+
span.addTags({ hello: 'stars', error: false });
341+
assert.strictEqual(otSpan.status.code, SpanStatusCode.OK);
342+
343+
span.addTags({ hello: 'stars', error: 'true' });
344+
assert.strictEqual(otSpan.status.code, SpanStatusCode.ERROR);
345+
346+
span.addTags({ hello: 'stars', error: 'false' });
347+
assert.strictEqual(otSpan.status.code, SpanStatusCode.OK);
348+
});
349+
350+
it('sets unknown error tag as attribute when adding multiple tags', () => {
351+
span.addTags({ hello: 'stars', error: 'whoopsie' });
352+
assert.strictEqual(otSpan.status.code, SpanStatusCode.UNSET);
353+
assert.strictEqual(otSpan.attributes.hello, 'stars');
354+
assert.strictEqual(otSpan.attributes.error, 'whoopsie');
355+
});
301356
});
302357

303358
it('logs KV pairs', () => {

0 commit comments

Comments
 (0)