Skip to content

Commit

Permalink
fix(api): clean up method signature and logic flow
Browse files Browse the repository at this point in the history
This clarifies the `recordException` method signature, cleans up the
logic sequence and avoids an extra invocation of
`sanitizeAttributes(...)`.

 See [this
comment](https://github.com/open-telemetry/opentelemetry-js/pull/5333/files#r1926210496)
for context

Co-authored-by: Godfrey Chan <[email protected]>
  • Loading branch information
brianphillips and chancancode committed Jan 23, 2025
1 parent 074324b commit 66558e9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
13 changes: 9 additions & 4 deletions api/src/trace/NonRecordingSpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,13 @@ export class NonRecordingSpan implements Span {

// By default does nothing
recordException(
_exception: Exception,
_attributesOrStartTime?: SpanAttributes | TimeInput,
_time?: TimeInput
): void {}
exception: Exception,
time?: TimeInput
): void;
recordException(
exception: Exception,
attributes: SpanAttributes | undefined,
time?: TimeInput
): void;
recordException(): void {}
}
6 changes: 5 additions & 1 deletion api/src/trace/span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ export interface Span {
*/
recordException(
exception: Exception,
attributes?: SpanAttributes,
time?: TimeInput
): void;
recordException(
exception: Exception,
attributes: SpanAttributes | undefined,
time?: TimeInput
): void;
}
26 changes: 14 additions & 12 deletions packages/opentelemetry-sdk-trace-base/src/Span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,18 +318,17 @@ export class SpanImpl implements Span {
return this._ended === false;
}

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

const attributes: Attributes = {};
if (typeof exception === 'string') {
attributes[SEMATTRS_EXCEPTION_MESSAGE] = exception;
Expand All @@ -346,16 +345,19 @@ export class SpanImpl implements Span {
attributes[SEMATTRS_EXCEPTION_STACKTRACE] = exception.stack;
}
}
if (attributesOrStartTime) {
Object.assign(attributes, sanitizeAttributes(attributesOrStartTime));

if (isTimeInput(attributesOrTime)) {
time = attributesOrTime;
} else {
Object.assign(attributes, sanitizeAttributes(attributesOrTime));
}

// these are minimum requirements from spec
if (
attributes[SEMATTRS_EXCEPTION_TYPE] ||
attributes[SEMATTRS_EXCEPTION_MESSAGE]
) {
this.addEvent(ExceptionEventName, attributes, timeStamp);
this.addEvent(ExceptionEventName, attributes, time);
} else {
diag.warn(`Failed to record an exception ${exception}`);
}
Expand Down

0 comments on commit 66558e9

Please sign in to comment.