| 1 |
# devtools::load_all(); dev_otel_kitchen() |
| 2 |
|
| 3 |
dev_otel_kitchen <- function() { |
| 4 |
library(mirai) |
| 5 |
mirai::daemons(2) |
| 6 |
|
| 7 |
# Inspiration from |
| 8 |
# * https://github.com/r-lib/otel/commit/a2ef493ae4b97701e4e178ac527f313580539080 |
| 9 |
# * https://github.com/r-lib/otel/commit/09c0eb6c80d5b907976de8fbaf89798cb11f8e6e#diff-169b8f234d0b208affb106fce375f86fefe2f16dba4ad66495a1dc06c8a4cd7b |
| 10 |
|
| 11 |
otel_logger <- otel::get_logger("my-app-logger") |
| 12 |
otel_tracer_name <- "my-app" |
| 13 |
|
| 14 |
log_and_msg <- function(..., .envir = parent.frame()) { |
| 15 |
msg <- paste(...) |
| 16 |
message(" -- ", msg) |
| 17 |
|
| 18 |
otel_log(msg, logger = otel_logger) |
| 19 |
} |
| 20 |
|
| 21 |
my_global_reactive <- reactiveVal(0) |
| 22 |
|
| 23 |
app <- shinyApp( |
| 24 |
ui = fluidPage( |
| 25 |
sliderInput("mymod-x", "x", 1, 10, 5), |
| 26 |
sliderInput("mymod-y", "y", 1, 10, 5), |
| 27 |
div("x * y: "), |
| 28 |
verbatimTextOutput("mymod-txt1"), |
| 29 |
verbatimTextOutput("mymod-txt2"), |
| 30 |
verbatimTextOutput("mymod-txt3"), |
| 31 |
verbatimTextOutput("task_result") |
| 32 |
), |
| 33 |
server = function(input, output, session) { |
| 34 |
log_and_msg("Start new Shiny session") |
| 35 |
|
| 36 |
b <- reactiveVal(1) |
| 37 |
observe(b(42)) |
| 38 |
|
| 39 |
shutdown <- function() { |
| 40 |
later::later( |
| 41 |
function() { |
| 42 |
message("\n\nClosing session for minimal logfire graphs") |
| 43 |
# session$close() |
| 44 |
# httpuv::stopAllServers() |
| 45 |
stopApp() |
| 46 |
mirai::daemons(0) |
| 47 |
}, |
| 48 |
delay = 100 / 1000 |
| 49 |
) |
| 50 |
} |
| 51 |
|
| 52 |
later::later( |
| 53 |
function() { |
| 54 |
if (!session$closed) { |
| 55 |
log_and_msg("Invoking shutdown after 5s") |
| 56 |
shutdown() |
| 57 |
} |
| 58 |
}, |
| 59 |
delay = 5 |
| 60 |
) |
| 61 |
|
| 62 |
xMod <- function(id) { |
| 63 |
moduleServer(id, function(input, output, session) { |
| 64 |
xVal <- reactiveVal(NULL) |
| 65 |
yVal <- reactiveVal(NULL) |
| 66 |
rv <- reactiveValues(x = NULL, y = NULL) |
| 67 |
|
| 68 |
log_and_msg("Shiny module") |
| 69 |
|
| 70 |
x_raw <- reactive({ |
| 71 |
isolate({ |
| 72 |
my_global_reactive(my_global_reactive() + 1) |
| 73 |
}) |
| 74 |
|
| 75 |
x_val <- xVal() |
| 76 |
req(x_val) |
| 77 |
log_and_msg(sprintf("X Val: %s", x_val)) |
| 78 |
x_val |
| 79 |
}) |
| 80 |
x <- debounce(x_raw, 100) |
| 81 |
y_raw <- reactive({ |
| 82 |
y_val <- input$y |
| 83 |
log_and_msg(sprintf("Y Val: %s", y_val)) |
| 84 |
# Sys.sleep(0.5) |
| 85 |
y_val |
| 86 |
}) |> bindCache(input$y) |> bindEvent(input$y) |
| 87 |
y <- throttle(y_raw, 100) |
| 88 |
|
| 89 |
calc <- reactive(label = "barret_calc", { |
| 90 |
log_and_msg("Doing expensive computation...") |
| 91 |
x() * y() |
| 92 |
}) |
| 93 |
|
| 94 |
observe({ |
| 95 |
log_and_msg("x: ", x()) |
| 96 |
}) |
| 97 |
|
| 98 |
output$txt1 <- renderText({ |
| 99 |
calc() |
| 100 |
}) |> |
| 101 |
bindCache(x(), y()) |
| 102 |
output$txt2 <- renderText({ |
| 103 |
calc() |
| 104 |
}) |> |
| 105 |
bindEvent(list(x(), y())) |
| 106 |
output$txt3 <- renderText({ |
| 107 |
calc() |
| 108 |
}) |> |
| 109 |
bindCache(x(), y()) |> |
| 110 |
bindEvent(list(x(), y())) |
| 111 |
|
| 112 |
rand_task <- ExtendedTask$new(function() { |
| 113 |
mirai::mirai( |
| 114 |
{ |
| 115 |
# Slow operation goes here |
| 116 |
Sys.sleep(100 / 1000) |
| 117 |
sample(1:100, 1) |
| 118 |
} |
| 119 |
) |
| 120 |
}) |
| 121 |
|
| 122 |
observeEvent(input$x, { |
| 123 |
# Invoke the extended in an observer |
| 124 |
rand_task$invoke() |
| 125 |
}, label = "invoke_rand_task") |
| 126 |
|
| 127 |
output$task_result <- renderText({ |
| 128 |
# React to updated results when the task completes |
| 129 |
number <- rand_task$result() |
| 130 |
paste0("Your number is ", number, ".") |
| 131 |
}) |
| 132 |
|
| 133 |
mydesc <- reactiveFileReader( |
| 134 |
1000, |
| 135 |
session, |
| 136 |
filePath = system.file("DESCRIPTION", package = "shiny"), |
| 137 |
readFunc = read.dcf |
| 138 |
) |
| 139 |
observe({ |
| 140 |
mydesc() |
| 141 |
}) |
| 142 |
|
| 143 |
myfile <- reactivePoll( |
| 144 |
1000, |
| 145 |
session, |
| 146 |
checkFunc = function() { |
| 147 |
Sys.time() |
| 148 |
}, |
| 149 |
# This function returns the content of log_file |
| 150 |
valueFunc = function() { |
| 151 |
read.dcf(system.file("DESCRIPTION", package = "shiny")) |
| 152 |
} |
| 153 |
) |
| 154 |
|
| 155 |
observe({ |
| 156 |
myfile() |
| 157 |
}) |
| 158 |
|
| 159 |
x_prom <- reactive({ |
| 160 |
# t0 |
| 161 |
x_span_id <- force(otel::get_active_span_context()$get_span_id()) |
| 162 |
# message("x_prom span id: ", x_span_id) |
| 163 |
x_val <- x() |
| 164 |
log_and_msg("x_prom init") |
| 165 |
p <- promises::promise(function(resolve, reject) { |
| 166 |
log_and_msg("x_prom 0") |
| 167 |
resolve(x_val) |
| 168 |
}) |
| 169 |
p <- promises::then(p, function(x_val) { |
| 170 |
log_and_msg("x_prom 1") |
| 171 |
log_and_msg("Launching mirai") |
| 172 |
x_val |
| 173 |
# mirai::mirai_map(seq_len(x_val), function(i) { |
| 174 |
# otel::start_local_active_span("slow compute") |
| 175 |
# Sys.sleep(i / 10 / 1000) |
| 176 |
# i |
| 177 |
# }) |> |
| 178 |
# promises::then(function(vals) { |
| 179 |
# max(unlist(vals)) |
| 180 |
# }) |
| 181 |
|
| 182 |
# mirai::mirai( |
| 183 |
# { |
| 184 |
# otel::start_local_active_span("slow compute") |
| 185 |
# # val |
| 186 |
# # Sys.sleep(0.2) |
| 187 |
# val |
| 188 |
# }, |
| 189 |
# val = x_val |
| 190 |
# ) |
| 191 |
}) |
| 192 |
p <- promises::then(p, function(x_val) { |
| 193 |
log_and_msg("x_prom 2") |
| 194 |
x_val |
| 195 |
}) |
| 196 |
p <- promises::then(p, function(x_val) { |
| 197 |
log_and_msg("x_prom 3") |
| 198 |
x_val |
| 199 |
}) |
| 200 |
}) |
| 201 |
|
| 202 |
y_prom <- reactive({ |
| 203 |
y_span_id <- force(otel::get_active_span_context()$get_span_id()) |
| 204 |
# message("y_prom span id: ", y_span_id) |
| 205 |
y_val <- y() |
| 206 |
log_and_msg("y_prom init") |
| 207 |
yp <- promises::promise(function(resolve, reject) { |
| 208 |
log_and_msg("y_prom 0") |
| 209 |
resolve(y_val) |
| 210 |
}) |
| 211 |
log_and_msg("make y_prom 1") |
| 212 |
yp <- promises::then(yp, function(y_val) { |
| 213 |
log_and_msg("y_prom 1") |
| 214 |
y_val |
| 215 |
}) |
| 216 |
log_and_msg("make y_prom 2") |
| 217 |
yp <- promises::then(yp, function(y_val) { |
| 218 |
log_and_msg("y_prom 2") |
| 219 |
y_val + calc() |
| 220 |
}) |
| 221 |
log_and_msg("make y_prom 3") |
| 222 |
yp <- promises::then(yp, function(y_val) { |
| 223 |
log_and_msg("y_prom 3") |
| 224 |
y_val |
| 225 |
}) |
| 226 |
|
| 227 |
log_and_msg( |
| 228 |
"done y_prom - ", |
| 229 |
getCurrentContext()$id, |
| 230 |
" - ", |
| 231 |
getCurrentContext()$.label |
| 232 |
) |
| 233 |
yp |
| 234 |
}) |
| 235 |
|
| 236 |
observe(label = "proms_observer", { |
| 237 |
p <- promises::promise_all( |
| 238 |
x_prom(), |
| 239 |
y_prom() |
| 240 |
) |
| 241 |
p <- promises::then(p, function(vals) { |
| 242 |
log_and_msg("Vals[1]: ", vals[[1]]) |
| 243 |
log_and_msg("Vals[2]: ", vals[[2]]) |
| 244 |
|
| 245 |
# cat(force) |
| 246 |
|
| 247 |
# Shut down the app so the telemetry can be seen easily |
| 248 |
if (vals[[1]] < 6) { |
| 249 |
updateSliderInput( |
| 250 |
"x", |
| 251 |
value = vals[[1]] + 1, |
| 252 |
session = session |
| 253 |
) |
| 254 |
} else { |
| 255 |
shutdown() |
| 256 |
} |
| 257 |
}) |
| 258 |
log_and_msg( |
| 259 |
"done proms_observer - ", |
| 260 |
getCurrentContext()$id, |
| 261 |
" - ", |
| 262 |
getCurrentContext()$.label |
| 263 |
) |
| 264 |
p |
| 265 |
}) |
| 266 |
|
| 267 |
# |> |
| 268 |
# bindOtel() |
| 269 |
|
| 270 |
# Set the value late in the reactive calc |
| 271 |
observeEvent( |
| 272 |
{ |
| 273 |
input$x |
| 274 |
}, |
| 275 |
{ |
| 276 |
rv$x <- input$x |
| 277 |
}, |
| 278 |
label = "singleObserveEvent" |
| 279 |
) |
| 280 |
|
| 281 |
tmp_val <- reactiveVal(NULL) |
| 282 |
|
| 283 |
# TODO: Not recording updates within the span!! |
| 284 |
x_calc <- eventReactive( |
| 285 |
{ |
| 286 |
isolate(tmp_val(1)) |
| 287 |
rv$x |
| 288 |
}, |
| 289 |
{ |
| 290 |
tmp_val(2) |
| 291 |
rv$x |
| 292 |
} |
| 293 |
) |
| 294 |
y_calc <- eventReactive( |
| 295 |
{ |
| 296 |
isolate(tmp_val(3)) |
| 297 |
input$y * 2 |
| 298 |
}, |
| 299 |
{ |
| 300 |
# x_calc() |
| 301 |
tmp_val(4) |
| 302 |
input$y * 2 / 2 |
| 303 |
} |
| 304 |
) |
| 305 |
# observeEvent(label = "set_y", { |
| 306 |
# rv$y <- input$y |
| 307 |
# }) |
| 308 |
observe(label = "set xVal", { |
| 309 |
x_calc() |
| 310 |
xVal(rv$x) |
| 311 |
}) |
| 312 |
observe(label = "set yVal", { |
| 313 |
yVal(y_calc()) |
| 314 |
}) |
| 315 |
}) |
| 316 |
} |
| 317 |
xMod("mymod") |
| 318 |
} |
| 319 |
) |
| 320 |
|
| 321 |
app |
| 322 |
} |