| 1 |
# observe mymod:<anonymous> |
| 2 |
# observe <anonymous> |
| 3 |
# observe mylabel |
| 4 |
|
| 5 |
# -- Reactives -------------------------------------------------------------- |
| 6 |
|
| 7 |
#' OpenTelemetry Label Generation Functions |
| 8 |
#' |
| 9 |
#' Functions for generating formatted labels for OpenTelemetry tracing spans |
| 10 |
#' in Shiny applications. These functions handle module namespacing and |
| 11 |
#' cache/event modifiers for different Shiny reactive constructs. |
| 12 |
#' |
| 13 |
#' @param x The object to generate a label for (reactive, observer, etc.) |
| 14 |
#' @param label Character string label for reactive values |
| 15 |
#' @param key Character string key for reactiveValues operations |
| 16 |
#' @param ... Additional arguments (unused) |
| 17 |
#' @param domain Shiny domain object containing namespace information |
| 18 |
#' |
| 19 |
#' @return Character string formatted for OpenTelemetry span labels |
| 20 |
#' @name otel_label |
| 21 |
#' @noRd |
| 22 |
NULL |
| 23 |
|
| 24 |
otel_span_label_reactive <- function(x, ..., domain) { |
| 25 |
fn_name <- otel_label_with_modifiers( |
| 26 |
x, |
| 27 |
"reactive", |
| 28 |
cache_class = "reactive.cache", |
| 29 |
event_class = "reactive.event" |
| 30 |
) |
| 31 |
|
| 32 |
label <- attr(x, "observable", exact = TRUE)[[".label"]] |
| 33 |
otel_span_label <- otel_label_upgrade(label, domain = domain) |
| 34 |
|
| 35 |
sprintf("%s %s", fn_name, otel_span_label) |
| 36 |
} |
| 37 |
|
| 38 |
otel_span_label_render_function <- function(x, ..., domain) { |
| 39 |
fn_name <- otel_label_with_modifiers( |
| 40 |
x, |
| 41 |
"output", |
| 42 |
cache_class = "shiny.render.function.cache", |
| 43 |
event_class = "shiny.render.function.event" |
| 44 |
) |
| 45 |
|
| 46 |
label <- getCurrentOutputInfo(session = domain)$name %||% "<unknown>" |
| 47 |
otel_span_label <- otel_label_upgrade(label, domain = domain) |
| 48 |
|
| 49 |
sprintf("%s %s", fn_name, otel_span_label) |
| 50 |
} |
| 51 |
|
| 52 |
otel_span_label_observer <- function(x, ..., domain) { |
| 53 |
fn_name <- otel_label_with_modifiers( |
| 54 |
x, |
| 55 |
"observe", |
| 56 |
cache_class = NULL, # Do not match a cache class here |
| 57 |
event_class = "Observer.event" |
| 58 |
) |
| 59 |
|
| 60 |
otel_span_label <- otel_label_upgrade(x$.label, domain = domain) |
| 61 |
|
| 62 |
sprintf("%s %s", fn_name, otel_span_label) |
| 63 |
} |
| 64 |
|
| 65 |
# -- Set reactive value(s) ---------------------------------------------------- |
| 66 |
|
| 67 |
otel_log_label_set_reactive_val <- function(label, ..., domain) { |
| 68 |
sprintf( |
| 69 |
"Set reactiveVal %s", |
| 70 |
otel_label_upgrade(label, domain = domain) |
| 71 |
) |
| 72 |
} |
| 73 |
|
| 74 |
otel_log_label_set_reactive_values <- function(label, key, ..., domain) { |
| 75 |
sprintf( |
| 76 |
"Set reactiveValues %s$%s", |
| 77 |
otel_label_upgrade(label, domain = domain), |
| 78 |
key |
| 79 |
) |
| 80 |
} |
| 81 |
|
| 82 |
# -- ExtendedTask ------------------------------------------------------------- |
| 83 |
|
| 84 |
otel_span_label_extended_task <- function(label, suffix = NULL, ..., domain) { |
| 85 |
sprintf( |
| 86 |
"ExtendedTask %s", |
| 87 |
otel_label_upgrade(label, domain = domain) |
| 88 |
) |
| 89 |
} |
| 90 |
otel_log_label_extended_task_add_to_queue <- function(label, ..., domain) { |
| 91 |
sprintf( |
| 92 |
"ExtendedTask %s add to queue", |
| 93 |
otel_label_upgrade(label, domain = domain) |
| 94 |
) |
| 95 |
} |
| 96 |
|
| 97 |
# -- Debounce / Throttle ------------------------------------------------------- |
| 98 |
|
| 99 |
otel_label_debounce <- function(label, ..., domain) { |
| 100 |
sprintf( |
| 101 |
"debounce %s", |
| 102 |
otel_label_upgrade(label, domain = domain) |
| 103 |
) |
| 104 |
} |
| 105 |
|
| 106 |
otel_label_throttle <- function(label, ..., domain) { |
| 107 |
sprintf( |
| 108 |
"throttle %s", |
| 109 |
otel_label_upgrade(label, domain = domain) |
| 110 |
) |
| 111 |
} |
| 112 |
|
| 113 |
# ---- Reactive Poll / File Reader ----------------------------------------------- |
| 114 |
otel_label_reactive_poll <- function(label, ..., domain) { |
| 115 |
sprintf( |
| 116 |
"reactivePoll %s", |
| 117 |
otel_label_upgrade(label, domain = domain) |
| 118 |
) |
| 119 |
} |
| 120 |
otel_label_reactive_file_reader <- function(label, ..., domain) { |
| 121 |
sprintf( |
| 122 |
"reactiveFileReader %s", |
| 123 |
otel_label_upgrade(label, domain = domain) |
| 124 |
) |
| 125 |
} |
| 126 |
|
| 127 |
# -- Helpers -------------------------------------------------------------- |
| 128 |
|
| 129 |
#' Modify function name based on object class modifiers |
| 130 |
#' |
| 131 |
#' @param x Object to check class of |
| 132 |
#' @param fn_name Base function name |
| 133 |
#' @param cache_class Optional class name that indicates cache operation |
| 134 |
#' @param event_class Optional class name that indicates event operation |
| 135 |
#' |
| 136 |
#' @return Modified function name with "cache" or "event" suffix if applicable |
| 137 |
#' @noRd |
| 138 |
otel_label_with_modifiers <- function( |
| 139 |
x, |
| 140 |
fn_name, |
| 141 |
cache_class = NULL, |
| 142 |
event_class = NULL |
| 143 |
) { |
| 144 |
for (x_class in rev(class(x))) { |
| 145 |
if (!is.null(cache_class) && x_class == cache_class) { |
| 146 |
fn_name <- sprintf("%s cache", fn_name) |
| 147 |
} else if (!is.null(event_class) && x_class == event_class) { |
| 148 |
fn_name <- sprintf("%s event", fn_name) |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
fn_name |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
#' Upgrade and format OpenTelemetry labels with module namespacing |
| 157 |
#' |
| 158 |
#' Processes labels for OpenTelemetry tracing, replacing default verbose labels |
| 159 |
#' with cleaner alternatives and prepending module namespaces when available. |
| 160 |
#' |
| 161 |
#' @param label Character string label to upgrade |
| 162 |
#' @param ... Additional arguments (unused) |
| 163 |
#' @param domain Shiny domain object containing namespace information |
| 164 |
#' |
| 165 |
#' @return Modified label string with module prefix if applicable |
| 166 |
#' @noRd |
| 167 |
#' |
| 168 |
#' @details |
| 169 |
#' Module prefix examples: |
| 170 |
#' - "" -> "" |
| 171 |
#' - "my-nested-mod-" -> "my-nested-mod" |
| 172 |
otel_label_upgrade <- function(label, ..., domain) { |
| 173 |
# By default, `observe()` sets the label to `observe(CODE)` |
| 174 |
# This label is too big and inconsistent. |
| 175 |
# Replace it with `<anonymous>` |
| 176 |
# (Similar with `eventReactive()` and `observeEvent()`) |
| 177 |
if (is_default_label(label) && grepl("(", label, fixed = TRUE)) { |
| 178 |
label <- "<anonymous>" |
| 179 |
# label <- sprintf("<anonymous> - %s", label) |
| 180 |
} |
| 181 |
|
| 182 |
if (is.null(domain)) { |
| 183 |
return(label) |
| 184 |
} |
| 185 |
|
| 186 |
namespace <- domain$ns("") |
| 187 |
|
| 188 |
if (!nzchar(namespace)) { |
| 189 |
return(label) |
| 190 |
} |
| 191 |
|
| 192 |
# Remove trailing module separator |
| 193 |
mod_ns <- sub(sprintf("%s$", ns.sep), "", namespace) |
| 194 |
|
| 195 |
# Prepend the module name to the label |
| 196 |
# Ex: `"mymod:x"` |
| 197 |
sprintf("%s:%s", mod_ns, label) |
| 198 |
} |