Skip to content

Commit

Permalink
Merge branch 'main' into sdk-trace-node-semconv
Browse files Browse the repository at this point in the history
  • Loading branch information
pichlermarc authored Jun 3, 2024
2 parents 621134f + ba7bdf5 commit 3ac402e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/
* 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-node): Use tree-shakeable string constants for semconv [#4748](https://github.com/open-telemetry/opentelemetry-js/pull/4748) @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

0 comments on commit 3ac402e

Please sign in to comment.