| 1 |
# * `session$userData[["_otel_span_reactive_update"]]` - The active reactive update span (or `NULL`) |
| 2 |
|
| 3 |
|
| 4 |
#' Start a `reactive_update` OpenTelemetry span and store it |
| 5 |
#' |
| 6 |
#' Used when a reactive expression is updated |
| 7 |
#' Will only start the span iff the otel tracing is enabled |
| 8 |
#' @param ... Ignored |
| 9 |
#' @param domain The reactive domain to associate with the span |
| 10 |
#' @return Invisibly returns. |
| 11 |
#' @seealso `otel_span_reactive_update_teardown()` |
| 12 |
#' @noRd |
| 13 |
otel_span_reactive_update_init <- function(..., domain) { |
| 14 |
|
| 15 |
if (!has_otel_collect("reactive_update")) return() |
| 16 |
|
| 17 |
# Ensure cleanup is registered only once per session |
| 18 |
if (is.null(domain$userData[["_otel_has_reactive_cleanup"]])) { |
| 19 |
domain$userData[["_otel_has_reactive_cleanup"]] <- TRUE |
| 20 |
|
| 21 |
# Clean up any dangling reactive spans on an unplanned exit |
| 22 |
domain$onSessionEnded(function() { |
| 23 |
otel_span_reactive_update_teardown(domain = domain) |
| 24 |
}) |
| 25 |
} |
| 26 |
|
| 27 |
# Safety check |
| 28 |
if (is_otel_span(domain$userData[["_otel_span_reactive_update"]])) { |
| 29 |
stop("Reactive update span already exists") |
| 30 |
} |
| 31 |
|
| 32 |
domain$userData[["_otel_span_reactive_update"]] <- |
| 33 |
start_otel_span( |
| 34 |
"reactive_update", |
| 35 |
..., |
| 36 |
attributes = otel_session_id_attrs(domain) |
| 37 |
) |
| 38 |
|
| 39 |
invisible() |
| 40 |
} |
| 41 |
|
| 42 |
#' End a `reactive_update` OpenTelemetry span and remove it from the session |
| 43 |
#' @param ... Ignored |
| 44 |
#' @param domain The reactive domain to associate with the span |
| 45 |
#' @return Invisibly returns. |
| 46 |
#' @seealso `otel_span_reactive_update_init()` |
| 47 |
#' @noRd |
| 48 |
otel_span_reactive_update_teardown <- function(..., domain) { |
| 49 |
ospan <- domain$userData[["_otel_span_reactive_update"]] |
| 50 |
|
| 51 |
if (is_otel_span(ospan)) { |
| 52 |
otel::end_span(ospan) |
| 53 |
domain$userData[["_otel_span_reactive_update"]] <- NULL |
| 54 |
} |
| 55 |
|
| 56 |
invisible() |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
#' Run expr within a `reactive_update` OpenTelemetry span |
| 61 |
#' |
| 62 |
#' Used to wrap the execution of a reactive expression. Will only |
| 63 |
#' require/activate the span iff the otel tracing is enabled |
| 64 |
#' @param expr The expression to executed within the span |
| 65 |
#' @param ... Ignored |
| 66 |
#' @param domain The reactive domain to associate with the span |
| 67 |
#' @noRd |
| 68 |
with_otel_span_reactive_update <- function(expr, ..., domain) { |
| 69 |
ospan <- domain$userData[["_otel_span_reactive_update"]] |
| 70 |
|
| 71 |
if (!is_otel_span(ospan)) { |
| 72 |
return(force(expr)) |
| 73 |
} |
| 74 |
|
| 75 |
# Given the reactive update span is started before and ended when exec count |
| 76 |
# is 0, we only need to wrap the expr in the span context |
| 77 |
otel::with_active_span(ospan, {force(expr)}) |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
#' Run expr within `reactive_update` otel span if not already active |
| 82 |
#' |
| 83 |
#' If the reactive update otel span is not already active, run the expression |
| 84 |
#' within the reactive update otel span context. This ensures that nested calls |
| 85 |
#' to reactive expressions do not attempt to re-enter the same span. |
| 86 |
#' |
| 87 |
#' This method is used within Context `run()` and running an Output's observer |
| 88 |
#' implementation |
| 89 |
#' @param expr The expression to executed within the span |
| 90 |
#' @param ... Ignored |
| 91 |
#' @param domain The reactive domain to associate with the span |
| 92 |
#' @noRd |
| 93 |
maybe_with_otel_span_reactive_update <- function(expr, ..., domain) { |
| 94 |
if (is.null(domain$userData[["_otel_reactive_update_is_active"]])) { |
| 95 |
domain$userData[["_otel_reactive_update_is_active"]] <- TRUE |
| 96 |
|
| 97 |
# When the expression is done promising, clear the active flag |
| 98 |
hybrid_then( |
| 99 |
{ |
| 100 |
with_otel_span_reactive_update(domain = domain, expr) |
| 101 |
}, |
| 102 |
on_success = function(value) { |
| 103 |
domain$userData[["_otel_reactive_update_is_active"]] <- NULL |
| 104 |
}, |
| 105 |
on_failure = function(e) { |
| 106 |
domain$userData[["_otel_reactive_update_is_active"]] <- NULL |
| 107 |
}, |
| 108 |
# Return the value before the callbacks |
| 109 |
tee = TRUE |
| 110 |
) |
| 111 |
} else { |
| 112 |
expr |
| 113 |
} |
| 114 |
} |