endSpan method

  1. @override
Future<ResultDart<void, TelemetryException>> endSpan({
  1. required String spanId,
  2. Map<String, String> attributes = const {},
})
override

Finishes a span by calculating duration.

Returns ResultDart with success or TelemetryException on error.

Implementation

@override
Future<ResultDart<void, TelemetryException>> endSpan({
  required String spanId,
  Map<String, String> attributes = const {},
}) async {
  final cached = _activeSpans[spanId];
  if (cached == null) {
    return Failure(
      TelemetryException(
        message: 'Span $spanId not found',
        code: 'SPAN_NOT_FOUND',
      ),
    );
  }

  final now = DateTime.now().toUtc();
  final updatedSpan = cached.copyWith(
    endTime: now,
    attributes: {...cached.attributes, ...attributes},
  );

  await _repository.updateSpan(
    spanId: spanId,
    endTime: now,
    attributes: updatedSpan.attributes,
  );
  _activeSpans.remove(spanId);
  return const Success(unit);
}