inOperation<T> method
Wraps an async operation with trace lifecycle and timing.
Starts a trace, executes fn, ends the trace, and records timing.
On error, attaches error attributes to the trace and rethrows.
Implementation
Future<T> inOperation<T>(
String operationName,
Future<T> Function() fn,
) async {
final trace = startTrace(operationName);
final stopwatch = Stopwatch()..start();
try {
final result = await fn();
await recordTiming(
name: '$operationName.duration',
duration: stopwatch.elapsed,
attributes: {'operation': operationName},
);
await endTrace(traceId: trace.traceId);
return result;
} catch (e, s) {
await endTrace(
traceId: trace.traceId,
attributes: {'error': e.toString()},
);
await recordEvent(
name: '$operationName.error',
severity: TelemetrySeverity.error,
message: e.toString(),
context: {'stackTrace': s.toString()},
);
rethrow;
}
}