Debian Codegraph @

tests/testthat/test-otel-error.R

raw
1 skip_on_cran()
2 skip_if_not_installed("otelsdk")
3
4 create_mock_session <- function() {
5 session <- MockShinySession$new()
6 session$token <- "test-session-token"
7 session
8 }
9
10 expect_session_warning <- function(session, warning) {
11 testthat::expect_warning(
12 capture.output(
13 type = "message",
14 {
15 session$flushReact()
16 }
17 ),
18 warning
19 )
20 }
21
22 exception_trace_events <- function(traces) {
23 unlist(lapply(traces, function(trace) {
24 if (is.null(trace$events)) return(list())
25 events <- Filter(function(event) {
26 !is.null(event$attributes) &&
27 !is.null(event$attributes[["exception.message"]])
28 }, trace$events)
29 events
30 }), recursive = FALSE)
31 }
32
33 test_server_with_otel_error <- function(session, server, expr, sanitize = FALSE, args = list()) {
34 stopifnot(inherits(session, "MockShinySession"))
35 stopifnot(is.function(server))
36
37 traces <- with_shiny_otel_record({ 42 })$traces
38 expect_length(traces, 0)
39
40 withr::with_options(
41 list(
42 shiny.otel.collect = "all",
43 shiny.otel.sanitize.errors = sanitize
44 ),
45 {
46 info <- with_shiny_otel_record({
47 # rlang quosure magic to capture and pass through `expr`
48 testServer(server, {{ expr }}, args = args, session = session)
49 })
50 }
51 )
52
53 info$traces
54 }
55
56
57 test_that("mark_otel_exception_as_seen() returns modified condition", {
58 cnd <- simpleError("test error")
59 result <- mark_otel_exception_as_seen(cnd)
60
61 expect_true(inherits(result, "error"))
62 expect_true(inherits(result, "condition"))
63 expect_equal(conditionMessage(result), "test error")
64 expect_true(isTRUE(result$.shiny_otel_exception))
65 })
66
67 test_that("mark_otel_exception_as_seen() marks error as seen", {
68 cnd <- simpleError("test error")
69 expect_false(has_seen_otel_exception(cnd))
70
71 cnd <- mark_otel_exception_as_seen(cnd)
72 expect_true(has_seen_otel_exception(cnd))
73 })
74
75
76 test_that("set_otel_exception_status() records sanitized errors by default", {
77 server <- function(input, output, session) {
78 r1 <- reactive(label = "r1", {
79 stop("test error in r1")
80 })
81
82 r2 <- reactive(label = "r2", {
83 r1()
84 })
85
86 observe(label = "obs", {
87 r2()
88 })
89 }
90
91 session <- create_mock_session()
92 traces <- test_server_with_otel_error(
93 sanitize = NULL,
94 session,
95 server,
96 {
97 # Expect an error to be thrown as warning
98 expect_session_warning(session, "test error in r1")
99 }
100 )
101
102 # IDK why, I don't have time to debug
103 skip_if(length(traces) > 3, "Too many traces collected; otelsdk traces are polluted. Run in single test file only: testthat::test_file(testthat::test_path('test-otel-error.R'))`")
104
105 # Find traces with exception events (should only be one)
106 exception_events <- exception_trace_events(traces)
107
108 # Exception should be recorded only once at the original point of failure
109 expect_equal(length(exception_events), 1)
110 expect_match(
111 exception_events[[1]]$attributes[["exception.message"]],
112 "Check your logs or contact the app author for clarification."
113 )
114 })
115
116 test_that("set_otel_exception_status() records exception only once in reactive context", {
117 server <- function(input, output, session) {
118 r1 <- reactive(label = "r1", {
119 stop("test error in r1")
120 })
121
122 r2 <- reactive(label = "r2", {
123 r1()
124 })
125
126 observe(label = "obs", {
127 r2()
128 })
129 }
130
131 session <- create_mock_session()
132 traces <- test_server_with_otel_error(session, server, {
133 # Expect an error to be thrown as warning
134 expect_session_warning(session, "test error in r1")
135 })
136
137 # Find traces with error status
138 for (trace in traces) {
139 expect_equal(trace$status, "error")
140 }
141
142 # Find traces with exception events (should only be one)
143 exception_events <- exception_trace_events(traces)
144
145 # Exception should be recorded only once at the original point of failure
146 expect_equal(length(exception_events), 1)
147 expect_match(
148 exception_events[[1]]$attributes[["exception.message"]],
149 "test error in r1"
150 )
151 })
152
153 test_that("set_otel_exception_status() records exception for multiple independent errors", {
154 server <- function(input, output, session) {
155 r1 <- reactive(label = "r1", {
156 stop("error in r1")
157 })
158
159 r2 <- reactive(label = "r2", {
160 stop("error in r2")
161 })
162
163 observe(label = "obs1", {
164 r1()
165 })
166
167 observe(label = "obs2", {
168 r2()
169 })
170 }
171
172 session <- create_mock_session()
173 traces <- test_server_with_otel_error(session, server, {
174 # Both observers should error
175 expect_session_warning(session, "error in r1")
176 })
177
178 # Find traces with exception events
179 exception_events <- exception_trace_events(traces)
180
181 # Each unique error should be recorded once
182 expect_gte(length(exception_events), 1)
183 })
184
185 test_that("set_otel_exception_status() does not record shiny.custom.error", {
186 server <- function(input, output, session) {
187 r <- reactive(label = "r", {
188 cnd <- simpleError("custom error")
189 class(cnd) <- c("shiny.custom.error", class(cnd))
190 stop(cnd)
191 })
192
193 observe(label = "obs", {
194 r()
195 })
196 }
197
198 session <- create_mock_session()
199 traces <- test_server_with_otel_error(session, server, {
200 expect_session_warning(session, "custom error")
201 })
202
203 # Find traces with error status (should be none for custom errors)
204 for (trace in traces) {
205 expect_true(trace$status != "error")
206 }
207 })
208
209 test_that("set_otel_exception_status() does not record shiny.silent.error", {
210 server <- function(input, output, session) {
211 r <- reactive(label = "r", {
212 cnd <- simpleError("silent error")
213 class(cnd) <- c("shiny.silent.error", class(cnd))
214 stop(cnd)
215 })
216
217 observe(label = "obs", {
218 r()
219 })
220 }
221
222 session <- create_mock_session()
223 traces <- test_server_with_otel_error(session, server, {
224 expect_no_error(session$flushReact())
225 })
226
227 # Find traces with error status (should be none for silent errors)
228 for (trace in traces) {
229 expect_true(trace$status != "error")
230 }
231 })