Learn R Programming

otel (version 0.2.0)

with_active_span: Evaluate R code with an active OpenTelemetry span

Description

Activates the span for evaluating an R expression.

Usually you need this function for spans created with start_span(), which does not activate the new span. Usually you don't need it for spans created with start_local_active_span(), because it activates the new span automatically.

Usage

with_active_span(span, expr, end_on_exit = FALSE)

Value

The return value of expr.

Arguments

span

The OpenTelemetry span to activate.

expr

R expression to evaluate.

end_on_exit

Whether to end after evaluating the R expression.

Details

After expr is evaluated (or an error occurs), the span is deactivated and the previously active span will be active again, if there was any.

It is possible to activate the same span for multiple R frames.

See Also

Other OpenTelemetry trace API: Zero Code Instrumentation, end_span(), is_tracing_enabled(), local_active_span(), start_local_active_span(), start_span(), tracing-constants

Other tracing for concurrent code: local_active_span()

Examples

Run this code
fun <- function() {
  # start span, do not activate
  spn <- otel::start_span("myfun")
  # do not leak resources
  on.exit(otel::end_span(spn), add = TRUE)
  myfun <- function() {
     otel::with_active_span(spn, {
       # create child span
       spn2 <- otel::start_local_active_span("myfun/2")
     })
  }

  myfun2 <- function() {
    otel::with_active_span(spn, {
      # create child span
      spn3 <- otel::start_local_active_span("myfun/3")
    })
  }
  myfun()
  myfun2()
  end_span(spn)
}
fun()

Run the code above in your browser using DataLab