measure static method
Runs serve for req and records the request once, with the status
serve answered: the count in http_requests_total, the duration and
the access record.
For a server that passes a request through a Router and may answer it
some other way afterwards: a web-server build gives every page to the
router first, gets a 404 because pages are not routes, and renders the
page with 200. Measured at the router, every such page was logged and
counted as a 404. A router called inside serve records nothing itself.
Implementation
static Future<Response> measure(
Request req, Future<Response> Function() serve) async {
final Future<Response> Function() inner =
() => _measuringZone().run(serve);
if (_unmeasured.contains(req.url.path) ||
req.url.path.startsWith('/_dartvel/')) {
return inner();
}
// Neither spans nor semantic metrics are read: the common case, measured
// without the closures, label map and telemetry scope those need.
if (!DVObservability.requestTracing && !DVObservability.semanticMetrics) {
final Stopwatch clock = Stopwatch()..start();
try {
final Response response = await inner();
clock.stop();
_record(req, response.status, clock, response);
return response;
} on Object catch (error, stack) {
clock.stop();
// The server layer turns this into a 500 inside a bare `catch (_)`,
// so if it is not recorded here the exception is gone.
_record(req, 500, clock, null, error: error, stackTrace: stack);
rethrow;
}
}
// The request's server span, when anything is reading spans. Checked as a
// field read, so a process nobody traces pays nothing else here.
final DVSpan? span = DVObservability.requestTracing
? dvStartServerSpan(DVObservability.tracer, req)
: null;
final bool semantic = DVObservability.semanticMetrics;
// Only when something will read it back: the metrics, or a route matched
// inside a child span naming the request's span.
final DVRequestTelemetry? scope =
semantic || span != null ? DVRequestTelemetry(span) : null;
final Map<String, String> activeLabels = semantic
? <String, String>{
'http.request.method': req.method,
'url.scheme': req.url.scheme.isEmpty ? 'http' : req.url.scheme,
}
: const <String, String>{};
final DVGauge? active = semantic ? _activeRequests(activeLabels) : null;
active?.increment();
final Stopwatch clock = Stopwatch()..start();
try {
Future<Response> traced() => span == null ? inner() : dvInSpan(span, inner);
final Response response = await (scope == null ? traced() : scope.run(traced));
clock.stop();
if (span != null) dvFinishServerSpan(span, response);
// Inside the span, so the access line names the request's trace.
void record() => _record(req, response.status, clock, response);
span == null ? record() : dvInSpan(span, record);
if (semantic) _recordSemantic(activeLabels, scope?.route, response.status, clock);
return response;
} on Object catch (error, stack) {
clock.stop();
span?.recordError(error, stack);
span?.setAttribute('http.response.status_code', 500);
// The server layer turns this into a 500 inside a bare `catch (_)`, so
// if it is not recorded here the exception is gone: no line, no count,
// and a client holding a 500 nobody can explain.
void record() =>
_record(req, 500, clock, null, error: error, stackTrace: stack);
span == null ? record() : dvInSpan(span, record);
if (semantic) _recordSemantic(activeLabels, scope?.route, 500, clock);
rethrow;
} finally {
active?.decrement();
span?.end();
}
}