send method

  1. @override
Future<StreamedResponse> send(
  1. BaseRequest request
)

Sends an HTTP request and asynchronously returns the response.

Implementers should call BaseRequest.finalize to get the body of the request as a ByteStream. They shouldn't make any assumptions about the state of the stream; it could have data written to it asynchronously at a later point, or it could already be closed when it's returned. Any internal HTTP errors should be wrapped as ClientExceptions.

Implementation

@override
Future<http.StreamedResponse> send(http.BaseRequest request) async {
  final traceHeaders = await DatadogTracing.createHeaders(
    method: request.method,
    resourceName: resourceName,
    url: request.url.toString(),
  );
  request.headers.addAll(traceHeaders);
  // To make sure the generated traces from Real User Monitoring
  // don’t affect your APM Index Spans counts.
  // https://docs.datadoghq.com/real_user_monitoring/connect_rum_and_traces/?tab=iosrum
  request.headers.addAll({'x-datadog-origin': 'rum'});

  http.StreamedResponse? response;
  try {
    return response = await _innerClient.send(request);
  } finally {
    final spanId = traceHeaders['x-datadog-parent-id'];
    if (spanId != null) {
      // the native DD layer should not delay completion of the request
      // errors are also swallowed
      // ignore: unawaited_futures
      DatadogTracing.finishSpan(
        spanId,
        statusCode: response?.statusCode,
      ).catchError((e) => null);
    }
  }
}