startSpanWithContext method

APISpan startSpanWithContext({
  1. required String name,
  2. required Context context,
  3. SpanKind kind = SpanKind.internal,
  4. Attributes? attributes,
})

Starts a span with the given context instead of the current context Sets the current span in the given context

Implementation

APISpan startSpanWithContext({
  required String name,
  required Context context,
  SpanKind kind = SpanKind.internal,
  Attributes? attributes,
}) {
  final span = startSpan(
    name,
    context: context,
    kind: kind,
    attributes: attributes,
  );

  // Set the span in the context we were given
  final updatedContext = context.setCurrentSpan(span);

  // Also update the current global context if needed
  if (Context.current == context) {
    Context.current = updatedContext;
  }

  return span;
}