| 1 |
# # Approach |
| 2 |
# Use flags on the reactive object to indicate whether to record OpenTelemetry spans. |
| 3 |
# |
| 4 |
# Cadence: |
| 5 |
# * `$.isRecordingOtel` - Whether to record OpenTelemetry spans for this reactive object |
| 6 |
# * `$.otelLabel` - The label to use for the OpenTelemetry span |
| 7 |
# * `$.otelAttrs` - Additional attributes to add to the OpenTelemetry span |
| 8 |
|
| 9 |
|
| 10 |
#' Add OpenTelemetry for reactivity to an object |
| 11 |
#' |
| 12 |
#' @description |
| 13 |
#' |
| 14 |
#' `enable_otel_*()` methods add OpenTelemetry flags for [reactive()] expressions |
| 15 |
#' and `render*` functions (like [renderText()], [renderTable()], ...). |
| 16 |
#' |
| 17 |
#' Wrapper to creating an active reactive OpenTelemetry span that closes when |
| 18 |
#' the reactive expression is done computing. Typically this is when the |
| 19 |
#' reactive expression finishes (synchronous) or when the returned promise is |
| 20 |
#' done computing (asynchronous). |
| 21 |
|
| 22 |
#' @section Async with OpenTelemetry: |
| 23 |
#' |
| 24 |
#' With a reactive expression, the key and/or value expression can be |
| 25 |
#' _asynchronous_. In other words, they can be promises --- not regular R |
| 26 |
#' promises, but rather objects provided by the |
| 27 |
#' \href{https://rstudio.github.io/promises/}{\pkg{promises}} package, which |
| 28 |
#' are similar to promises in JavaScript. (See [promises::promise()] for more |
| 29 |
#' information.) You can also use [mirai::mirai()] or [future::future()] |
| 30 |
#' objects to run code in a separate process or even on a remote machine. |
| 31 |
#' |
| 32 |
#' When reactive expressions are being calculated in parallel (by having |
| 33 |
#' another reactive promise compute in the main process), the currently active |
| 34 |
#' OpenTelemetry span will be dynamically swapped out according to the |
| 35 |
#' currently active reactive expression. This means that as long as a promise |
| 36 |
#' was `then()`ed or `catch()`ed with an active OpenTelemetry span, the span |
| 37 |
#' will be correctly propagated to the next step (and subsequently other |
| 38 |
#' steps) in the promise chain. |
| 39 |
#' |
| 40 |
#' While the common case is for a reactive expression to be created |
| 41 |
#' synchronously, troubles arise when the reactive expression is created |
| 42 |
#' asynchronously. The span **must** be created before the reactive expression |
| 43 |
#' is executed, it **must** be active for the duration of the expression, and |
| 44 |
#' it **must** not be closed until the reactive expression is done executing. |
| 45 |
#' This is not easily achieved with a single function call, so we provide a |
| 46 |
#' way to create a reactive expression that is bound to an OpenTelemetry |
| 47 |
#' span. |
| 48 |
#' |
| 49 |
#' @section Span management and performance: |
| 50 |
#' |
| 51 |
#' Dev note - Barret 2025-10: |
| 52 |
#' Typically, an OpenTelemetry span (`otel_span`) will inherit from the parent |
| 53 |
#' span. This works well and we can think of the hierarchy as a tree. With |
| 54 |
#' `options("shiny.otel.collect" = <value>)`, we are able to control with a sliding |
| 55 |
#' dial how much of the tree we are interested in: "none", "session", |
| 56 |
#' "reactive_update", "reactivity", and finally "all". |
| 57 |
#' |
| 58 |
#' Leveraging this hierarchy, we can avoid creating spans that are not needed. |
| 59 |
#' The act of making a noop span takes on the order of 10microsec. Handling of |
| 60 |
#' the opspan is also in the 10s of microsecond range. We should avoid this when |
| 61 |
#' we **know** that we're not interested in the span. Therefore, manually |
| 62 |
#' handling spans should be considered for Shiny. |
| 63 |
#' |
| 64 |
#' * Q: |
| 65 |
#' * But what about app author who want the current span? Is there any |
| 66 |
#' guarantee that the current span is expected `reactive()` span? |
| 67 |
#' * A: |
| 68 |
#' * No. The current span is whatever the current span is. If the app author |
| 69 |
#' wants a specific span, they should create it themselves. |
| 70 |
#' * Proof: |
| 71 |
#' ```r |
| 72 |
#' noop <- otel::get_active_span() |
| 73 |
#' noop$get_context()$get_span_id() |
| 74 |
#' #> [1] "0000000000000000" |
| 75 |
#' ignore <- otelsdk::with_otel_record({ |
| 76 |
#' a <- otel::start_local_active_span("a") |
| 77 |
#' a$get_context()$get_span_id() |> str() |
| 78 |
#' otel::with_active_span(noop, { |
| 79 |
#' otel::get_active_span()$get_context()$get_span_id() |> str() |
| 80 |
#' }) |
| 81 |
#' }) |
| 82 |
#' #> chr "2645e95715841e75" |
| 83 |
#' #> chr "2645e95715841e75" |
| 84 |
#' # ## It is reasonable to expect the second id to be `0000000000000000`, but it's not. |
| 85 |
#' ``` |
| 86 |
#' Therefore, the app author has no guarantee that the current span is the |
| 87 |
#' span they're expecting. If the app author wants a specific span, they should |
| 88 |
#' create it themselves and let natural inheritance take over. |
| 89 |
#' |
| 90 |
#' Given this, I will imagine that app authors will set |
| 91 |
#' `options("shiny.otel.collect" = "reactive_update")` as their default behavior. |
| 92 |
#' Enough to know things are happening, but not overwhelming from **everything** |
| 93 |
#' that is reactive. |
| 94 |
#' |
| 95 |
#' To _light up_ a specific area, users can call `withr::with_options(list("shiny.otel.collect" = "all"), { ... })`. |
| 96 |
#' |
| 97 |
#' @param x The object to add caching to. |
| 98 |
#' @param ... Future parameter expansion. |
| 99 |
#' @noRd |
| 100 |
NULL |
| 101 |
|
| 102 |
|
| 103 |
enable_otel_reactive_val <- function(x) { |
| 104 |
|
| 105 |
impl <- attr(x, ".impl", exact = TRUE) |
| 106 |
# Set flag for otel logging when setting the value |
| 107 |
impl$.isRecordingOtel <- TRUE |
| 108 |
|
| 109 |
class(x) <- c("reactiveVal.otel", class(x)) |
| 110 |
|
| 111 |
x |
| 112 |
} |
| 113 |
|
| 114 |
enable_otel_reactive_values <- function(x) { |
| 115 |
|
| 116 |
impl <- .subset2(x, "impl") |
| 117 |
# Set flag for otel logging when setting values |
| 118 |
impl$.isRecordingOtel <- TRUE |
| 119 |
|
| 120 |
class(x) <- c("reactivevalues.otel", class(x)) |
| 121 |
|
| 122 |
x |
| 123 |
} |
| 124 |
|
| 125 |
enable_otel_reactive_expr <- function(x) { |
| 126 |
|
| 127 |
domain <- reactive_get_domain(x) |
| 128 |
|
| 129 |
impl <- attr(x, "observable", exact = TRUE) |
| 130 |
impl$.isRecordingOtel <- TRUE |
| 131 |
# Covers both reactive and reactive.event |
| 132 |
impl$.otelLabel <- otel_span_label_reactive(x, domain = impl$.domain) |
| 133 |
|
| 134 |
class(x) <- c("reactiveExpr.otel", class(x)) |
| 135 |
|
| 136 |
x |
| 137 |
} |
| 138 |
|
| 139 |
enable_otel_observe <- function(x) { |
| 140 |
x$.isRecordingOtel <- TRUE |
| 141 |
x$.otelLabel <- otel_span_label_observer(x, domain = x$.domain) |
| 142 |
|
| 143 |
class(x) <- c("Observer.otel", class(x)) |
| 144 |
invisible(x) |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
enable_otel_shiny_render_function <- function(x) { |
| 150 |
|
| 151 |
valueFunc <- force(x) |
| 152 |
otel_span_label <- NULL |
| 153 |
otel_span_attrs <- NULL |
| 154 |
|
| 155 |
renderFunc <- function(...) { |
| 156 |
# Dynamically determine the span label given the current reactive domain |
| 157 |
if (is.null(otel_span_label)) { |
| 158 |
domain <- getDefaultReactiveDomain() |
| 159 |
otel_span_label <<- |
| 160 |
otel_span_label_render_function(x, domain = domain) |
| 161 |
otel_span_attrs <<- c( |
| 162 |
attr(x, "otelAttrs"), |
| 163 |
otel_session_id_attrs(domain) |
| 164 |
) |
| 165 |
} |
| 166 |
|
| 167 |
with_otel_span( |
| 168 |
otel_span_label, |
| 169 |
{ |
| 170 |
hybrid_then( |
| 171 |
valueFunc(...), |
| 172 |
on_failure = set_otel_exception_status_and_throw, |
| 173 |
# Must save the error object |
| 174 |
tee = FALSE |
| 175 |
) |
| 176 |
}, |
| 177 |
attributes = otel_span_attrs |
| 178 |
) |
| 179 |
} |
| 180 |
|
| 181 |
renderFunc <- addAttributes(renderFunc, renderFunctionAttributes(valueFunc)) |
| 182 |
class(renderFunc) <- c("shiny.render.function.otel", class(valueFunc)) |
| 183 |
renderFunc |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
# ## If we ever expose a S3 function, I'd like to add this method. |
| 188 |
# bindOtel.function <- function(x, ...) { |
| 189 |
# cli::cli_abort(paste0( |
| 190 |
# "Don't know how to add OpenTelemetry recording to a plain function. ", |
| 191 |
# "If this is a {.code render*()} function for Shiny, it may need to be updated. ", |
| 192 |
# "Please see {.help shiny::bindOtel} for more information." |
| 193 |
# )) |
| 194 |
# } |