dvTraced function

Future<Response> dvTraced(
  1. DVTracer tracer,
  2. Request request,
  3. Future<Response> handler(
    1. Request request
    )
)

Runs handler inside a span for the request.

Joins the caller's trace when the request carries a valid traceparent, and starts a new one when it does not.

When the request is already inside a server span -- every request is, once tracing is on, because Router.measure opens one -- this runs the handler as it is. A second server span for the same request would draw a service calling itself.

Implementation

Future<Response> dvTraced(
  DVTracer tracer,
  Request request,
  Future<Response> Function(Request request) handler,
) async {
  final DVSpan? current = dvCurrentSpan;
  if (current != null && current.kind == DVSpanKind.server && !current.isEnded) {
    return handler(request);
  }
  final DVSpan span = dvStartServerSpan(tracer, request);
  try {
    final Response response = await dvInSpan(span, () => handler(request));
    dvFinishServerSpan(span, response);
    return response;
  } on Object catch (error, stack) {
    span.recordError(error, stack);
    rethrow;
  } finally {
    // Either way. A span left open is a span never exported, so an endpoint
    // that always fails would be the one that never appears in the trace
    // view.
    span.end();
  }
}