Debian Codegraph @

tests/testthat/test-otel.R

raw
1 skip_on_cran()
2
3 skip_if_not_installed("otelsdk")
4
5 with_otel_promise_domain({
6 describe("OpenTelemetry integration", {
7 describe("otel::start_span()", {
8 it("creates spans appropriately", {
9 records <- otelsdk::with_otel_record({
10 span <- otel::start_span("test_span0", tracer = NULL)
11 expect_true(inherits(span, "otel_span"))
12 otel::end_span(span)
13 })
14 expect_true(!is.null(records$traces[["test_span0"]]))
15
16 skip_if(!Sys.getenv("OTEL_TRACES_EXPORTER") %in% c("", "none"))
17 expect_s3_class(
18 otel::start_span("test_span"),
19 "otel_span_noop"
20 )
21 })
22 })
23
24 describe("Deprecated functions", {
25 it("with_ospan_async() is deprecated", {
26 expect_snapshot({
27 with_ospan_async(
28 "test_span_deprecated",
29 {
30 42
31 },
32 tracer = promises_otel_tracer()
33 )
34 })
35 })
36
37 it("with_ospan_promise_domain() is deprecated", {
38 expect_snapshot({
39 with_ospan_promise_domain({
40 42
41 })
42 })
43 })
44
45 it("local_ospan_promise_domain() is deprecated", {
46 expect_snapshot({
47 local({
48 local_ospan_promise_domain()
49 42
50 })
51 })
52 })
53 })
54
55 describe("with_otel_span()", {
56 it("executes synchronous expressions without otel", {
57 # When recording is off, should still execute expression
58 result <- with_otel_span(
59 "test_span1",
60 {
61 42
62 },
63 tracer = promises_otel_tracer()
64 )
65 expect_equal(result, 42)
66 })
67 it("executes synchronous expressions", {
68 records <- otelsdk::with_otel_record({
69 result <- with_otel_span(
70 "test_span1",
71 {
72 42
73 },
74 tracer = promises_otel_tracer()
75 )
76 expect_equal(result, 42)
77 })
78 expect_true(!is.null(records$traces[["test_span1"]]))
79 })
80 it("executes asynchronous expressions without otel", {
81 # When recording is off, should still execute expression
82 result <- with_otel_span(
83 "test_span1",
84 {
85 promise_resolve(42)
86 },
87 tracer = promises_otel_tracer()
88 )
89 expect_true(is.promising(result))
90 expect_equal(extract(result), 42)
91 })
92
93 it("handles promise results", {
94 records <- otelsdk::with_otel_record({
95 result <- with_otel_span(
96 "test_span2",
97 {
98 promise_resolve(42)
99 },
100 tracer = promises_otel_tracer()
101 )
102
103 expect_true(is.promising(result))
104 expect_equal(extract(result), 42)
105 })
106 expect_true(!is.null(records$traces[["test_span2"]]))
107 })
108 })
109
110 describe("promise domain integration", {
111 it("integrates with existing promise domains", {
112 # Test domain composition works even when otel is not available
113 counting_domain <- create_counting_domain()
114
115 p <- with_promise_domain(counting_domain, {
116 with_otel_span("test_operation", tracer = promises_otel_tracer(), {
117 promise_resolve(42) |>
118 then(function(x) x * 2)
119 })
120 })
121
122 result <- extract(p)
123 expect_equal(result, 84)
124
125 # Verify the counting domain was active
126 expect_gte(counting_domain$counts$onFulfilledBound, 1L)
127 })
128 })
129
130 describe("error handling", {
131 it("handles promise rejections gracefully", {
132 # Should handle rejected promises without errors
133 records <- otelsdk::with_otel_record({
134 p <- with_otel_span("test_span3", tracer = promises_otel_tracer(), {
135 promise_reject("test error") |>
136 catch(function(reason) {
137 with_otel_span("catch_span3", tracer = promises_otel_tracer(), {
138 42
139 })
140
141 expect_true(inherits(reason, "error"))
142 expect_match(as.character(reason), "test error")
143
144 "caught"
145 }) |>
146 then(function(result) {
147 expect_equal(result, "caught")
148 with_otel_span("then_span3", tracer = promises_otel_tracer(), {
149 42
150 })
151 })
152 })
153 wait_for_it(p)
154 p
155 })
156 expect_true(is.promising(p))
157
158 # Should be able to catch the error
159
160 test_id <- records$traces[["test_span3"]]$span_id
161 expect_equal(records$traces[["catch_span3"]]$parent, test_id)
162 expect_equal(records$traces[["then_span3"]]$parent, test_id)
163 })
164
165 it("propagates regular errors", {
166 # Regular errors should still propagate
167 expect_error(
168 with_otel_span("test_span4", tracer = promises_otel_tracer(), {
169 stop("regular error")
170 }),
171 "regular error"
172 )
173 })
174 })
175
176 it("maintains span context across parallel promise chains", {
177 # Track execution order and span context
178 execution_order <- character(0)
179
180 # Compare execution order after making `recording`
181 # Compare span parents using `recording`
182
183 recording <- otelsdk::with_otel_record({
184 # Create two parallel promise chains, each with their own span
185 chain1 <- with_otel_span("chain_1", tracer = promises_otel_tracer(), {
186 promise_resolve("init1") |>
187 then(function(x) {
188 spn <- otel::start_span("chain1_step1")
189 on.exit(otel::end_span(spn))
190 execution_order <<- c(execution_order, "chain1_step1")
191 paste0(x, "_step11")
192 }) |>
193 then(function(x) {
194 spn <- otel::start_span("chain1_step2")
195 on.exit(otel::end_span(spn))
196 execution_order <<- c(execution_order, "chain1_step2")
197 paste0(x, "_step12")
198 }) |>
199 then(function(x) {
200 spn <- otel::start_span("chain1_step3")
201 on.exit(otel::end_span(spn))
202 execution_order <<- c(execution_order, "chain1_step3")
203 paste0(x, "_step13")
204 }) |>
205 then(function(x) {
206 spn <- otel::start_span("chain1_step4")
207 on.exit(otel::end_span(spn))
208 execution_order <<- c(execution_order, "chain1_step4")
209 paste0(x, "_final1")
210 })
211 })
212
213 chain2 <- with_otel_span("chain_2", tracer = promises_otel_tracer(), {
214 promise_resolve("init2") |>
215 then(function(x) {
216 spn <- otel::start_span("chain2_step1")
217 on.exit(otel::end_span(spn))
218 execution_order <<- c(execution_order, "chain2_step1")
219 paste0(x, "_step12")
220 }) |>
221 then(function(x) {
222 spn <- otel::start_span("chain2_step2")
223 on.exit(otel::end_span(spn))
224 execution_order <<- c(execution_order, "chain2_step2")
225 paste0(x, "_step22")
226 }) |>
227 then(function(x) {
228 spn <- otel::start_span("chain2_step3")
229 on.exit(otel::end_span(spn))
230 execution_order <<- c(execution_order, "chain2_step3")
231 paste0(x, "_step23")
232 }) |>
233 then(function(x) {
234 spn <- otel::start_span("chain2_step4")
235 on.exit(otel::end_span(spn))
236 execution_order <<- c(execution_order, "chain2_step4")
237 paste0(x, "_final2")
238 })
239 })
240
241 # Wait for both chains to complete
242 result1 <- extract(chain1)
243 result2 <- extract(chain2)
244
245 # Verify final recording
246 expect_equal(result1, "init1_step11_step12_step13_final1")
247 expect_equal(result2, "init2_step12_step22_step23_final2")
248
249 # Check that execution alternates between chains
250 expected_alternating_pattern <- c(
251 "chain1_step1",
252 "chain2_step1",
253 "chain1_step2",
254 "chain2_step2",
255 "chain1_step3",
256 "chain2_step3",
257 "chain1_step4",
258 "chain2_step4"
259 )
260
261 expect_equal(
262 execution_order,
263 expected_alternating_pattern,
264 info = "Execution should alternate between chains"
265 )
266 })
267
268 # * Verify spans at each step have the same parent, even though the
269 # previous calculation may have been a different promise domain.
270 # * All steps in chain1 should have same parent span, all in chain2 should
271 # have same parent span.
272 chain1_id <- recording$traces[["chain_1"]]$span_id
273 for (name in c(
274 "chain1_step1",
275 "chain1_step2",
276 "chain1_step3",
277 "chain1_step4"
278 )) {
279 expect_equal(recording$traces[[name]]$parent, chain1_id)
280 }
281
282 chain2_id <- recording$traces[["chain_2"]]$span_id
283 for (name in c(
284 "chain2_step1",
285 "chain2_step2",
286 "chain2_step3",
287 "chain2_step4"
288 )) {
289 expect_equal(recording$traces[[name]]$parent, chain2_id)
290 }
291 })
292 })
293 })
294
295 describe("local_otel_promise_domain()", {
296 it("sets up otel span promise domain for local scope", {
297 # Test that the domain is active within the local scope
298 original_domain <- current_promise_domain()
299
300 local({
301 local_otel_promise_domain()
302 current_domain <- current_promise_domain()
303
304 # Should have an otel span domain set
305 expect_false(identical(current_domain, original_domain))
306 expect_true(has_otel_promise_domain(current_domain))
307 })
308
309 # Should be restored after exiting the local scope
310 expect_identical(current_promise_domain(), original_domain)
311 })
312
313 it("restores previous domain when scope exits", {
314 counting_domain <- create_counting_domain()
315
316 expect_equal(current_promise_domain(), NULL)
317
318 with_promise_domain(counting_domain, {
319 domain_before <- current_promise_domain()
320
321 local({
322 local_otel_promise_domain()
323 current_domain <- current_promise_domain()
324
325 # Should be a composed domain with otel span support
326 expect_false(identical(current_domain, domain_before))
327 expect_true(has_otel_promise_domain(current_domain))
328 })
329
330 # Should restore the counting domain
331 expect_identical(current_promise_domain(), domain_before)
332 })
333
334 expect_equal(current_promise_domain(), NULL)
335 })
336
337 # it("works with custom environment", {
338 # test_env <- new.env()
339 # original_domain <- current_promise_domain()
340
341 # # Set up domain in custom environment
342 # local_otel_promise_domain(envir = test_env)
343
344 # # Domain should still be active since test_env hasn't been cleaned
345 # current_domain <- current_promise_domain()
346 # expect_false(identical(current_domain, original_domain))
347 # expect_true(has_otel_promise_domain(current_domain))
348
349 # # Clean up the environment to trigger restoration
350 # rm(list = ls(test_env), envir = test_env)
351
352 # # Force garbage collection to ensure cleanup handlers run
353 # gc()
354
355 # # Domain should be restored (though this timing is implementation dependent)
356 # # So we mainly test that the function doesn't error with custom envir
357 # expect_true(TRUE)
358 # })
359
360 it("integrates with promise execution", {
361 records <- otelsdk::with_otel_record({
362 result <- local({
363 local_otel_promise_domain()
364
365 otel::start_local_active_span("outer_test_span")
366
367 # Create a promise within the otel span domain
368 promise_resolve(21) |>
369 then(function(x) {
370 # This should be executed within the otel span domain
371 span <- otel::start_span("inner_test_span")
372 on.exit(otel::end_span(span))
373 x * 2
374 })
375 })
376
377 wait_for_it(result)
378 result
379 })
380
381 expect_true(is.promising(result))
382 expect_equal(extract(result), 42)
383 expect_true(!is.null(records$traces[["outer_test_span"]]))
384 expect_true(!is.null(records$traces[["inner_test_span"]]))
385 expect_equal(
386 records$traces[["inner_test_span"]]$parent,
387 records$traces[["outer_test_span"]]$span_id
388 )
389 })
390
391 it("can be nested without issues", {
392 # Track how many times promise domain setup occurs
393 domain_creation_count <- 0
394 original_copd <- create_otel_promise_domain
395
396 # Mock the domain creation to count calls
397 with_mocked_bindings(
398 create_otel_promise_domain = function() {
399 domain_creation_count <<- domain_creation_count + 1
400 original_copd()
401 },
402 {
403 original_domain <- current_promise_domain()
404
405 local({
406 local_otel_promise_domain()
407 expect_equal(domain_creation_count, 1)
408 domain1 <- current_promise_domain()
409
410 local({
411 # This should not create another domain due to idempotency
412 local_otel_promise_domain()
413 expect_equal(domain_creation_count, 1) # Still 1, not 2
414 domain2 <- current_promise_domain()
415
416 # Both should have otel span domains and should be identical
417 expect_true(has_otel_promise_domain(domain1))
418 expect_true(has_otel_promise_domain(domain2))
419 expect_identical(domain1, domain2) # Should be the same due to idempotency
420 })
421
422 # Should still be domain1 after nested scope exits
423 expect_identical(current_promise_domain(), domain1)
424 })
425
426 # Should restore to original after all scopes exit
427 # Note: This may not be immediate due to deferred cleanup
428 expect_equal(domain_creation_count, 1) # Only created once total
429 }
430 )
431 })
432 })
433
434 describe("has_otel_promise_domain()", {
435 it("returns FALSE for NULL domain", {
436 expect_false(has_otel_promise_domain(NULL))
437 })
438
439 it("returns FALSE for empty list", {
440 expect_false(has_otel_promise_domain(list()))
441 })
442
443 it("returns FALSE for regular promise domain", {
444 regular_domain <- new_promise_domain(
445 wrapOnFulfilled = function(onFulfilled) onFulfilled
446 )
447 expect_false(has_otel_promise_domain(regular_domain))
448 })
449
450 it("returns TRUE for otel span promise domain", {
451 otel_span_domain <- create_otel_promise_domain()
452 expect_true(has_otel_promise_domain(otel_span_domain))
453 })
454
455 it("returns TRUE for composed domain with otel span", {
456 counting_domain <- create_counting_domain()
457 otel_span_domain <- create_otel_promise_domain()
458 composed_domain <- compose_domains(counting_domain, otel_span_domain)
459
460 expect_true(has_otel_promise_domain(composed_domain))
461 })
462
463 it("returns FALSE for composed domain without otel span", {
464 counting_domain1 <- create_counting_domain()
465 counting_domain2 <- create_counting_domain()
466 composed_domain <- compose_domains(counting_domain1, counting_domain2)
467
468 expect_false(has_otel_promise_domain(composed_domain))
469 })
470
471 it("uses current_promise_domain() when no argument provided", {
472 # Initially no domain
473 expect_false(has_otel_promise_domain())
474
475 # With otel span domain active
476 with_otel_promise_domain({
477 expect_true(has_otel_promise_domain())
478 })
479
480 # Back to no domain
481 expect_false(has_otel_promise_domain())
482 })
483
484 it("works with various data types", {
485 # # Test edge cases
486 # expect_false(has_otel_promise_domain(character(0)))
487 # expect_false(has_otel_promise_domain(numeric(0)))
488 # expect_false(has_otel_promise_domain("not a domain"))
489 # expect_false(has_otel_promise_domain(42))
490
491 # Test list with wrong flag
492 fake_domain <- list(.some_other_flag = TRUE)
493 expect_false(has_otel_promise_domain(fake_domain))
494
495 # Test list with correct flag
496 correct_domain <- list(.otel_promise_domain = TRUE)
497 expect_true(has_otel_promise_domain(correct_domain))
498
499 # Test list with correct flag but FALSE value
500 false_domain <- list(.otel_promise_domain = FALSE)
501 expect_false(has_otel_promise_domain(false_domain))
502 })
503 })
504
505 describe("with_otel_promise_domain() idempotency", {
506 it("is idempotent when called multiple times", {
507 # Track how many times promise domain setup occurs
508 domain_creation_count <- 0
509 reset_count <- function() {
510 domain_creation_count <<- 0
511 }
512 original_copd <- create_otel_promise_domain
513
514 expect_false(has_otel_promise_domain())
515
516 # Mock the domain creation to count calls
517 with_mocked_bindings(
518 create_otel_promise_domain = function() {
519 domain_creation_count <<- domain_creation_count + 1
520 original_copd()
521 },
522 {
523 # First call should create the domain
524 result1 <- with_otel_promise_domain({
525 42
526 })
527 expect_equal(result1, 42)
528 expect_equal(domain_creation_count, 1)
529
530 reset_count()
531
532 result2 <-
533 with_otel_promise_domain({
534 expect_equal(domain_creation_count, 1)
535
536 # Nested calls should not create additional domains
537 with_otel_promise_domain({
538 with_otel_promise_domain({
539 expect_equal(domain_creation_count, 1)
540
541 84
542 })
543 })
544 })
545 expect_equal(result2, 84)
546 # Should still be 1, even with two prom domain calls
547 expect_equal(domain_creation_count, 1)
548 }
549 )
550 })
551
552 it("allows nested calls without duplicate domain setup", {
553 # Test that nested with_otel_promise_domain calls work correctly
554 # and don't interfere with each other
555
556 records <- otelsdk::with_otel_record({
557 result <- with_otel_promise_domain({
558 with_otel_span("outer_span", tracer = promises_otel_tracer(), {
559 # This nested call should be idempotent
560 with_otel_promise_domain({
561 with_otel_span("inner_span", tracer = promises_otel_tracer(), {
562 promise_resolve(42) |>
563 then(function(x) {
564 # Another nested call
565 with_otel_promise_domain({
566 x * 2
567 })
568 })
569 })
570 })
571 })
572 })
573
574 wait_for_it(result)
575 result
576 })
577
578 expect_true(is.promising(result))
579 expect_equal(extract(result), 84)
580
581 # Verify spans were created correctly
582 expect_true(!is.null(records$traces[["outer_span"]]))
583 expect_true(!is.null(records$traces[["inner_span"]]))
584
585 # Verify parent-child relationship
586 outer_id <- records$traces[["outer_span"]]$span_id
587 expect_equal(records$traces[["inner_span"]]$parent, outer_id)
588 })
589 })