openUrl method

  1. @override
Future<HttpClientRequest> openUrl(
  1. String method,
  2. Uri url
)
override

Opens an HTTP connection.

The HTTP method is specified in method and the URL to use in url.

The Host header for the request will be set to the value Uri.host:Uri.port from url (if url.host is an IP address, it will still be used in the Host header). This can be overridden through the HttpClientRequest interface before the request is sent.

For additional information on the sequence of events during an HTTP transaction, and the objects returned by the futures, see the overall documentation for the class HttpClient.

Implementation

@override
Future<HttpClientRequest> openUrl(String method, Uri url) async {
  // Reserve metadata as early as possible for native HTTP request
  final hub = _integration._hub;
  final reservation = hub?.reserveEventMetadata();
  final metadata = HTTPRequestMetadata(
    method: method,
    url: url,
    startTime: DateTime.now().toUtc(),
    stopwatch: Stopwatch()..start(),
  );

  try {
    final request = await _inner.openUrl(method, url);

    // Check if should capture
    if (!metadata.shouldCapture(
        ConfigController.instance.config?.requestBlacklist?.toSet() ?? {})) {
      return request;
    }

    return ObslyNativeHttpClientRequest._(request, _integration, metadata,
        reservation: reservation);
  } catch (e) {
    metadata.stopwatch.stop();

    // Check if should capture this error
    if (!metadata.shouldCapture(
        ConfigController.instance.config?.requestBlacklist?.toSet() ?? {})) {
      rethrow;
    }

    // Capture DNS/network errors that occur during connection setup
    try {
      int? effectiveStatusCode = HTTPIntegration._mapNetworkErrorToStatus(e);
      String? enhancedErrorMessage = effectiveStatusCode == -1
          ? HTTPIntegration._enhanceNetworkErrorMessage(e)
          : e.toString();

      final event = HTTPEventBase(
        method: metadata.method,
        url: metadata.url.toString(),
        statusCode: effectiveStatusCode,
        duration:
            Duration(milliseconds: metadata.stopwatch.elapsedMilliseconds),
        requestHeaders: {}, // No headers available for DNS errors captured at openUrl level
        responseHeaders: {},
        requestBody: metadata.getSafeRequestBody(),
        responseBody: null,
        isError: true,
        errorMessage: enhancedErrorMessage,
      );

      if (reservation != null) {
        _integration.captureHTTPEvent(event, reservation);
      }
    } catch (captureError) {
      ObslyLogger.error(
          'Error capturing native HTTP connection error: $captureError');
    }

    rethrow;
  }
}