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

Exception semantic conventions: Add example & more explanation for exception.escaped. #946

Merged
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ New:
- Add Span API and semantic conventions for recording exceptions
([#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` was added
arminru marked this conversation as resolved.
Show resolved Hide resolved
([#784](https://github.com/open-telemetry/opentelemetry-specification/pull/784),
[#946](https://github.com/open-telemetry/opentelemetry-specification/pull/946))

Updates:

Expand Down
33 changes: 32 additions & 1 deletion specification/trace/semantic_conventions/exceptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 name="exception-end-example"></a>

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering whether we should hold up adding this example till the actual API change hits OTel Java ;)

Copy link
Member Author

@Oberon00 Oberon00 Sep 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@carlosalberto OTel Java already supports this API. But the actual API has more boilerplate (I think I would have to write AttributeValue.booleanAttribute(true) for the second parameter). See https://github.com/open-telemetry/opentelemetry-java/blob/v0.8.0/api/src/main/java/io/opentelemetry/trace/Span.java#L232-L239 (open-telemetry/opentelemetry-java#1599)

Copy link
Member Author

@Oberon00 Oberon00 Sep 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the example is easier to read for non-Java users without that boilerplate, but I could add it too, if you prefer. Then I can write "Java" instead of "pseudo-Java" above.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be personally more comfortable with the exact code we need (i.e. through Attributes). Else, go full pseudo code way :)

Copy link
Member Author

@Oberon00 Oberon00 Sep 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@carlosalberto Now (d359f47) it's using the real OTel-Java API (at some version; seems like the Attributes are recently often changed)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are trying to get the last changes through before GA, but Attributes should settle down here quite soon. :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please continue at #946 (comment)

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
Expand All @@ -26,7 +44,7 @@ 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. E.g. "java.net.ConnectException", "OSError" | One of `exception.type` or `exception.message` is required |
| exception.message | String | The exception message. E.g. `"Division by zero"`, `"Can't convert 'int' object to str implicitly"` | One of `exception.type` or `exception.message` is required |
| 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. E.g. `"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 | Bool | 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 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. | No |
| exception.escaped | Bool | 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. See [explanation below](#escaped-exceptions). | No |

### Stacktrace Representation

Expand Down Expand Up @@ -57,3 +75,16 @@ grained information from a stacktrace, if necessary.
[csharp-stacktrace]: https://docs.microsoft.com/en-us/dotnet/api/system.exception.tostring
[go-stacktrace]: https://golang.org/pkg/runtime/debug/#Stack
[telemetry-sdk-resource]: https://github.com/open-telemetry/opentelemetry-specification/tree/master/specification/resource/semantic_conventions#telemetry-sdk

### Escaped exceptions

An exception is considered to have escaped (or left) the scope of a span,
if that span is ended while the exception is still "in flight".

While it is usually not possible to determine whether some exception thrown
now *will* escape the scope of a span, it is trivial to know that an exception
will escape, if one checks for an active exception just before ending the span.
See 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 that event might have been recorded at a time
where it was not clear whether the exception will escape.