endTransaction method

Future<void> endTransaction(
  1. String name, {
  2. String? updatedDescription,
})

End a performance transaction and generate final event with all data

Implementation

Future<void> endTransaction(String name, {String? updatedDescription}) async {
  final hub = _hub;
  if (!_isInitialized || hub == null) {
    ObslyLogger.warn('PerformanceController not initialized');
    return;
  }

  // Check if performance is enabled
  final config = ConfigController.instance.config;
  if (!(config?.enablePerformance ?? true)) {
    ObslyLogger.debug('Performance disabled, ignoring endTransaction call');
    return;
  }

  try {
    final transaction = _activeTransactions.remove(name);
    if (transaction == null) {
      ObslyLogger.warn('Transaction not found: $name');
      return;
    }

    final endTime = DateTime.now();
    final endNanoTime = DateTime.now().microsecondsSinceEpoch;
    final durationMs =
        endTime.difference(transaction.startTime).inMilliseconds;

    // Create final performance event with all transaction data
    final event = PerformanceEventBase(
      action: 'transaction',
      transactionName: name,
      description: updatedDescription ?? transaction.description,
      startNanoTime: transaction.startNanoTime,
      endNanoTime: endNanoTime,
      durationMs: durationMs,
      steps: transaction.steps.map((step) => step.toMap()).toList(),
    );

    final reservation = hub.reserveEventMetadata();
    hub.captureEvent(event, reservation);

    ObslyLogger.debug(
        '📊 Performance transaction completed: $name (${durationMs}ms, ${transaction.steps.length} steps)');
  } catch (e) {
    ObslyLogger.error('Error ending transaction: $e');
  }
}