Libraries
    Preparing search index...

    Function withSpan

    • Wraps fn in a custom OTel span and returns whatever fn returns.

      The span is activated as the current context span via startActiveSpan, so any child spans created inside fn (e.g. DB queries from instrumentKnex) will be correctly nested under it. Error handling is automatic — exceptions are recorded and the span is marked ERROR before re-throwing.

      Type Parameters

      • T

      Parameters

      Returns T

      const result = await withSpan('order.process', async span => {
      span.setAttribute('order.id', orderId);
      return processOrder(orderId); // DB spans nest under this span
      });
    • Creates a custom OTel span and returns a SpanHandle that implements AsyncDisposable.

      Use with await using for an ergonomic resource-management syntax. Note that the span is not the active context span (see SpanHandle for details). Call SpanHandle.fail in a catch block to record errors before re-throwing.

      Parameters

      Returns SpanHandle

      await using handle = withSpan('todo.create', { attributes: { 'todo.user_id': userId } });
      try {
      const todo = await db.todos.insert({...});
      handle.span.setAttribute('todo.id', todo.id);
      } catch (err) {
      handle.fail(err);
      throw err;
      }