% Generated by roxygen2: do not edit by hand % Please edit documentation in R/otel.R \name{with_otel_span} \alias{with_otel_span} \alias{with_otel_promise_domain} \alias{local_otel_promise_domain} \title{OpenTelemetry integration} \usage{ with_otel_span(name, expr, ..., tracer, attributes = NULL) with_otel_promise_domain(expr) local_otel_promise_domain(envir = parent.frame()) } \arguments{ \item{name}{Character string. The name of the otel span.} \item{expr}{An expression to evaluate within the otel span context.} \item{...}{Additional arguments passed to \code{\link[otel:start_span]{otel::start_span()}}.} \item{tracer}{(Required) An \code{{otel}} tracer. It is required to provide your own tracer from your own package. See \code{\link[otel:get_tracer]{otel::get_tracer()}} for more details.} \item{attributes}{Attributes passed through \code{\link[otel:as_attributes]{otel::as_attributes()}} (when not \code{NULL})} \item{envir}{The "local" environment in which to add the promise domain. When the environment is exited, the promise domain is removed.} } \description{ \pkg{otel} provides tools for integrating with OpenTelemetry, a framework for observability and tracing in distributed systems. These methods are intended to enhance the framework to be used with the \pkg{promises} package, not as a generic replacement. Developer note - Barret 2025/09: This otel span handoff promise domain topic is complex and has been discussed over many hours. Many advanced Shiny/R developers are not even aware of promise domains (very reasonable!), therefore this topic requires more in-depth documentation and examples. } \section{Functions}{ \itemize{ \item \code{with_otel_span()}: Creates an OpenTelemetry span, executes the given expression within it, and ends the span. This method \strong{requires} the use of \code{with_otel_promise_domain()} to be within the execution stack. This function is designed to handle both synchronous and asynchronous (promise-based) operations. For promises, the span is automatically ended when the promise resolves or rejects. Returns the result of evaluating \code{expr}. If \code{expr} returns a promise, the span will be automatically ended when the promise completes. This function differs from synchronous otel span operations in that it installs a promise domain and properly handles asynchronous operations. In addition, the internal span will be ended either when the function exits (for synchronous operations) or when a returned promise completes (for asynchronous operations). If OpenTelemetry is not enabled, the expression will be evaluated without any tracing context. \item \code{with_otel_promise_domain()}: Adds an idempotent handoff Active OpenTelemetry span promise domain. Package authors are required to use this function to have otel span context persist across asynchronous boundaries. This method is only needed once per promise domain stack. If you are unsure, feel free to call \code{with_otel_promise_domain()} as the underlying promise domain will only be added if not found within the current promise domain stack. If your package \strong{only} works within Shiny apps, Shiny will have already added the domain so no need to add it yourself. If your package works outside of Shiny and you use \code{{promises}} (i.e. \code{{chromote}}), then you'll need to use this wrapper method. This method adds a \emph{handoff} Active OpenTelemetry span promise domain to the expression evaluation. This \emph{handoff} promise domain will only run once on reactivation. This is critical if there are many layered \code{with_otel_span()} calls, such as within Shiny reactivity. For example, if we nested many \code{with_otel_span()} calls of which each call added a promise domain that reactivated each otel span on restore, we'd reactivate \code{k} otel span objects (\code{O(k)}) when we only need to activate the \strong{last} span (\code{O(1)}). Returns the result of evaluating \code{expr} within the otel span promise domain. \item \code{local_otel_promise_domain()}: Local OpenTelemetry span promise domain Adds an OpenTelemetry span promise domain to the local scope. This is useful for \code{{coro}} operations where encapsulating the coro operations inside a \verb{with_*()} methods is not allowed. When not using \code{{coro}}, please prefer to use \code{with_otel_span()} or \code{with_otel_promise_domain()}. }} \section{Definitions}{ \itemize{ \item Promise domain: An environment in which has setup/teardown methods. These environments can be composed together to facilitate execution context for promises. In normal R execution, this can be achieved with scope / stack. But for complex situations, such as the currently open graphics device, async operations require promise domains to setup/teardown these contexts to function properly. Otherwise a multi-stage promise that adds to the graphics device at each stage will only ever print to the most recently created graphics device, not the associated graphics device. These promise domains are not automatically created, they must be manually added to the execution stack, for example \code{with_otel_promise_domain()} does this for OpenTelemetry spans. \item Promise domain restoration: When switching from one promise chain to another, the execution context is torn down and then re-established. This re-establishment is called "promise domain restoration". During this process, the promise domains are restored in their previously established combination order. \item Promise chain: A set of promise objects to execute over multiple async ticks. \item Async tick: the number of times an event loop must run to move computation forward. (Similar to a JavaScript event loop tick.) \item \code{then()} promise domain capture: When \code{then()} is called, it will capture the current promise domain. This promise domain is restored (only if needed) when evaluating the given \code{onFulfilled} and \code{onRejected} callbacks. This captured promise domain does not go into any downstream promise chain objects. The only way the promise domain is captured is exactly when the \code{then()} method is called. } \code{with_otel_promise_domain()} creates a promise domain that restores the currently active OpenTelemetry span from when a call to \code{promises::then()} is executed. Given the special circumstance where only the current otel span is needed to continue recording (not a full ancestry tree of otel spans), we can capture \emph{just} the current otel span and reactivate that otel span during promise domain restoration. } \section{When promise domains are captured}{ Asynchronous operation \itemize{ \item Creates \code{async_op} otel span \item Automatically ends the otel span (\code{async_op}) when the promise (p) resolves or rejects } The code below illustrates an example of when the promise domain are created/captured/restored and when otel span objects are created/activated/reactivated/ended. \if{html}{\out{