Skip to content

Commit

Permalink
Add attributes argument to recordException API
Browse files Browse the repository at this point in the history
  • Loading branch information
dvoytenko committed Aug 28, 2023
1 parent 8ac369f commit 6d528aa
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
6 changes: 5 additions & 1 deletion api/src/trace/NonRecordingSpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,9 @@ export class NonRecordingSpan implements Span {
}

// By default does nothing
recordException(_exception: Exception, _time?: TimeInput): void {}
recordException(
_exception: Exception,
_attributesOrStartTime?: SpanAttributes | TimeInput,
_time?: TimeInput
): void {}
}
6 changes: 5 additions & 1 deletion api/src/trace/span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,9 @@ export interface Span {
* @param [time] the time to set as Span's event time. If not provided,
* use the current time.
*/
recordException(exception: Exception, time?: TimeInput): void;
recordException(
exception: Exception,
attributesOrStartTime?: SpanAttributes | TimeInput,
time?: TimeInput
): void;
}
17 changes: 14 additions & 3 deletions packages/opentelemetry-sdk-trace-base/src/Span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,19 @@ export class Span implements APISpan, ReadableSpan {
return this._ended === false;
}

recordException(exception: Exception, time?: TimeInput): void {
const attributes: SpanAttributes = {};
recordException(
exception: Exception,
attributesOrStartTime?: SpanAttributes | TimeInput,
timeStamp?: TimeInput
): void {
if (isTimeInput(attributesOrStartTime)) {
if (!isTimeInput(timeStamp)) {
timeStamp = attributesOrStartTime;
}
attributesOrStartTime = undefined;
}

const attributes = sanitizeAttributes(attributesOrStartTime);
if (typeof exception === 'string') {
attributes[SemanticAttributes.EXCEPTION_MESSAGE] = exception;
} else if (exception) {
Expand All @@ -296,7 +307,7 @@ export class Span implements APISpan, ReadableSpan {
attributes[SemanticAttributes.EXCEPTION_TYPE] ||
attributes[SemanticAttributes.EXCEPTION_MESSAGE]
) {
this.addEvent(ExceptionEventName, attributes, time);
this.addEvent(ExceptionEventName, attributes, timeStamp);
} else {
diag.warn(`Failed to record an exception ${exception}`);
}
Expand Down
40 changes: 40 additions & 0 deletions packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,46 @@ describe('Span', () => {
const event = span.events[0];
assert.deepStrictEqual(event.time, [0, 123]);
});

it('should record an exception with provided time as a 3rd arg', () => {
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);
span.recordException('boom', undefined, [0, 123]);
const event = span.events[0];
assert.deepStrictEqual(event.time, [0, 123]);
});
});

describe('when attributes are provided', () => {
it('should sanitized and merge attributes when provided', () => {
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);
span.recordException('boom', {
...validAttributes,
...invalidAttributes,
} as unknown as SpanAttributes);
const event = span.events[0];
assert.deepStrictEqual(event.attributes, {
[SemanticAttributes.EXCEPTION_MESSAGE]: 'boom',
...validAttributes,
});
});
});

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

0 comments on commit 6d528aa

Please sign in to comment.