Skip to content

Commit

Permalink
update spotlight transport
Browse files Browse the repository at this point in the history
  • Loading branch information
buenaflor committed Dec 14, 2023
1 parent 2ca35c5 commit 3a985bd
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions dart/lib/src/transport/spotlight_http_transport.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import 'package:http/http.dart';

import '../../sentry.dart';
import '../client_reports/discard_reason.dart';
import 'data_category.dart';

/// Spotlight HTTP transport class that sends Sentry envelopes to both Sentry and Spotlight.
class SpotlightHttpTransport extends Transport {
final SentryOptions _options;
final Transport _transport;
final Map<String, String> _headers = {'Content-Type': 'application/x-sentry-envelope'};

SpotlightHttpTransport(this._options, this._transport);

Expand All @@ -20,10 +23,38 @@ class SpotlightHttpTransport extends Transport {
final StreamedRequest spotlightRequest =
StreamedRequest('POST', spotlightUri);

try {
await _options.httpClient.send(spotlightRequest);
} catch (e) {
// Handle any exceptions.
envelope
.envelopeStream(_options)
.listen(spotlightRequest.sink.add)
.onDone(spotlightRequest.sink.close);

spotlightRequest.headers.addAll(_headers);

final response = await _options.httpClient
.send(spotlightRequest)
.then(Response.fromStream);

if (response.statusCode != 200) {
// body guard to not log the error as it has performance impact to allocate
// the body String.
if (_options.debug) {
_options.logger(
SentryLevel.error,
'Spotlight Sidecar API returned an error, statusCode = ${response.statusCode}, '
'body = ${response.body}',
);
print('body = ${response.request}');
}

if (response.statusCode >= 400 && response.statusCode != 429) {
_options.recorder.recordLostEvent(
DiscardReason.networkError, DataCategory.error);
}
} else {
_options.logger(
SentryLevel.debug,
'Envelope ${envelope.header.eventId ?? "--"} was sent successfully to spotlight ($spotlightUri)',
);
}
}
}

0 comments on commit 3a985bd

Please sign in to comment.