Skip to content

Commit efbf819

Browse files
committed
Update otel tracer caching implementation
1 parent 0a2deba commit efbf819

File tree

2 files changed

+39
-47
lines changed

2 files changed

+39
-47
lines changed

R/otel.R

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,48 @@
11
otel_tracer_name <- "org.r-lib.testthat"
2-
otel_is_tracing <- FALSE
3-
otel_tracer <- NULL
42

53
# generic otel helpers ---------------------------------------------------------
64

7-
# nocov start
8-
9-
otel_cache_tracer <- function() {
10-
requireNamespace("otel", quietly = TRUE) || return()
11-
otel_tracer <<- otel::get_tracer(otel_tracer_name)
12-
otel_is_tracing <<- tracer_enabled(otel_tracer)
13-
}
14-
15-
# nocov end
5+
otel_cache_tracer <- NULL
6+
otel_local_active_span <- NULL
7+
8+
local({
9+
otel_is_tracing <- FALSE
10+
otel_tracer <- NULL
11+
12+
otel_cache_tracer <<- function() {
13+
requireNamespace("otel", quietly = TRUE) || return()
14+
otel_tracer <<- otel::get_tracer(otel_tracer_name)
15+
otel_is_tracing <<- tracer_enabled(otel_tracer)
16+
}
17+
18+
otel_local_active_span <<- function(
19+
name,
20+
label,
21+
attributes = list(),
22+
links = NULL,
23+
options = NULL,
24+
scope = parent.frame()
25+
) {
26+
otel_is_tracing || return()
27+
spn <- otel::start_local_active_span(
28+
sprintf("%s %s", name, label),
29+
attributes = otel::as_attributes(attributes),
30+
links = links,
31+
options = options,
32+
tracer = otel_tracer,
33+
activation_scope = scope
34+
)
35+
}
36+
})
1637

1738
tracer_enabled <- function(tracer) {
1839
.subset2(tracer, "is_enabled")()
1940
}
2041

21-
otel_refresh_tracer <- function() {
22-
requireNamespace("otel", quietly = TRUE) || return()
23-
tracer <- otel::get_tracer()
24-
modify_binding(
25-
topenv(),
26-
list(otel_tracer = tracer, otel_is_tracing = tracer_enabled(tracer))
27-
)
28-
}
29-
30-
modify_binding <- function(env, lst) {
31-
lapply(names(lst), unlockBinding, env)
32-
list2env(lst, envir = env)
33-
lapply(names(lst), lockBinding, env)
34-
}
35-
36-
otel_local_active_span <- function(
37-
name,
38-
label,
39-
attributes = list(),
40-
links = NULL,
41-
options = NULL,
42-
scope = parent.frame()
43-
) {
44-
otel_is_tracing || return()
45-
spn <- otel::start_local_active_span(
46-
sprintf("%s %s", name, label),
47-
attributes = otel::as_attributes(attributes),
48-
links = links,
49-
options = options,
50-
tracer = otel_tracer,
51-
activation_scope = scope
52-
)
42+
with_otel_record <- function(expr) {
43+
on.exit(otel_cache_tracer())
44+
otelsdk::with_otel_record({
45+
otel_cache_tracer()
46+
expr
47+
})
5348
}

tests/testthat/test-otel.R

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
test_that("otel instrumentation works", {
22
skip_if_not_installed("otelsdk")
33

4-
record <- otelsdk::with_otel_record({
5-
otel_refresh_tracer()
4+
record <- with_otel_record({
65
test_that("otel testing", {
76
expect_equal(1, 1)
87
expect_error(stop("otel error"))
98
})
109
})
11-
# reset tracer after tests
12-
otel_refresh_tracer()
1310

1411
traces <- record$traces
1512
expect_length(traces, 1L)

0 commit comments

Comments
 (0)