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 @@ -53,7 +53,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
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Suggests:
covr,
httpuv,
knitr,
otel,
otelsdk,
rmarkdown,
sodium,
spelling,
Expand All @@ -49,4 +51,4 @@ Config/testthat/edition: 3
Encoding: UTF-8
Language: en-US
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2.9000
RoxygenNote: 7.3.3
53 changes: 53 additions & 0 deletions R/otel.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
otel_tracer_name <- "org.r-lib.gargle"

otel_cache_tracer <- NULL
otel_local_active_span <- NULL
otel_span_add_events <- 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,
attributes = list(),
links = NULL,
options = NULL,
return_ctx = FALSE,
scope = parent.frame()
) {
otel_is_tracing || return()
otel::start_local_active_span(
name,
attributes = otel::as_attributes(attributes),
links = links,
options = options,
tracer = otel_tracer,
activation_scope = scope
)
}

otel_span_add_events <<- function(text) {
otel_is_tracing || return()
spn <- otel::get_active_span()
lapply(cli::ansi_strip(text), spn$add_event)
}
})

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/token_fetch.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#' token_fetch(scopes = "https://www.googleapis.com/auth/userinfo.email")
#' }
token_fetch <- function(scopes = NULL, ...) {
gargle_span <- otel_local_active_span("token fetch")
gargle_debug("trying {.fun token_fetch}")
for (f in gargle_env$cred_funs) {
token <- NULL
Expand Down
3 changes: 3 additions & 0 deletions R/utils-ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ with_gargle_verbosity <- function(level, code) {
}

gargle_debug <- function(text, .envir = caller_env()) {
otel_span_add_events(
cli::cli_fmt(cli::cli_bullets(text, .envir = .envir))
)
if (gargle_verbosity() == "debug") {
cli::cli_div(theme = gargle_theme())
cli::cli_bullets(text, .envir = .envir)
Expand Down
1 change: 1 addition & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.onLoad <- function(lib, pkg) {
# nocov start
otel_cache_tracer()
cred_funs_set_default()
invisible()
} # nocov end