startSpan<T> method

Future<T> startSpan<T>(
  1. String name,
  2. Future<T> callback(
    1. SentrySpanV2 span
    ), {
  3. Map<String, SentryAttribute>? attributes,
  4. SentrySpanV2? parentSpan = const UnsetSentrySpanV2(),
  5. DateTime? startTimestamp,
})

Implementation

Future<T> startSpan<T>(
  String name,
  Future<T> Function(SentrySpanV2 span) callback, {
  Map<String, SentryAttribute>? attributes,
  SentrySpanV2? parentSpan = const UnsetSentrySpanV2(),
  DateTime? startTimestamp,
}) async {
  final span = _createSpan(
    name,
    parentSpan: parentSpan,
    attributes: attributes,
    startTimestamp: startTimestamp,
  );
  switch (span) {
    case NoOpSentrySpanV2():
      internalLogger.info('Hub: startSpan returning no-op for \'$name\'.');
      return callback(span);
    case UnsetSentrySpanV2():
      internalLogger.error(
        'Hub: _createSpan returned UnsetSentrySpanV2 for \'$name\'. '
        'This is a bug — the sentinel should never leak out of _createSpan.',
      );
      return callback(span);
    case RecordingSentrySpanV2 recordingSpan:
      final forkedScope = _forkScopeWithActiveSpan(recordingSpan);
      try {
        return await runZoned(
          () => callback(recordingSpan),
          zoneValues: {_scopeKey: forkedScope},
        );
      } catch (_) {
        span.status = SentrySpanStatusV2.error;
        rethrow;
      } finally {
        span.end();
        forkedScope.removeActiveSpan(span);
      }
  }
}