end method

void end({
  1. double? totalMs,
})

Ends the client span.

When totalMs is provided (unread-body fallback), total / call and the span endTime use that snapshot rather than wall-clock now, so an orphan timer does not inflate duration.

Implementation

void end({double? totalMs}) {
  if (_ended) return;
  _ended = true;

  final resolvedTotalMs = totalMs ?? elapsedMs(_totalStopwatch);
  if (supportedPhases.contains(HttpTimingPhase.total)) {
    span.setAttribute(
      Attribute.fromDouble('http.client.timing.total_ms', resolvedTotalMs),
    );
    _completedPhases.add(HttpTimingPhase.total);
  }
  if (supportedPhases.contains(HttpTimingPhase.call)) {
    span.addEvent(
      'http.call',
      attributes: [Attribute.fromDouble('duration_ms', resolvedTotalMs)],
    );
    _completedPhases.add(HttpTimingPhase.call);
  }

  span.setAttribute(
    Attribute.fromString(
      'http.client.timing.phases_complete',
      _completedPhases.map((p) => p.name).join(','),
    ),
  );

  if (totalMs != null) {
    final impl = span;
    if (impl is sdk_span.Span) {
      span.end(
        endTime: impl.startTime + Int64((totalMs * _nanosPerMs).round()),
      );
      return;
    }
  }
  span.end();
}