11import 'package:http/http.dart' ;
22
33import '../../sentry.dart' ;
4+ import '../client_reports/discard_reason.dart' ;
5+ import 'data_category.dart' ;
46
57/// Spotlight HTTP transport class that sends Sentry envelopes to both Sentry and Spotlight.
68class SpotlightHttpTransport extends Transport {
79 final SentryOptions _options;
810 final Transport _transport;
11+ final Map <String , String > _headers = {'Content-Type' : 'application/x-sentry-envelope' };
912
1013 SpotlightHttpTransport (this ._options, this ._transport);
1114
@@ -20,10 +23,38 @@ class SpotlightHttpTransport extends Transport {
2023 final StreamedRequest spotlightRequest =
2124 StreamedRequest ('POST' , spotlightUri);
2225
23- try {
24- await _options.httpClient.send (spotlightRequest);
25- } catch (e) {
26- // Handle any exceptions.
26+ envelope
27+ .envelopeStream (_options)
28+ .listen (spotlightRequest.sink.add)
29+ .onDone (spotlightRequest.sink.close);
30+
31+ spotlightRequest.headers.addAll (_headers);
32+
33+ final response = await _options.httpClient
34+ .send (spotlightRequest)
35+ .then (Response .fromStream);
36+
37+ if (response.statusCode != 200 ) {
38+ // body guard to not log the error as it has performance impact to allocate
39+ // the body String.
40+ if (_options.debug) {
41+ _options.logger (
42+ SentryLevel .error,
43+ 'Spotlight Sidecar API returned an error, statusCode = ${response .statusCode }, '
44+ 'body = ${response .body }' ,
45+ );
46+ print ('body = ${response .request }' );
47+ }
48+
49+ if (response.statusCode >= 400 && response.statusCode != 429 ) {
50+ _options.recorder.recordLostEvent (
51+ DiscardReason .networkError, DataCategory .error);
52+ }
53+ } else {
54+ _options.logger (
55+ SentryLevel .debug,
56+ 'Envelope ${envelope .header .eventId ?? "--" } was sent successfully to spotlight ($spotlightUri )' ,
57+ );
2758 }
2859 }
2960}
0 commit comments