Skip to content

Commit

Permalink
move 'recording exceptions' to recording_errors.md doc
Browse files Browse the repository at this point in the history
  • Loading branch information
lmolkova committed Jan 16, 2025
1 parent 7c037d7 commit 1f37350
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 52 deletions.
52 changes: 1 addition & 51 deletions docs/exceptions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,11 @@ path_base_for_github_subdir:

# Semantic Conventions for Exceptions

**Status**: [Stable][DocumentStatus], Unless otherwise specified.
**Status**: [Stable][DocumentStatus]

Semantic conventions for Exceptions are defined for the following signals:

* [Exceptions on spans](exceptions-spans.md): Semantic Conventions for Exceptions associated with *spans*.
* [Exceptions in logs](exceptions-logs.md): Semantic Conventions for Exceptions recorded in *logs*.

## Reporting exceptions in instrumentation code

**Status**: [Development][DocumentStatus]

When an instrumented operation fails with an exception, instrumentation SHOULD record
this exception as a [span event](exceptions-spans.md) or a [log record](exceptions-logs.md).

Recording exceptions on spans SHOULD be accompanied by:

- setting span status to `ERROR`
- setting [`error.type`](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/attributes-registry/error.md#error-type) attribute

Refer to the [Recording errors](/docs/general/recording-errors.md) document for additional
details on how to record errors across different signals.

It's RECOMMENDED to use the `Span.recordException` API or logging library API that takes exception instance
instead of providing individual attributes. This enables the OpenTelemetry SDK to
control what information is recorded based on application configuration.

It's NOT RECOMMENDED to record the same exception more than once.
It's NOT RECOMMENDED to record exceptions that are handled by the instrumented library.

For example, in this code-snippet, `ResourceAlreadyExistsException` is handled and the corresponding
native instrumentation should not record it. Exceptions which are propagated
to the caller should be recorded (or logged) once.

```java
public boolean createIfNotExists(String resourceId) throws IOException {
Span span = startSpan();
try {
create(resourceId);
return true;
} catch (ResourceAlreadyExistsException e) {
// not recording exception and not setting span status to error - exception is handled
// but we can set attributes that capture additional details
span.setAttribute(AttributeKey.stringKey("acme.resource.create.status"), "already_exists");
return false;
} catch (IOException e) {
// recording exception here (assuming it was not recorded inside `create` method)
span.recordException(e);
// or
// logger.warn(e);

span.setAttribute(AttributeKey.stringKey("error.type"), e.getClass().getCanonicalName())
span.setStatus(StatusCode.ERROR, e.getMessage());
throw e;
}
}
```

[DocumentStatus]: https://opentelemetry.io/docs/specs/otel/document-status
42 changes: 41 additions & 1 deletion docs/general/recording-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ When the operation ends with an error, instrumentation:
When the operation fails with an exception, the span status description SHOULD be set to
the exception message.

Refer to the [general exception guidance](/docs/exceptions/README.md) on capturing exception
Refer to the [recording exceptions](#recording-errors) on capturing exception
details.

## How to record errors on metrics
Expand All @@ -76,5 +76,45 @@ and metrics when both are reported. A span and its corresponding metric for a si
operation SHOULD have the same `error.type` value if the operation failed and SHOULD NOT
include it if the operation succeeded.

## Recording exceptions

When an instrumented operation fails with an exception, instrumentation SHOULD record
this exception as a [span event](exceptions-spans.md) or a [log record](exceptions-logs.md).

It's RECOMMENDED to use the `Span.recordException` API or logging library API that takes exception instance
instead of providing individual attributes. This enables the OpenTelemetry SDK to
control what information is recorded based on application configuration.

It's NOT RECOMMENDED to record the same exception more than once.
It's NOT RECOMMENDED to record exceptions that are handled by the instrumented library.

For example, in this code-snippet, `ResourceAlreadyExistsException` is handled and the corresponding
native instrumentation should not record it. Exceptions which are propagated
to the caller should be recorded (or logged) once.

```java
public boolean createIfNotExists(String resourceId) throws IOException {
Span span = startSpan();
try {
create(resourceId);
return true;
} catch (ResourceAlreadyExistsException e) {
// not recording exception and not setting span status to error - exception is handled
// but we can set attributes that capture additional details
span.setAttribute(AttributeKey.stringKey("acme.resource.create.status"), "already_exists");
return false;
} catch (IOException e) {
// recording exception here (assuming it was not recorded inside `create` method)
span.recordException(e);
// or
// logger.warn(e);

span.setAttribute(AttributeKey.stringKey("error.type"), e.getClass().getCanonicalName())
span.setStatus(StatusCode.ERROR, e.getMessage());
throw e;
}
}
```

[DocumentStatus]: https://opentelemetry.io/docs/specs/otel/document-status
[SpanStatus]: https://github.com/open-telemetry/opentelemetry-specification/tree/v1.39.0/specification/trace/api.md#set-status

0 comments on commit 1f37350

Please sign in to comment.