Record a condition on an OpenTelemetry span¶
Source: R/opentelemetry.R ยท Help: ?record_otel_exception
record_otel_exception() records a condition as an exception event on a
span, with the attributes of condition_to_otel_attributes(). With no
span, it records on the active span of the r-lib otel package, when otel
is installed and tracing is on.
Usage¶
record_otel_exception(
cnd,
span = NULL,
operation_context = NULL,
include_attributes = TRUE,
max_depth = 8L,
include_stacktrace = TRUE,
include_envelope = FALSE,
set_status = TRUE
)
Arguments¶
cnd- A condition object.
span- The span to record on: any object whose
record_exception()method takes the condition and anattributeslist, as otel's spans do.NULLmeans otel's active span. operation_context- An operation context from
operation_context(), orNULL. include_attributes- Include the
attributesfield? max_depth- How many levels of chained conditions to render. A cause
beyond this depth is replaced by the truncation marker
{"truncated": true}. include_stacktrace- Include
exception.stacktracewhen the condition has one? include_envelope- Include the whole envelope as a JSON attribute?
set_status- Also set the span's status to
"error", when the span has aset_status()method?
Value¶
TRUE, invisibly, when the exception was recorded, and FALSE
otherwise.
Details¶
It is meant to be called while a failure is being handled, so it is
fail-open: if otel is not installed, no span is recording, the span has no
record_exception() method, or anything goes wrong converting or recording,
it returns FALSE and the failure being handled carries on unchanged. Use
condition_to_otel_attributes() directly when a mistake should be an error.
Recording through dataexcept also keeps credentials out of the trace. The otel SDK builds the exception message by printing the condition and falls back to the condition's call for the stack trace, both unredacted; the attributes recorded here replace those fields with redacted ones.
With set_status = TRUE, the default, the span's status is also set to
"error", with the redacted message as its description. That marks the
operation as failed, and it matters for redaction: when an error escapes a
span that otel ends automatically (one from otel::start_local_active_span(),
otel::with_active_span() or otel::local_active_span()), the SDK records
it itself, unredacted, unless the span's status is already set. Recording
the failure here first means it appears once, redacted. Pass
set_status = FALSE when the handler recovers from the failure and the
operation goes on to succeed.
Examples¶
# A span-like recorder, standing in for an otel span.
recorded <- NULL
span <- list(record_exception = function(error_condition, attributes = NULL, ...) {
recorded <<- attributes
})
err <- validation_error("age", -1)
record_otel_exception(err, span = span)
str(recorded)
# With no span, the active otel span is used. With tracing off there is
# nothing to record on, and the call does nothing.
record_otel_exception(err)