runWithActiveGlobalSpan static method
Runs block in a zone where getActiveGlobalSpanId returns spanId.
Implements CoralogixGlobalSpan.withContext via
runZonedGuarded(..., zoneValues: {#cxActiveSpanId: spanId}) so the active
id survives await (Q5).
A Completer is used instead of late final so that async errors captured
by runZonedGuarded's error handler after block has already returned a
future do not trigger a LateInitializationError on reassignment.
Implementation
static Future<void> runWithActiveGlobalSpan(
String spanId,
Future<void> Function() block,
) {
final completer = Completer<void>();
runZonedGuarded(
() {
block().then(
(_) {
if (!completer.isCompleted) completer.complete();
},
onError: (Object error, StackTrace stack) {
if (!completer.isCompleted) completer.completeError(error, stack);
},
);
},
(Object error, StackTrace stack) {
if (!completer.isCompleted) completer.completeError(error, stack);
},
zoneValues: <Symbol, Object?>{cxActiveSpanIdKey: spanId},
);
return completer.future;
}