trace<T> function

Future<T> trace<T>(
  1. String name,
  2. Future<T> fn(), {
  3. Context? context,
  4. Tracer? tracer,
})

Records a span of the given name for the given function with a given api.Tracer and marks the span as errored if an exception occurs.

Implementation

Future<T> trace<T>(String name, Future<T> Function() fn,
    {api.Context? context, api.Tracer? tracer}) async {
  context ??= api.Context.current;
  tracer ??= _tracerProvider.getTracer('videosdk_otel-dart');

  final span = tracer.startSpan(name, context: context);

  try {
    return await context.withSpan(span).execute(fn);
  } catch (e, s) {
    span
      ..setStatus(api.StatusCode.error, description: e.toString())
      ..recordException(e, stackTrace: s);
    rethrow;
  } finally {
    span.end();
  }
}