Skip to content

Commit

Permalink
feat(shim-opentracing): map error tag to status code in addTags
Browse files Browse the repository at this point in the history
  • Loading branch information
vreynolds committed May 11, 2021
1 parent ec3a9aa commit 873d6ab
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
33 changes: 25 additions & 8 deletions packages/opentelemetry-shim-opentracing/src/shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,14 @@ export class SpanShim extends opentracing.Span {
* @param keyValueMap set of KV pairs representing tags
*/
addTags(keyValueMap: SpanAttributes): this {
this._span.setAttributes(keyValueMap);
for (const [key, value] of Object.entries(keyValueMap)) {
if (this._setSpanStatusCode(key, value)) {
continue;
}
if (value !== undefined) {
this._span.setAttribute(key, value);
}
}
return this;
}

Expand All @@ -311,12 +318,8 @@ export class SpanShim extends opentracing.Span {
* @param value value for the tag
*/
setTag(key: string, value: SpanAttributeValue): this {
if (key === opentracing.Tags.ERROR) {
const statusCode = SpanShim._mapErrorTag(value);
this._span.setStatus({ code: statusCode });
if (statusCode !== SpanStatusCode.UNSET) {
return this;
}
if (this._setSpanStatusCode(key, value)) {
return this;
}

this._span.setAttribute(key, value);
Expand All @@ -340,7 +343,21 @@ export class SpanShim extends opentracing.Span {
return this._span;
}

private static _mapErrorTag(value: SpanAttributeValue): SpanStatusCode {
private _setSpanStatusCode(
key: string,
value: SpanAttributeValue | undefined
): boolean {
if (key === opentracing.Tags.ERROR) {
const statusCode = SpanShim._mapErrorTag(value);
this._span.setStatus({ code: statusCode });
return statusCode !== SpanStatusCode.UNSET;
}
return false;
}

private static _mapErrorTag(
value: SpanAttributeValue | undefined
): SpanStatusCode {
switch (value) {
case true:
case 'true':
Expand Down
29 changes: 29 additions & 0 deletions packages/opentelemetry-shim-opentracing/test/Shim.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ describe('OpenTracing Shim', () => {
assert.strictEqual(otSpan.attributes.from, 'earth');
});

it('ignores undefined tags', () => {
span.addTags({ hello: 'stars', from: undefined });
assert.deepStrictEqual(otSpan.attributes, { hello: 'stars' });
});

it('maps error tag to status code', () => {
span.setTag('error', '');
assert.strictEqual(otSpan.status.code, SpanStatusCode.UNSET);
Expand All @@ -326,6 +331,30 @@ describe('OpenTracing Shim', () => {
assert.strictEqual(otSpan.status.code, SpanStatusCode.UNSET);
assert.strictEqual(otSpan.attributes.error, 'whoopsie');
});

it('maps error tag to status code when adding multiple tags', () => {
span.addTags({ hello: 'stars', error: '' });
assert.strictEqual(otSpan.status.code, SpanStatusCode.UNSET);

span.addTags({ hello: 'stars', error: true });
assert.strictEqual(otSpan.status.code, SpanStatusCode.ERROR);

span.addTags({ hello: 'stars', error: false });
assert.strictEqual(otSpan.status.code, SpanStatusCode.OK);

span.addTags({ hello: 'stars', error: 'true' });
assert.strictEqual(otSpan.status.code, SpanStatusCode.ERROR);

span.addTags({ hello: 'stars', error: 'false' });
assert.strictEqual(otSpan.status.code, SpanStatusCode.OK);
});

it('sets unknown error tag as attribute when adding multiple tags', () => {
span.addTags({ hello: 'stars', error: 'whoopsie' });
assert.strictEqual(otSpan.status.code, SpanStatusCode.UNSET);
assert.strictEqual(otSpan.attributes.hello, 'stars');
assert.strictEqual(otSpan.attributes.error, 'whoopsie');
});
});

it('logs KV pairs', () => {
Expand Down

0 comments on commit 873d6ab

Please sign in to comment.