Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): add attributes argument to recordException API #5333

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ For semantic convention package changes, see the [semconv CHANGELOG](packages/se

### :rocket: (Enhancement)

* feat(api): add attributes argument to recordException API [#4071](https://github.com/open-telemetry/opentelemetry-js/pull/4071)
* feat(sdk-metrics): implement MetricProducer specification [#4007](https://github.com/open-telemetry/opentelemetry-js/pull/4007)
* feat: update PeriodicExportingMetricReader and PrometheusExporter to accept optional metric producers [#4077](https://github.com/open-telemetry/opentelemetry-js/pull/4077) @aabmass

Expand Down
2 changes: 2 additions & 0 deletions api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ All notable changes to this project will be documented in this file.

### :rocket: (Enhancement)

* feat(api): add attributes argument to recordException API [#5333](https://github.com/open-telemetry/opentelemetry-js/pull/5333) @brianphillips

### :bug: (Bug Fix)

### :books: (Refine Doc)
Expand Down
11 changes: 10 additions & 1 deletion api/src/trace/NonRecordingSpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,14 @@ export class NonRecordingSpan implements Span {
}

// By default does nothing
recordException(_exception: Exception, _time?: TimeInput): void {}
recordException(
exception: Exception,
time?: TimeInput
): void;
recordException(
exception: Exception,
attributes: SpanAttributes | undefined,
time?: TimeInput
): void;
recordException(): void {}
}
17 changes: 17 additions & 0 deletions api/src/trace/span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,21 @@ export interface Span {
* use the current time.
*/
recordException(exception: Exception, time?: TimeInput): void;

/**
* Sets exception as a span event
* @param exception the exception the only accepted values are string or Error
* @param [attributes] the attributes that will be added to the error event.
* @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,
attributes: SpanAttributes | undefined,
time?: TimeInput
): void;
}
18 changes: 17 additions & 1 deletion packages/opentelemetry-sdk-trace-base/src/Span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,17 @@ export class SpanImpl implements Span {
return this._ended === false;
}

brianphillips marked this conversation as resolved.
Show resolved Hide resolved
recordException(exception: Exception, time?: TimeInput): void {
recordException(exception: Exception, time?: TimeInput): void;
recordException(
exception: Exception,
attributes: Attributes | undefined,
time?: TimeInput
): void;
recordException(
exception: Exception,
attributesOrTime?: Attributes | TimeInput,
time?: TimeInput
): void {
const attributes: Attributes = {};
if (typeof exception === 'string') {
attributes[SEMATTRS_EXCEPTION_MESSAGE] = exception;
Expand All @@ -336,6 +346,12 @@ export class SpanImpl implements Span {
}
}

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

// these are minimum requirements from spec
if (
attributes[SEMATTRS_EXCEPTION_TYPE] ||
Expand Down
80 changes: 80 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 @@ -1463,6 +1463,86 @@ 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 SpanImpl({
scope: tracer.instrumentationScope,
resource: tracer['_resource'],
context: ROOT_CONTEXT,
name,
spanContext,
kind: SpanKind.CLIENT,
spanLimits: tracer.getSpanLimits(),
spanProcessor: tracer['_spanProcessor'],
});
// @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 SpanImpl({
scope: tracer.instrumentationScope,
resource: tracer['_resource'],
context: ROOT_CONTEXT,
name,
spanContext,
kind: SpanKind.CLIENT,
spanLimits: tracer.getSpanLimits(),
spanProcessor: tracer['_spanProcessor'],
});
// @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, {
...validAttributes,
...invalidAttributes,
} as unknown as Attributes);
const event = span.events[0];
assert.deepStrictEqual(event.attributes, {
[SEMATTRS_EXCEPTION_TYPE]: 'Error',
[SEMATTRS_EXCEPTION_MESSAGE]: 'boom',
[SEMATTRS_EXCEPTION_STACKTRACE]: 'bar',
...validAttributes,
});
});

it('should prioritize the provided attributes over generated', () => {
const span = new SpanImpl({
scope: tracer.instrumentationScope,
resource: tracer['_resource'],
context: ROOT_CONTEXT,
name,
spanContext,
kind: SpanKind.CLIENT,
spanLimits: tracer.getSpanLimits(),
spanProcessor: tracer['_spanProcessor'],
});
// @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, {
[SEMATTRS_EXCEPTION_TYPE]: 'OverrideError',
[SEMATTRS_EXCEPTION_MESSAGE]: 'override-boom',
[SEMATTRS_EXCEPTION_STACKTRACE]: 'override-bar',
...validAttributes,
...invalidAttributes,
} as unknown as Attributes);
const event = span.events[0];
assert.deepStrictEqual(event.attributes, {
...validAttributes,
[SEMATTRS_EXCEPTION_TYPE]: 'OverrideError',
[SEMATTRS_EXCEPTION_MESSAGE]: 'override-boom',
[SEMATTRS_EXCEPTION_STACKTRACE]: 'override-bar',
});
});
});

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