traceSync<R> function

  1. @Deprecated('This method will be removed in 0.19.0. Use [traceContextSync] instead.')
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

@Deprecated(
    'This method will be removed in 0.19.0. Use [traceContextSync] instead.')
R traceSync<R>(String name, R Function() fn,
    {api.Context? context, api.Tracer? tracer}) {
  context ??= api.globalContextManager.active;
  tracer ??= _tracerProvider.getTracer('opentelemetry-dart');

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

  try {
    final r = api.contextWithSpan(context, 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, e.toString())
      ..recordException(e, stackTrace: s);
    rethrow;
  } finally {
    span.end();
  }
}