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
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,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:

Expand Down
15 changes: 11 additions & 4 deletions semantic_conventions/trace/exception.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,18 @@ 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).
it is known that the exception is escaping the scope of 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.
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.
Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I find this paragraph exceedingly confusing. The first sentence says "difficult to say X, but trivial to say X" (X==X). The terms "in flight" and "escape" are undefined (and not a particularly common lingo). "in flight" exception, specifically, is not observable by the code, whereas a caught exception is not "in flight".

Terms that are not ambiguous: catch exception, re-throw exception.

Copy link
Member Author

Choose a reason for hiding this comment

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

These terms are not ambiguous but also not appropriate here. If you look at the code without the auto-instrumentation in place, the exception would indeed be currently in a state where it was thrown but not yet caught.

Copy link
Member Author

@Oberon00 Oberon00 Sep 19, 2020

Choose a reason for hiding this comment

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

@yurishkuro I tried to clarify in 350ff89 although I think it would also be OK to be more vague here. WDYT?

constraints:
- any_of:
- "exception.type"
Expand Down
23 changes: 21 additions & 2 deletions 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 @@ -27,9 +45,10 @@ 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`<br>`OSError` | See below |
| `exception.message` | string | The exception message. | `Division by zero`<br>`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 |
| `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]:** 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.
**[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 "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.

**Additional attribute requirements:** At least one of the following sets of attributes is required:

Expand Down