Skip to content

Commit ae1bdd1

Browse files
obecnydyladan
authored andcommitted
feat: adding possibility of recording exception (open-telemetry#1372)
* feat: adding possibility of recording exception * chore: copy * chore: copy * chore: linting * chore: reviews * chore: updating exception type * chore: reviews * chore: reviews * chore: fixing test when waiting for files to be loaded
1 parent 88514be commit ae1bdd1

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

api/src/common/Exception.ts

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
interface ExceptionWithCode {
18+
code: string;
19+
name?: string;
20+
message?: string;
21+
stack?: string;
22+
}
23+
24+
interface ExceptionWithMessage {
25+
code?: string;
26+
message: string;
27+
name?: string;
28+
stack?: string;
29+
}
30+
31+
interface ExceptionWithName {
32+
code?: string;
33+
message?: string;
34+
name: string;
35+
stack?: string;
36+
}
37+
38+
/**
39+
* Defines Exception.
40+
*
41+
* string or an object with one of (message or name or code) and optional stack
42+
*/
43+
export type Exception =
44+
| ExceptionWithCode
45+
| ExceptionWithMessage
46+
| ExceptionWithName
47+
| string;

api/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
export * from './common/Exception';
1718
export * from './common/Logger';
1819
export * from './common/Time';
1920
export * from './context/propagation/getter';

api/src/trace/NoopSpan.ts

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { Exception } from '../common/Exception';
1718
import { TimeInput } from '../common/Time';
1819
import { Attributes } from './attributes';
1920
import { Span } from './span';
@@ -76,6 +77,9 @@ export class NoopSpan implements Span {
7677
isRecording(): boolean {
7778
return false;
7879
}
80+
81+
// By default does nothing
82+
recordException(exception: Exception, time?: TimeInput): void {}
7983
}
8084

8185
export const NOOP_SPAN = new NoopSpan();

api/src/trace/span.ts

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { Exception } from '../common/Exception';
1718
import { Attributes } from './attributes';
1819
import { SpanContext } from './span_context';
1920
import { Status } from './status';
@@ -114,4 +115,12 @@ export interface Span {
114115
* with the `AddEvent` operation and attributes using `setAttributes`.
115116
*/
116117
isRecording(): boolean;
118+
119+
/**
120+
* Sets exception as a span event
121+
* @param exception the exception the only accepted values are string or Error
122+
* @param [time] the time to set as Span's event time. If not provided,
123+
* use the current time.
124+
*/
125+
recordException(exception: Exception, time?: TimeInput): void;
117126
}

0 commit comments

Comments
 (0)