diff --git a/CHANGELOG.md b/CHANGELOG.md index 189b8492bea..66a35f7c539 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,10 +23,12 @@ New: ([#606](https://github.com/open-telemetry/opentelemetry-specification/pull/606/)) - Clarification of the behavior of the Trace API, re: context propagation, in the absence of an installed SDK -- Add Span API and semantic conventions for recording exceptions +- Add API and semantic conventions for recording exceptions as Span Events ([#697](https://github.com/open-telemetry/opentelemetry-specification/pull/697)) * API was extended to allow adding arbitrary event attributes ([#874](https://github.com/open-telemetry/opentelemetry-specification/pull/874)) - * `exception.escaped` was added ([#784](https://github.com/open-telemetry/opentelemetry-specification/pull/784)) + * `exception.escaped` semantic span event attribute was added + ([#784](https://github.com/open-telemetry/opentelemetry-specification/pull/784), + [#946](https://github.com/open-telemetry/opentelemetry-specification/pull/946)) Updates: diff --git a/semantic_conventions/trace/exception.yaml b/semantic_conventions/trace/exception.yaml index 612eaa04026..beaafdb93b1 100644 --- a/semantic_conventions/trace/exception.yaml +++ b/semantic_conventions/trace/exception.yaml @@ -29,11 +29,25 @@ groups: type: boolean brief: > SHOULD be set to true if the exception event is recorded at a point where - it is known that the exception is escaping the scope of the span (e.g. if - there is an exception active just before ending the span). - note: > - Note that an exception may still leave the scope of the span even if this - was not set or set to false, if the event was recorded at an earlier time. + it is known that the exception is escaping the scope of the span. + note: |- + An exception is considered to have escaped (or left) the scope of a span, + if that span is ended while the exception is still logically "in flight". + This may be actually "in flight" in some languages (e.g. if the exception + is passed to a Context manager's `__exit__` method in Python) but will + usually be caught at the point of recording the exception in most languages. + + It is usually not possible to determine at the point where an exception is thrown + whether it will escape the scope of a span. + However, it is trivial to know that an exception + will escape, if one checks for an active exception just before ending the span, + as done in the [example above](#exception-end-example). + + It follows that an exception may still escape the scope of the span + even if the `exception.escaped` attribute was not set or set to false, + since the event might have been recorded at a time where it was not + clear whether the exception will escape. + constraints: - any_of: - "exception.type" diff --git a/specification/trace/semantic_conventions/exceptions.md b/specification/trace/semantic_conventions/exceptions.md index 7117b86bcb8..f975cc3ed8a 100644 --- a/specification/trace/semantic_conventions/exceptions.md +++ b/specification/trace/semantic_conventions/exceptions.md @@ -16,6 +16,24 @@ exceptions. An exception SHOULD be recorded as an `Event` on the span during which it occurred. The name of the event MUST be `"exception"`. + + +A typical template for an auto-instrumentation implementing this semantic convention +using an [API-provided `recordException` method](../api.md#record-exception) +could look like this (pseudo-Java): + +```java +Span span = myTracer.startSpan(/*...*/); +try { + // Code that does the actual work which the Span represents +} catch (Throwable e) { + span.recordException(e, Attributes.of("exception.escaped", true)); + throw e; +} finally { + span.end(); +} +``` + ## Attributes The table below indicates which attributes should be added to the `Event` and @@ -27,9 +45,24 @@ their types. | `exception.type` | string | The type of the exception (its fully-qualified class name, if applicable). The dynamic type of the exception should be preferred over the static type in languages that support it. | `java.net.ConnectException`
`OSError` | See below | | `exception.message` | string | The exception message. | `Division by zero`
`Can't convert 'int' object to str implicitly` | See below | | `exception.stacktrace` | string | A stacktrace as a string in the natural representation for the language runtime. The representation is to be determined and documented by each language SIG. | `Exception in thread "main" java.lang.RuntimeException: Test exception\n at com.example.GenerateTrace.methodB(GenerateTrace.java:13)\n at com.example.GenerateTrace.methodA(GenerateTrace.java:9)\n at com.example.GenerateTrace.main(GenerateTrace.java:5)` | No | -| `exception.escaped` | boolean | SHOULD be set to true if the exception event is recorded at a point where it is known that the exception is escaping the scope of the span (e.g. if there is an exception active just before ending the span). [1] | | No | - -**[1]:** Note that an exception may still leave the scope of the span even if this was not set or set to false, if the event was recorded at an earlier time. +| `exception.escaped` | boolean | SHOULD be set to true if the exception event is recorded at a point where it is known that the exception is escaping the scope of the span. [1] | | No | + +**[1]:** An exception is considered to have escaped (or left) the scope of a span, +if that span is ended while the exception is still logically "in flight". +This may be actually "in flight" in some languages (e.g. if the exception +is passed to a Context manager's `__exit__` method in Python) but will +usually be caught at the point of recording the exception in most languages. + +It is usually not possible to determine at the point where an exception is thrown +whether it will escape the scope of a span. +However, it is trivial to know that an exception +will escape, if one checks for an active exception just before ending the span, +as done in the [example above](#exception-end-example). + +It follows that an exception may still escape the scope of the span +even if the `exception.escaped` attribute was not set or set to false, +since the event might have been recorded at a time where it was not +clear whether the exception will escape. **Additional attribute requirements:** At least one of the following sets of attributes is required: