You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: oteps/4333-recording-exceptions-on-logs.md
+95-85Lines changed: 95 additions & 85 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,116 +26,147 @@ This guidance applies to general-purpose instrumentations including native ones.
26
26
27
27
## Guidance
28
28
29
-
1. Exceptions should be recorded as [logs](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/exceptions/exceptions-logs.md)
30
-
or [log-based events](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/general/events.md)
29
+
This guidance boils down to the following:
31
30
32
-
This rule ensures that exception logs can be recorded independently from traces and covers cases when no span exists,
33
-
or when the corresponding span is not recorded.
31
+
- we should record full exception details including stack traces only for unhandled exceptions (by default).
32
+
- we should log error details and context when the error happens. These records don't need need to include exception stack traces unless this exception is unhandled.
33
+
- we should avoid logging the same error as it propagates up through the stack.
34
+
- we should log errors with appropriate severity ranging from `Trace` to `Fatal`.
34
35
35
-
2. An exception should be logged with appropriate severity depending on the available context.
36
+
> [!NOTE]
37
+
>
38
+
> Based on this guidance non-native instrumentations should record exceptions in top-level instrumentations only (#2 above)
36
39
37
-
- Exceptions that don't indicate any issue should be recorded with severity not higher than `Info`.
38
-
- Transient errors (even if it's the last try) should be recorded with severity not higher than `Warning`.
40
+
### Details
39
41
40
-
This rule enables typical logging mechanisms to control logs volume.
42
+
1. Exceptions should be recorded as [logs](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/exceptions/exceptions-logs.md)
43
+
or [log-based events](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/general/events.md)
41
44
42
-
3. An exception log should be recorded when the exception instance is created and thrown for the first time.
43
-
This includes new exception instances that wrap other exception(s).
45
+
2. Instrumentations for incoming requests, message processing, background job execution, or others that wrap user code and usually create local root spans, should record logs
46
+
for unhandled exceptions with `Error` severity and [`exception.escaped`](https://github.com/open-telemetry/semantic-conventions/blob/v1.29.0/docs/attributes-registry/exception.md) flag set to `true`.
44
47
45
-
This rule ensures that an exception log is recorded at least once for each exception thrown.
48
+
<!-- TODO: do we need an `exception.unhandled` attribute instead of `exception.escaped`? -->
49
+
Some runtimes and frameworks provide global exception handler that can be used to record exception logs. Priority should be given to the instrumentation point where the operation context is available.
46
50
47
-
4. An exception log should not be recorded when an exception is handled or rethrown as is, except the following cases:
48
-
- exceptions handled in global exception handlers (see #5 below)
49
-
- exceptions from code that doesn't record exception logs in a way that is compatible with OTel.
51
+
3. It's recommended to record exception stack traces only for unhandled exceptions in cases outlined in #2 above.
50
52
51
-
This rule ensures that an exception log is recorded at most once for each *handled* exception.
53
+
4. Native instrumentations should record log describing an error and the context it happened in
54
+
when this error is detected. Corresponding log record should not contain exception stack
55
+
traces (if an exception was thrown/caught) unless such exceptions usually remain unhandled.
52
56
53
-
5. Instrumentations for incoming requests, message processing, background job execution, or others that wrap user code and usually create local root spans, should record logs
54
-
for unhandled exceptions with `Error` severity and [`exception.escaped`](https://github.com/open-telemetry/semantic-conventions/blob/v1.29.0/docs/attributes-registry/exception.md) flag set to `true`.
57
+
5. An error should be logged with appropriate severity depending on the available context.
55
58
56
-
Some runtimes and frameworks provide global exception handler that can be used to record exception logs. Priority should be given to the instrumentation point where the operation context is available.
59
+
- Error that don't indicate any issue should be recorded with severity not higher than `Info`.
60
+
- Transient errors (even if it's the last try) should be recorded with severity not higher than `Warning`.
61
+
- Unhandled exceptions that don't result in application shutdown should be recorded with severity `Error`
62
+
- Errors that result in application shutdown should be recorded with severity `Fatal`
57
63
58
-
<!-- TODO: do we need an `exception.unhandled` attribute instead of `exception.escaped`? -->
64
+
6. Instrumentations should not log errors or exceptions that are handled or
65
+
are propagated as is, except ones handled in global exception handlers (see #2 below)
59
66
60
-
This allows to record unhandled exception with proper severity and distinguish them from handled ones.
67
+
If a new exception is created based on the original one or a new details about the error become available,
68
+
instrumentation may record another error (without stack trace)
61
69
62
-
6. When recording exception on logs, user applications and instrumentations are encouraged to put additional attributes
70
+
7. When recording exception on logs, user applications and instrumentations are encouraged to put additional attributes
63
71
to describe the context that the exception was thrown in.
64
72
They are also encouraged to define their own error events and enrich them with `exception.*` attributes.
65
73
66
-
### Examples
74
+
##API changes
67
75
68
-
#### Catching exception from client library call
76
+
It should not be an instrumentation library concern to decide whether exception stack trace should be recorded or not.
77
+
Library may write logs providing exception instance through a log bridge and not be aware of this guidance.
69
78
70
-
If underlying client library is already recording exceptions using OTel-compatible logger facade, the caller should not record exception event.
79
+
It also maybe desirable by some vendors/apps to record all the exception details.
80
+
81
+
OTel Logs API should provide additional methods that enrich log record with exception details such as
82
+
`addException(exception)` (`addUnhandledException`, etc), similar to [RecordException](../specification/trace/api.md?plain=1#L682)
83
+
method on span.
84
+
85
+
OTel SDK should implement such methods and set exception attributes based on configuration
86
+
and in the optimal way. This would also provide a more scalable way to evolve this guidance
87
+
and extend configuration options if necessary.
88
+
89
+
### Examples
90
+
91
+
#### Catching exception from client library in a user application
Some libraries or runtimes don't record exception logs or record them in ways that are not compatible with OpenTelemetry. In this case, it's recommended to
138
-
record exception event when the exception is rethrown.
169
+
Network level errors are part of normal life, we should consider using low severity for them
139
170
140
171
```java
141
172
publicclassConnection {
@@ -146,15 +177,12 @@ public class Connection {
146
177
try {
147
178
return socketChannel.write(content);
148
179
} catch (Throwable ex) {
149
-
// we're re-throwing the exception here, but still recording it on logs
150
-
// since the underlying platform code may or may not record exception logs depending on JRE,
151
-
// configuration, and other implementation details
0 commit comments