Debian Codegraph @

R/otel-shiny.R

raw
1 # Used by otel to identify the tracer and logger for this package
2 # https://github.com/r-lib/otel/blob/afc31bc1f4bd177870d44b051ada1d9e4e685346/R/tracer-name.R#L33-L49
3 # DO NOT CHANGE THIS VALUE without understanding the implications for existing telemetry data!
4 otel_tracer_name <- "co.posit.r-package.shiny"
5
6 init_otel <- function() {
7 .globals$otel_tracer <- otel::get_tracer()
8 .globals$otel_is_tracing_enabled <- otel::is_tracing_enabled(.globals$otel_tracer)
9
10 .globals$otel_logger <- otel::get_logger()
11 # .globals$otel_is_logging_enabled <- otel::is_logging_enabled()
12 }
13 on_load({init_otel()})
14
15 #' Run expr within a Shiny OpenTelemetry recording context
16 #'
17 #' Reset the OpenTelemetry tracer and logger for Shiny.
18 #' Used for testing purposes only.
19 #' @param expr Expression to evaluate within the recording context
20 #' @return The result of evaluating `otelsdk::with_otel_record(expr)` with freshly enabled Shiny otel tracer and logger
21 #' @noRd
22 with_shiny_otel_record <- function(expr) {
23 # Only use within internal testthat tests
24 stopifnot(testthat__is_testing())
25 withr::defer({ init_otel() })
26
27 otelsdk::with_otel_record({
28 init_otel()
29
30 force(expr)
31 })
32 }
33
34 #' Check if OpenTelemetry tracing is enabled
35 #'
36 #' @param tracer The OpenTelemetry tracer to check (default: Shiny otel tracer)
37 #' @return `TRUE` if tracing is enabled, `FALSE` otherwise
38 #' @noRd
39 otel_is_tracing_enabled <- function() {
40 .globals[["otel_is_tracing_enabled"]]
41 }
42
43 #' Shiny OpenTelemetry logger
44 #'
45 #' Used for logging OpenTelemetry events via `otel_log()`
46 #' @return An OpenTelemetry logger
47 #' @noRd
48 shiny_otel_logger <- function() {
49 .globals[["otel_logger"]]
50 }
51
52
53
54 #' Shiny OpenTelemetry tracer
55 #'
56 #' Used for creating OpenTelemetry spans via `with_otel_span()` and
57 #' `start_otel_span()`
58 #'
59 #' Inspired by httr2:::get_tracer().
60 #' @return An OpenTelemetry tracer
61 #' @noRd
62 shiny_otel_tracer <- function() {
63 .globals[["otel_tracer"]]
64 }
65
66
67
68
69 #' Create and use a Shiny OpenTelemetry span
70 #'
71 #' If otel is disabled, the span will not be created,
72 #' however the expression will still be evaluated.
73 #' @param name Span name
74 #' @param expr Expression to evaluate within the span
75 #' @param ... Ignored
76 #' @param attributes Optional span attributes
77 #' @return The result of evaluating `expr`
78 #' @noRd
79 with_otel_span <- function(name, expr, ..., attributes = NULL) {
80 promises::with_otel_span(name, expr, ..., attributes = attributes, tracer = shiny_otel_tracer())
81 }
82
83
84 #' Start a Shiny OpenTelemetry span
85 #'
86 #' @param name Span name
87 #' @param ... Additional arguments passed to `otel::start_span()`
88 #' @return An OpenTelemetry span
89 #' @noRd
90 start_otel_span <- function(name, ...) {
91 otel::start_span(name, ..., tracer = shiny_otel_tracer())
92 }
93
94
95 # # TODO: Set attributes on the current active span
96 # # 5. Set attributes on the current active span
97 # set_otel_span_attrs(status = 200L)
98
99
100 # -- Helpers --------------------------------------------------------------
101
102
103 is_otel_span <- function(x) {
104 inherits(x, "otel_span")
105 }
106
107 testthat__is_testing <- function() {
108 # testthat::is_testing()
109 identical(Sys.getenv("TESTTHAT"), "true")
110 }
111
112 #' Log a message using the Shiny OpenTelemetry logger
113 #'
114 #' @param msg The log message
115 #' @param ... Additional attributes to add to the log record
116 #' @param severity The log severity level (default: "info")
117 #' @param logger The OpenTelemetry logger to use (default: Shiny otel logger)
118 #' @return Invisibly returns.
119 #' @noRd
120 otel_log <- function(
121 msg,
122 ...,
123 severity = "info",
124 logger = shiny_otel_logger()
125 ) {
126 otel::log(msg, ..., severity = severity, logger = logger)
127 }