traceSync<R> function

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

Use traceSync instead of trace when fn is not an async function.

Implementation

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

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

  try {
    final r = context.withSpan(span).execute(fn);

    if (r is Future) {
      throw ArgumentError.value(fn, 'fn',
          'Use traceSync to trace functions that do not return a [Future].');
    }

    return r;
  } catch (e, s) {
    span
      ..setStatus(api.StatusCode.error, description: e.toString())
      ..recordException(e, stackTrace: s);
    rethrow;
  } finally {
    span.end();
  }
}