| 1 |
#' Temporarily set OpenTelemetry (OTel) collection level |
| 2 |
#' |
| 3 |
#' @description |
| 4 |
#' Control Shiny's OTel collection level for particular reactive expression(s). |
| 5 |
#' |
| 6 |
#' `withOtelCollect()` sets the OpenTelemetry collection level for |
| 7 |
#' the duration of evaluating `expr`. `localOtelCollect()` sets the collection |
| 8 |
#' level for the remainder of the current function scope. |
| 9 |
#' |
| 10 |
#' @details |
| 11 |
#' Note that `"session"` and `"reactive_update"` levels are not permitted as |
| 12 |
#' these are runtime-specific levels that should only be set permanently via |
| 13 |
#' `options(shiny.otel.collect = ...)` or the `SHINY_OTEL_COLLECT` environment |
| 14 |
#' variable, not temporarily during reactive expression creation. |
| 15 |
#' |
| 16 |
#' @section Best practice: |
| 17 |
#' |
| 18 |
#' Best practice is to set the collection level for code that *creates* reactive |
| 19 |
#' expressions, not code that *runs* them. For instance: |
| 20 |
#' |
| 21 |
#' ```r |
| 22 |
#' # Disable telemetry for a reactive expression |
| 23 |
#' withOtelCollect("none", { |
| 24 |
#' my_reactive <- reactive({ ... }) |
| 25 |
#' }) |
| 26 |
#' |
| 27 |
#' # Disable telemetry for a render function |
| 28 |
#' withOtelCollect("none", { |
| 29 |
#' output$my_plot <- renderPlot({ ... }) |
| 30 |
#' }) |
| 31 |
#' |
| 32 |
#' #' # Disable telemetry for an observer |
| 33 |
#' withOtelCollect("none", { |
| 34 |
#' observe({ ... })) |
| 35 |
#' }) |
| 36 |
#' |
| 37 |
#' # Disable telemetry for an entire module |
| 38 |
#' withOtelCollect("none", { |
| 39 |
#' my_result <- my_module("my_id") |
| 40 |
#' }) |
| 41 |
#' # Use `my_result` as normal here |
| 42 |
#' ``` |
| 43 |
#' |
| 44 |
#' NOTE: It's not recommended to pipe existing reactive objects into |
| 45 |
#' `withOtelCollect()` since they won't inherit their intended OTel settings, |
| 46 |
#' leading to confusion. |
| 47 |
#' |
| 48 |
#' @param collect Character string specifying the OpenTelemetry collection level. |
| 49 |
#' Must be one of the following: |
| 50 |
#' |
| 51 |
#' * `"none"` - No telemetry data collected |
| 52 |
#' * `"reactivity"` - Collect reactive execution spans (includes session and |
| 53 |
#' reactive update events) |
| 54 |
#' * `"all"` - All available telemetry (currently equivalent to `"reactivity"`) |
| 55 |
#' @param expr Expression to evaluate with the specified collection level |
| 56 |
#' (for `withOtelCollect()`). |
| 57 |
#' @param envir Environment where the collection level should be set |
| 58 |
#' (for `localOtelCollect()`). Defaults to the parent frame. |
| 59 |
#' |
| 60 |
#' @return |
| 61 |
#' * `withOtelCollect()` returns the value of `expr`. |
| 62 |
#' * `localOtelCollect()` is called for its side effect and returns the previous |
| 63 |
#' `collect` value invisibly. |
| 64 |
#' |
| 65 |
#' @seealso See the `shiny.otel.collect` option within [`shinyOptions`]. Setting |
| 66 |
#' this value will globally control OpenTelemetry collection levels. |
| 67 |
#' |
| 68 |
#' @examples |
| 69 |
#' \dontrun{ |
| 70 |
#' # Temporarily disable telemetry collection |
| 71 |
#' withOtelCollect("none", { |
| 72 |
#' # Code here won't generate telemetry |
| 73 |
#' reactive({ input$x + 1 }) |
| 74 |
#' }) |
| 75 |
#' |
| 76 |
#' # Collect reactivity telemetry but not other events |
| 77 |
#' withOtelCollect("reactivity", { |
| 78 |
#' # Reactive execution will be traced |
| 79 |
#' observe({ print(input$x) }) |
| 80 |
#' }) |
| 81 |
#' |
| 82 |
#' # Use local variant in a function |
| 83 |
#' my_function <- function() { |
| 84 |
#' localOtelCollect("none") |
| 85 |
#' # Rest of function executes without telemetry |
| 86 |
#' reactive({ input$y * 2 }) |
| 87 |
#' } |
| 88 |
#' } |
| 89 |
#' |
| 90 |
#' @rdname withOtelCollect |
| 91 |
#' @export |
| 92 |
withOtelCollect <- function(collect, expr) { |
| 93 |
collect <- as_otel_collect_with(collect) |
| 94 |
|
| 95 |
withr::with_options( |
| 96 |
list(shiny.otel.collect = collect), |
| 97 |
expr |
| 98 |
) |
| 99 |
} |
| 100 |
|
| 101 |
#' @rdname withOtelCollect |
| 102 |
#' @export |
| 103 |
localOtelCollect <- function(collect, envir = parent.frame()) { |
| 104 |
collect <- as_otel_collect_with(collect) |
| 105 |
|
| 106 |
old <- withr::local_options( |
| 107 |
list(shiny.otel.collect = collect), |
| 108 |
.local_envir = envir |
| 109 |
) |
| 110 |
|
| 111 |
invisible(old) |
| 112 |
} |
| 113 |
|
| 114 |
# Helper function to validate collect levels for with/local functions |
| 115 |
# Only allows "none", "reactivity", and "all" - not "session" or "reactive_update" |
| 116 |
as_otel_collect_with <- function(collect) { |
| 117 |
if (!is.character(collect)) { |
| 118 |
stop("`collect` must be a character vector.") |
| 119 |
} |
| 120 |
|
| 121 |
allowed_levels <- c("none", "reactivity", "all") |
| 122 |
collect <- match.arg(collect, allowed_levels, several.ok = FALSE) |
| 123 |
|
| 124 |
return(collect) |
| 125 |
} |