runWithTrace<T> method

Future<T?> runWithTrace<T>({
  1. required String name,
  2. required String operation,
  3. required FutureOr<T?> fn(
    1. AnalyticsTrace? parentTrace
    ),
  4. AnalyticsTrace? parentTrace,
})

Runs a function with a trace. This is useful for wrapping functions that need to be traced. It can also be part of a nested trace, using the parentTrace parameter.

Implementation

Future<T?> runWithTrace<T>({
  required String name,
  required String operation,
  required FutureOr<T?> Function(AnalyticsTrace? parentTrace) fn,
  AnalyticsTrace? parentTrace,
}) async {
  final trace = parentTrace != null
      ? await parentTrace.startChild(name, operation)
      : await startTrace(name, operation);

  try {
    final value = fn(trace);
    if (value is Future) {
      return await value;
    }

    return value;
  } catch (ex, stackTrace) {
    reportError(ex, stackTrace: stackTrace);
    rethrow;
  } finally {
    await trace.stop();
  }
}