A failure event for logs and observability tools¶
Source: R/observability.R ยท Help: ?condition_to_event
condition_to_event() pairs the envelope of a condition with the operation
context it happened in, in the event format the Python package's
exception_to_observability_event() produces:
Usage¶
condition_to_event(
cnd,
operation_context = NULL,
include_attributes = TRUE,
max_depth = 8L
)
condition_to_event_json(
cnd,
operation_context = NULL,
include_attributes = TRUE,
max_depth = 8L,
pretty = FALSE
)
Arguments¶
cnd- A condition object.
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}. pretty- Indent the JSON for reading?
Value¶
condition_to_event() returns the event as a list;
condition_to_event_json() returns it as a single JSON string.
Details¶
The envelope stays exactly as condition_to_envelope() writes it, redacted
and with its chain of causes. The operation context sits under its own key
instead of being flattened into the failure, so a log pipeline can index the
label fields and keep the run identifiers for correlation. With no context,
or an empty one, there is no operation key: nothing is invented.
condition_to_event_json() writes the event as one line of strict JSON,
ready for a JSON-lines log.
Examples¶
context <- operation_context(
system = "batch", operation = "load_orders", job_id = "job-42"
)
err <- tryCatch(
stop(missing_column_error("customer_id", dataframe = "orders")),
error = identity
)
cat(condition_to_event_json(err, operation_context = context, pretty = TRUE))
# Log every failure of a job as one JSON line.
log_file <- tempfile(fileext = ".jsonl")
tryCatch(
stop(validation_error("age", -1)),
dataexcept_error = function(e) {
cat(condition_to_event_json(e, operation_context = context), "\n",
file = log_file, append = TRUE, sep = ""
)
}
)
readLines(log_file)