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

refactor(sdk-trace-base): Use tree-shakeable string constants for semconv #4749

Merged
Merged
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 @@ -15,6 +15,7 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/
* feat: support node 22 [#4666](https://github.com/open-telemetry/opentelemetry-js/pull/4666) @dyladan
* feat(context-zone*): support zone.js 0.12.x [#4376](https://github.com/open-telemetry/opentelemetry-js/pull/4736) @maldago
* refactor(core): Use tree-shakeable string constants for semconv [#4739](https://github.com/open-telemetry/opentelemetry-js/pull/4739) @JohannesHuster
* refactor(sdk-trace-base): Use tree-shakeable string constants for semconv [#4749](https://github.com/open-telemetry/opentelemetry-js/pull/4749) @JohannesHuster

### :bug: (Bug Fix)

Expand Down
21 changes: 12 additions & 9 deletions packages/opentelemetry-sdk-trace-base/src/Span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ import {
sanitizeAttributes,
} from '@opentelemetry/core';
import { IResource } from '@opentelemetry/resources';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
SEMATTRS_EXCEPTION_MESSAGE,
SEMATTRS_EXCEPTION_STACKTRACE,
SEMATTRS_EXCEPTION_TYPE,
} from '@opentelemetry/semantic-conventions';
import { ExceptionEventName } from './enums';
import { ReadableSpan } from './export/ReadableSpan';
import { SpanProcessor } from './SpanProcessor';
Expand Down Expand Up @@ -299,26 +303,25 @@ export class Span implements APISpan, ReadableSpan {
recordException(exception: Exception, time?: TimeInput): void {
const attributes: SpanAttributes = {};
if (typeof exception === 'string') {
attributes[SemanticAttributes.EXCEPTION_MESSAGE] = exception;
attributes[SEMATTRS_EXCEPTION_MESSAGE] = exception;
} else if (exception) {
if (exception.code) {
attributes[SemanticAttributes.EXCEPTION_TYPE] =
exception.code.toString();
attributes[SEMATTRS_EXCEPTION_TYPE] = exception.code.toString();
} else if (exception.name) {
attributes[SemanticAttributes.EXCEPTION_TYPE] = exception.name;
attributes[SEMATTRS_EXCEPTION_TYPE] = exception.name;
}
if (exception.message) {
attributes[SemanticAttributes.EXCEPTION_MESSAGE] = exception.message;
attributes[SEMATTRS_EXCEPTION_MESSAGE] = exception.message;
}
if (exception.stack) {
attributes[SemanticAttributes.EXCEPTION_STACKTRACE] = exception.stack;
attributes[SEMATTRS_EXCEPTION_STACKTRACE] = exception.stack;
}
}

// these are minimum requirements from spec
if (
attributes[SemanticAttributes.EXCEPTION_TYPE] ||
attributes[SemanticAttributes.EXCEPTION_MESSAGE]
attributes[SEMATTRS_EXCEPTION_TYPE] ||
attributes[SEMATTRS_EXCEPTION_MESSAGE]
) {
this.addEvent(ExceptionEventName, attributes, time);
} else {
Expand Down
15 changes: 9 additions & 6 deletions packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ import {
hrTimeToNanoseconds,
otperformance as performance,
} from '@opentelemetry/core';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
SEMATTRS_EXCEPTION_MESSAGE,
SEMATTRS_EXCEPTION_STACKTRACE,
SEMATTRS_EXCEPTION_TYPE,
} from '@opentelemetry/semantic-conventions';
import * as assert from 'assert';
import * as sinon from 'sinon';
import { BasicTracerProvider, Span, SpanProcessor } from '../../src';
Expand Down Expand Up @@ -1250,11 +1254,10 @@ describe('Span', () => {

assert.ok(event.attributes);

const type = event.attributes[SemanticAttributes.EXCEPTION_TYPE];
const message =
event.attributes[SemanticAttributes.EXCEPTION_MESSAGE];
const type = event.attributes[SEMATTRS_EXCEPTION_TYPE];
const message = event.attributes[SEMATTRS_EXCEPTION_MESSAGE];
const stacktrace = String(
event.attributes[SemanticAttributes.EXCEPTION_STACKTRACE]
event.attributes[SEMATTRS_EXCEPTION_STACKTRACE]
);
assert.strictEqual(type, 'Error');
assert.strictEqual(message, 'boom');
Expand Down Expand Up @@ -1294,7 +1297,7 @@ describe('Span', () => {
span.recordException({ code: 12 });
const event = span.events[0];
assert.deepStrictEqual(event.attributes, {
[SemanticAttributes.EXCEPTION_TYPE]: '12',
[SEMATTRS_EXCEPTION_TYPE]: '12',
});
});
});
Expand Down