Skip to content

Commit 9fa06c0

Browse files
authored
[v9] Remove max response body size (#2709)
1 parent a685eaf commit 9fa06c0

File tree

8 files changed

+120
-130
lines changed

8 files changed

+120
-130
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
- Add hint for transactions ([#2675](https://github.com/getsentry/sentry-dart/pull/2675))
1919
- `BeforeSendTransactionCallback` now has a `Hint` parameter
2020
- Remove `dart:html` usage in favour of `package:web` ([#2710](https://github.com/getsentry/sentry-dart/pull/2710))
21-
21+
- Remove max response body size ([#2709](https://github.com/getsentry/sentry-dart/pull/2709))
22+
- Responses are now only attached if size is below ~0.15mb
23+
- Responses are attached to the `Hint` object, which can be read in `beforeSend`/`beforeSendTransaction` callbacks via `hint.response`.
24+
- For now, only the `dio` integration is supported.
25+
2226
### Enhancements
2327

2428
- Replay: improve Android native interop performance by using JNI ([#2670](https://github.com/getsentry/sentry-dart/pull/2670))

dart/lib/src/hint.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import 'protocol/sentry_response.dart';
12
import 'sentry_attachment/sentry_attachment.dart';
2-
import 'sentry_options.dart';
3+
4+
import 'package:meta/meta.dart';
35

46
/// Hints are used in [BeforeSendCallback], [BeforeBreadcrumbCallback] and
57
/// event processors.
@@ -41,6 +43,9 @@ import 'sentry_options.dart';
4143
/// };
4244
/// ```
4345
class Hint {
46+
@internal
47+
static const maxResponseBodySize = 157286;
48+
4449
final Map<String, dynamic> _internalStorage = {};
4550

4651
final List<SentryAttachment> attachments = [];
@@ -49,6 +54,8 @@ class Hint {
4954

5055
SentryAttachment? viewHierarchy;
5156

57+
SentryResponse? response;
58+
5259
Hint();
5360

5461
factory Hint.withAttachment(SentryAttachment attachment) {
@@ -81,6 +88,12 @@ class Hint {
8188
return hint;
8289
}
8390

91+
factory Hint.withResponse(SentryResponse response) {
92+
final hint = Hint();
93+
hint.response = response;
94+
return hint;
95+
}
96+
8497
// Key/Value Storage
8598

8699
void addAll(Map<String, dynamic> keysAndValues) {

dart/lib/src/protocol/contexts.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,10 @@ class Contexts extends MapView<String, dynamic> {
137137

138138
set trace(SentryTraceContext? trace) => this[SentryTraceContext.type] = trace;
139139

140-
/// Response context for a HTTP response.
140+
/// Response context for a HTTP response. Not added automatically.
141141
SentryResponse? get response => this[SentryResponse.type];
142142

143+
/// Use [Hint.response] in `beforeSend/beforeSendTransaction` to populate this value.
143144
set response(SentryResponse? value) => this[SentryResponse.type] = value;
144145

145146
/// Feedback context for a feedback event.

dart/lib/src/sentry_options.dart

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,6 @@ class SentryOptions {
124124
/// This does not change whether an event is captured.
125125
MaxRequestBodySize maxRequestBodySize = MaxRequestBodySize.never;
126126

127-
/// Configures up to which size response bodies should be included in events.
128-
/// This does not change whether an event is captured.
129-
MaxResponseBodySize maxResponseBodySize = MaxResponseBodySize.never;
130-
131127
SentryLogger _logger = noOpLogger;
132128

133129
/// Logger interface to log useful debugging information if debug is enabled

dio/example/example.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ Future<void> main() async {
1616
options.sendDefaultPii = true;
1717

1818
options.maxRequestBodySize = MaxRequestBodySize.small;
19-
options.maxResponseBodySize = MaxResponseBodySize.small;
2019
},
2120
appRunner: runApp, // Init your App.
2221
);
Lines changed: 15 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// ignore_for_file: deprecated_member_use
22

3-
import 'dart:convert';
4-
53
import 'package:dio/dio.dart';
64
import 'package:sentry/sentry.dart';
75

@@ -34,17 +32,13 @@ class DioEventProcessor implements EventProcessor {
3432
return event;
3533
}
3634

37-
final response = _responseFrom(dioError);
35+
hint.response ??= _responseFrom(dioError);
3836

39-
var contexts = event.contexts;
40-
if (event.contexts.response == null) {
41-
contexts = contexts.copyWith(response: response);
42-
}
4337
// Don't override just parts of the original request.
4438
// Keep the original one or if there's none create one.
4539
event = event.copyWith(
4640
request: event.request ?? _requestFrom(dioError),
47-
contexts: contexts,
41+
contexts: event.contexts,
4842
);
4943

5044
return event;
@@ -86,77 +80,31 @@ class DioEventProcessor implements EventProcessor {
8680
final headers = response?.headers.map.map(
8781
(key, value) => MapEntry(key, value.join('; ')),
8882
);
83+
final contentLengthHeader = headers?['content-length'];
84+
int? contentLength;
85+
if (contentLengthHeader != null) {
86+
contentLength = int.tryParse(contentLengthHeader);
87+
}
8988

9089
return SentryResponse(
9190
headers: _options.sendDefaultPii ? headers : null,
92-
bodySize: _getBodySize(
93-
dioError.response?.data,
94-
dioError.requestOptions.responseType,
95-
),
91+
bodySize: contentLength,
9692
statusCode: response?.statusCode,
97-
data: _getResponseData(
98-
dioError.response?.data,
99-
dioError.requestOptions.responseType,
100-
),
93+
data: _getResponseData(dioError.response?.data, contentLength),
10194
);
10295
}
10396

104-
/// Returns the response data, if possible according to the users settings.
105-
Object? _getResponseData(Object? data, ResponseType responseType) {
97+
Object? _getResponseData(Object? data, int? contentLength) {
10698
if (!_options.sendDefaultPii || data == null) {
10799
return null;
108100
}
109-
switch (responseType) {
110-
case ResponseType.json:
111-
// ignore: invalid_use_of_internal_member
112-
final jsData = utf8JsonEncoder.convert(data);
113-
if (_options.maxResponseBodySize.shouldAddBody(jsData.length)) {
114-
return data;
115-
}
116-
break;
117-
case ResponseType.stream:
118-
break; // No support for logging stream body.
119-
case ResponseType.plain:
120-
if (data is String &&
121-
_options.maxResponseBodySize.shouldAddBody(data.codeUnits.length)) {
122-
return data;
123-
}
124-
break;
125-
case ResponseType.bytes:
126-
if (data is List<int> &&
127-
_options.maxResponseBodySize.shouldAddBody(data.length)) {
128-
return data;
129-
}
130-
break;
131-
}
132-
return null;
133-
}
134-
135-
int? _getBodySize(Object? data, ResponseType responseType) {
136-
if (data == null) {
101+
if (contentLength == null) {
137102
return null;
138103
}
139-
switch (responseType) {
140-
case ResponseType.json:
141-
return json.encode(data).codeUnits.length;
142-
case ResponseType.stream:
143-
if (data is String) {
144-
return data.length;
145-
} else {
146-
return null;
147-
}
148-
case ResponseType.plain:
149-
if (data is String) {
150-
return data.codeUnits.length;
151-
} else {
152-
return null;
153-
}
154-
case ResponseType.bytes:
155-
if (data is List<int>) {
156-
return data.length;
157-
} else {
158-
return null;
159-
}
104+
// ignore: invalid_use_of_internal_member
105+
if (contentLength > Hint.maxResponseBodySize) {
106+
return null;
160107
}
108+
return data;
161109
}
162110
}

0 commit comments

Comments
 (0)