finishStep method

Future<void> finishStep(
  1. String stepName,
  2. String transactionName, {
  3. String? updatedDescription,
})

Finish a step within a transaction (manages state and checks auto-finish)

Implementation

Future<void> finishStep(
  String stepName,
  String transactionName, {
  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 finishStep call');
    return;
  }

  try {
    final transaction = _activeTransactions[transactionName];
    if (transaction == null) {
      ObslyLogger.warn('Transaction not found for step: $transactionName');
      return;
    }

    final step = transaction.activeSteps.remove(stepName);
    if (step == null) {
      ObslyLogger.warn('Step not found: $stepName in $transactionName');
      return;
    }

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

    // Complete the step and add to finished steps
    final finishedStep = FinishedPerformanceStep(
      name: stepName,
      description: updatedDescription ?? step.description,
      startNanoTime: step.startNanoTime,
      endNanoTime: endNanoTime,
      durationMs: durationMs,
    );

    transaction.steps.add(finishedStep);

    ObslyLogger.debug(
        '📊 Performance step finished: $stepName (${durationMs}ms)');

    // Check if transaction should auto-finish
    if (transaction.autofinishWithStepsCount != null &&
        transaction.steps.length >= transaction.autofinishWithStepsCount!) {
      await endTransaction(transactionName,
          updatedDescription:
              'Auto-finished with ${transaction.steps.length} steps');
    }
  } catch (e) {
    ObslyLogger.error('Error finishing step: $e');
  }
}