trace<T> method

Future<T?> trace<T>({
  1. required String name,
  2. required String operation,
  3. required FutureOr<T?> fn(
    1. Trace? parentTrace
    ),
  4. Trace? parentTrace,
  5. LogLevel? level = LogLevel.info,
})

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?> trace<T>({
  required String name,
  required String operation,
  required FutureOr<T?> Function(Trace? parentTrace) fn,
  Trace? parentTrace,
  LogLevel? level = LogLevel.info,
}) async {
  final trace = parentTrace != null
      ? await parentTrace.startChild(name, operation, level: level)
      : await startTrace(name, operation, level: level);

  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();
  }
}