Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ jobs:

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::rcmdcheck
extra-packages: |
any::rcmdcheck
otelsdk=?ignore-before-r=4.3.0
needs: check

- uses: r-lib/actions/check-r-package@v2
Expand Down
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Suggests:
digest (>= 0.6.33),
gh,
knitr,
otel,
otelsdk,
rmarkdown,
rstudioapi,
S7,
Expand Down
42 changes: 42 additions & 0 deletions R/otel.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
otel_tracer_name <- "org.r-lib.testthat"

# generic otel helpers ---------------------------------------------------------

otel_cache_tracer <- NULL
otel_local_active_span <- NULL

local({
otel_is_tracing <- FALSE
otel_tracer <- NULL

otel_cache_tracer <<- function() {
requireNamespace("otel", quietly = TRUE) || return()
otel_tracer <<- otel::get_tracer(otel_tracer_name)
otel_is_tracing <<- tracer_enabled(otel_tracer)
}

otel_local_active_span <<- function(
name,
label,
scope = parent.frame()
) {
otel_is_tracing || return()
otel::start_local_active_span(
sprintf("%s %s", name, label),
tracer = otel_tracer,
activation_scope = scope
)
}
})

tracer_enabled <- function(tracer) {
.subset2(tracer, "is_enabled")()
}

with_otel_record <- function(expr) {
on.exit(otel_cache_tracer())
otelsdk::with_otel_record({
otel_cache_tracer()
expr
})
}
1 change: 1 addition & 0 deletions R/test-that.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ test_code <- function(code, env, reporter = NULL, skip_on_empty = TRUE) {

test <- test_description()
if (!is.null(test)) {
otel_local_active_span("test that", test, scope = frame)
reporter$start_test(context = reporter$.context, test = test)
withr::defer(reporter$end_test(context = reporter$.context, test = test))
}
Expand Down
8 changes: 8 additions & 0 deletions R/testthat-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@ the$in_check_reporter <- FALSE
#' @importFrom lifecycle deprecated
## usethis namespace: end
NULL

# nocov start

.onLoad <- function(libname, pkgname) {
otel_cache_tracer()
}

# nocov end
15 changes: 15 additions & 0 deletions tests/testthat/test-otel.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
test_that("otel instrumentation works", {
skip_if_not_installed("otelsdk")

record <- with_otel_record({
test_that("otel testing", {
expect_equal(1, 1)
expect_error(stop("otel error"))
})
})

traces <- record$traces
expect_length(traces, 1L)
expect_equal(traces[[1L]]$name, "test that otel instrumentation works / otel testing")
expect_equal(traces[[1L]]$instrumentation_scope$name , "org.r-lib.testthat")
})