sentry-dart icon indicating copy to clipboard operation
sentry-dart copied to clipboard

Support custom client and transport for the Flutter SDK

Open KeterVM opened this issue 3 years ago • 5 comments

Hi, Currently we are using the self-hosting Sentry back-end.

Due to security concerns, our devOps asked me to add some extra properties to request headers.

Im tried implements a BaseClient and pass it to the option, but it doesn't work.

  if (kReleaseMode) {
    await SentryFlutter.init(
      (options) {
        options.dsn =
            'http://##########dnsLink########/2';
        // Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring.
        // We recommend adjusting this value in production.
        options.httpClient = MySentryHttpClient;
        options.tracesSampleRate = 1.0;
      },
      appRunner: () => runApp(const MyApp()),
    );
  } else {
    runApp(
      const MyApp(),
    );
  }

my_sentry_http_client.dart

class MySentryHttpClient extends BaseClient {
  final Client _httpClient = Client();

  MySentryHttpClient();

  @override
  Future<StreamedResponse> send(BaseRequest request) {
    var defaultHeaders = <String, String>{
      'Content-Type': 'application/json; charset=UTF-8',
      "Extra-Property": "tokenABC",
    };
    request.headers.addAll(defaultHeaders);
    return _httpClient.send(request);
  }
}

Sorry for my broken english and thank you for everything....

KeterVM avatar Aug 03 '22 13:08 KeterVM

@KeterVM we'll look into it, thanks.

marandaneto avatar Aug 04 '22 08:08 marandaneto

The Java SDK makes it possible having a ITransportFactory and a RequestDetails parameter, so users could override the transportFactory property in the SentryOptions and mutate the requestDetails, we could do the same here. For the Dart SDK that would be fine but for the Flutter SDK, we'd need to sync down this configuration to the Android and iOS SDK, the iOS SDK currently does not support this feature either, so we are blocked for now. @philipphofmann @brustolin to confirm.

@KeterVM your option should work for the Dart SDK but not for the Flutter SDK (it uses the client and transport from the Native SDKs).

marandaneto avatar Nov 21 '22 08:11 marandaneto

Adding extra HTTP headers should be possible with SentryOptions.urlSessionDelegate. Can you confirm @brustolin?

philipphofmann avatar Nov 24 '22 15:11 philipphofmann

@brustolin can you confirm that?

marandaneto avatar May 25 '23 13:05 marandaneto

Adding extra HTTP headers should be possible with SentryOptions.urlSessionDelegate. Can you confirm @brustolin?

No, not possible. URLSession Delegate is to respond to state changes, not mutate the request. Currently is not possible to do this in the iOS SDK.

brustolin avatar May 26 '23 09:05 brustolin