setCurrentSpan method

Context setCurrentSpan(
  1. APISpan? span
)

Creates a new Context with the given span set as active.

This is separate from span creation as per the specification. This method only returns a new context; it does not change the current context. To make the new context active, use run or runSync.

Implementation

Context setCurrentSpan(APISpan? span) {
  if (span == null) {
    // Remove both span and span context
    return ContextCreate.create(
        contextMap: Map.from(_values)
          ..remove(_spanKey)
          ..remove(_spanContextKey));
  } else {
    // Set both span and span context
    return ContextCreate.create(contextMap: {
      ..._values,
      _spanKey: span,
      _spanContextKey: span.spanContext,
    });
  }
}