Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve FileSystemTransport by using compute() #1811

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions dart/lib/src/sentry_envelope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:convert';
import 'client_reports/client_report.dart';
import 'protocol.dart';
import 'sentry_item_type.dart';
import 'sentry_options.dart';
import 'sentry_trace_context_header.dart';
import 'utils.dart';
import 'sentry_attachment/sentry_attachment.dart';
Expand Down Expand Up @@ -83,7 +82,7 @@ class SentryEnvelope {
}

/// Stream binary data representation of `Envelope` file encoded.
Stream<List<int>> envelopeStream(SentryOptions options) async* {
Stream<List<int>> envelopeStream(int maxAttachmentSize) async* {
yield utf8JsonEncoder.convert(header.toJson());

final newLineData = utf8.encode('\n');
Expand All @@ -94,10 +93,9 @@ class SentryEnvelope {
if (length < 0) {
continue;
}
// Only attachments should be filtered according to
// SentryOptions.maxAttachmentSize
// Filter out attachments larger than maxAttachmentSize
if (item.header.type == SentryItemType.attachment) {
if (await item.header.length() > options.maxAttachmentSize) {
if (await item.header.length() > maxAttachmentSize) {
continue;
}
}
Expand Down
4 changes: 2 additions & 2 deletions dart/lib/src/transport/http_transport_request_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ class HttpTransportRequestHandler {
if (_options.compressPayload) {
final compressionSink = compressInSink(streamedRequest.sink, _headers);
envelope
.envelopeStream(_options)
.envelopeStream(_options.maxAttachmentSize)
.listen(compressionSink.add)
.onDone(compressionSink.close);
} else {
envelope
.envelopeStream(_options)
.envelopeStream(_options.maxAttachmentSize)
.listen(streamedRequest.sink.add)
.onDone(streamedRequest.sink.close);
}
Expand Down
2 changes: 1 addition & 1 deletion dart/test/mocks/mock_envelope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class MockEnvelope implements SentryEnvelope {
}

@override
Stream<List<int>> envelopeStream(SentryOptions options) async* {
Stream<List<int>> envelopeStream(int maxAttachmentSize) async* {
yield [0];
}

Expand Down
10 changes: 4 additions & 6 deletions dart/test/sentry_envelope_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void main() {
'$expectedHeaderJsonSerialized\n$expectedItemSerialized\n$expectedItemSerialized');

final envelopeData = <int>[];
await sut.envelopeStream(SentryOptions()).forEach(envelopeData.addAll);
await sut.envelopeStream(20 * 1024 * 1024).forEach(envelopeData.addAll);
expect(envelopeData, expected);
});

Expand Down Expand Up @@ -155,13 +155,11 @@ void main() {
);

final sutEnvelopeData = <int>[];
await sut
.envelopeStream(SentryOptions()..maxAttachmentSize = 1)
.forEach(sutEnvelopeData.addAll);
await sut.envelopeStream(1).forEach(sutEnvelopeData.addAll);

final envelopeData = <int>[];
await expectedEnvelopeItem
.envelopeStream(SentryOptions())
.envelopeStream(20 * 1024 * 1024)
.forEach(envelopeData.addAll);

expect(sutEnvelopeData, envelopeData);
Expand All @@ -181,7 +179,7 @@ void main() {
dsn: fakeDsn,
);

final _ = sut.envelopeStream(SentryOptions()).map((e) => e);
final _ = sut.envelopeStream(20 * 1024 * 1024).map((e) => e);
});
});
}
Expand Down
4 changes: 2 additions & 2 deletions dart/test/sentry_envelope_vm_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void main() {

final envelopeData = <int>[];
await envelope
.envelopeStream(SentryOptions())
.envelopeStream(20 * 1024 * 1024)
.forEach(envelopeData.addAll);

final expectedEnvelopeFile =
Expand All @@ -60,7 +60,7 @@ void main() {
attachments: [attachment],
);

final data = (await envelope.envelopeStream(SentryOptions()).toList())
final data = (await envelope.envelopeStream(20 * 1024 * 1024).toList())
.reduce((a, b) => a + b);

final file = File('test_resources/envelope-no-attachment.envelope');
Expand Down
2 changes: 1 addition & 1 deletion dart/test/transport/http_transport_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void main() {

final envelopeData = <int>[];
await filteredEnvelope
.envelopeStream(fixture.options)
.envelopeStream(20 * 1024 * 1024)
.forEach(envelopeData.addAll);

expect(body, envelopeData);
Expand Down
2 changes: 1 addition & 1 deletion dart/test/transport/spotlight_http_transport_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void main() {

final envelopeData = <int>[];
await envelope
.envelopeStream(fixture.options)
.envelopeStream(20 * 1024 * 1024)
.forEach(envelopeData.addAll);

expect(body, envelopeData);
Expand Down
15 changes: 15 additions & 0 deletions flutter/example/integration_test/integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ void main() {
expect(sentryId != const SentryId.empty(), true);
});

testWidgets('setup sentry and capture event with attachment', (tester) async {
await setupSentryAndApp(tester);

final event = SentryEvent();
final attachment = SentryAttachment.fromIntList(
utf8.encode('Lorem Ipsum dolar sit amet'),
'foobar.txt',
contentType: 'text/plain',
);
final hint = Hint.withAttachment(attachment);
final sentryId = await Sentry.captureEvent(event, hint: hint);

expect(sentryId != const SentryId.empty(), true);
});

testWidgets('setup sentry and capture exception', (tester) async {
await setupSentryAndApp(tester);

Expand Down
21 changes: 17 additions & 4 deletions flutter/lib/src/file_system_transport.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// ignore: unnecessary_import
import 'dart:typed_data';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:sentry/sentry.dart';

Expand All @@ -13,10 +14,8 @@ class FileSystemTransport implements Transport {

@override
Future<SentryId?> send(SentryEnvelope envelope) async {
final envelopeData = <int>[];
await envelope.envelopeStream(_options).forEach(envelopeData.addAll);
// https://flutter.dev/docs/development/platform-integration/platform-channels#codec
final args = [Uint8List.fromList(envelopeData)];
final envelopeData = await compute(_convert, _ConvertMessage(envelope, _options.maxAttachmentSize));
final args = [envelopeData];
try {
await _channel.invokeMethod('captureEnvelope', args);
} catch (exception, stackTrace) {
Expand All @@ -31,4 +30,18 @@ class FileSystemTransport implements Transport {

return envelope.header.eventId;
}

static Future<Uint8List> _convert(_ConvertMessage message) async {
final envelopeData = <int>[];
await message.envelope.envelopeStream(message.maxAttachmentSize).forEach(envelopeData.addAll);
//https://flutter.dev/docs/development/platform-integration/platform-channels#codec
return Uint8List.fromList(envelopeData);
}
}

class _ConvertMessage {
final SentryEnvelope envelope;
final int maxAttachmentSize;

_ConvertMessage(this.envelope, this.maxAttachmentSize);
}
Loading