Skip to content

Commit

Permalink
make sure the passed attributes override exception and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dvoytenko committed Aug 20, 2023
1 parent b337342 commit 5f25b4f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/opentelemetry-sdk-trace-base/src/Span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export class Span implements APISpan, ReadableSpan {
attributesOrStartTime = undefined;
}

const attributes = sanitizeAttributes(attributesOrStartTime);
const attributes: SpanAttributes = {};
if (typeof exception === 'string') {
attributes[SemanticAttributes.EXCEPTION_MESSAGE] = exception;
} else if (exception) {
Expand All @@ -301,6 +301,9 @@ export class Span implements APISpan, ReadableSpan {
attributes[SemanticAttributes.EXCEPTION_STACKTRACE] = exception.stack;
}
}
if (attributesOrStartTime) {
Object.assign(attributes, sanitizeAttributes(attributesOrStartTime));
}

// these are minimum requirements from spec
if (
Expand Down
33 changes: 32 additions & 1 deletion packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1233,16 +1233,47 @@ describe('Span', () => {
// @ts-expect-error writing readonly property. performance time origin is mocked to return ms value of [1,1]
span['_performanceOffset'] = 0;
assert.strictEqual(span.events.length, 0);
span.recordException('boom', {
const exception = { code: 'Error', message: 'boom', stack: 'bar' };
span.recordException(exception, {
...validAttributes,
...invalidAttributes,
} as unknown as SpanAttributes);
const event = span.events[0];
assert.deepStrictEqual(event.attributes, {
[SemanticAttributes.EXCEPTION_TYPE]: 'Error',
[SemanticAttributes.EXCEPTION_MESSAGE]: 'boom',
[SemanticAttributes.EXCEPTION_STACKTRACE]: 'bar',
...validAttributes,
});
});

it('should prioritize the provided attributes over generated', () => {
const span = new Span(
tracer,
ROOT_CONTEXT,
name,
spanContext,
SpanKind.CLIENT
);
// @ts-expect-error writing readonly property. performance time origin is mocked to return ms value of [1,1]
span['_performanceOffset'] = 0;
assert.strictEqual(span.events.length, 0);
const exception = { code: 'Error', message: 'boom', stack: 'bar' };
span.recordException(exception, {
[SemanticAttributes.EXCEPTION_TYPE]: 'OverrideError',
[SemanticAttributes.EXCEPTION_MESSAGE]: 'override-boom',
[SemanticAttributes.EXCEPTION_STACKTRACE]: 'override-bar',
...validAttributes,
...invalidAttributes,
} as unknown as SpanAttributes);
const event = span.events[0];
assert.deepStrictEqual(event.attributes, {
...validAttributes,
[SemanticAttributes.EXCEPTION_TYPE]: 'OverrideError',
[SemanticAttributes.EXCEPTION_MESSAGE]: 'override-boom',
[SemanticAttributes.EXCEPTION_STACKTRACE]: 'override-bar',
});
});
});

describe('when exception code is numeric', () => {
Expand Down

0 comments on commit 5f25b4f

Please sign in to comment.