Skip to content

Commit 647fe2d

Browse files
Denis AndrašecDenis Andrašec
Denis Andrašec
authored and
Denis Andrašec
committed
Merge branch 'feat/client-report-recorder' into feat/client-report-envelope-item
2 parents bc44e36 + 4576880 commit 647fe2d

11 files changed

+152
-135
lines changed

dart/lib/src/client_reports/client_report_recorder.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import 'package:meta/meta.dart';
33
import '../sentry_options.dart';
44
import 'client_report.dart';
55
import 'discarded_event.dart';
6-
import 'outcome.dart';
7-
import '../transport/rate_limit_category.dart';
6+
import 'discard_reason.dart';
7+
import '../transport/data_category.dart';
88

99
@internal
1010
class ClientReportRecorder {
@@ -13,7 +13,8 @@ class ClientReportRecorder {
1313
final ClockProvider _clock;
1414
final Map<_QuantityKey, int> _quantities = {};
1515

16-
void recordLostEvent(final Outcome reason, final RateLimitCategory category) {
16+
void recordLostEvent(
17+
final DiscardReason reason, final DataCategory category) {
1718
final key = _QuantityKey(reason, category);
1819
var current = _quantities[key] ?? 0;
1920
_quantities[key] = current + 1;
@@ -38,8 +39,8 @@ class ClientReportRecorder {
3839
class _QuantityKey {
3940
_QuantityKey(this.reason, this.category);
4041

41-
final Outcome reason;
42-
final RateLimitCategory category;
42+
final DiscardReason reason;
43+
final DataCategory category;
4344

4445
@override
4546
int get hashCode => Object.hash(reason, category);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import 'package:meta/meta.dart';
2+
3+
/// A reason that defines why events were lost, see
4+
/// https://develop.sentry.dev/sdk/client-reports/#envelope-item-payload.
5+
@internal
6+
enum DiscardReason {
7+
beforeSend,
8+
eventProcessor,
9+
sampleRate,
10+
networkError,
11+
queueOverflow,
12+
cacheOverflow,
13+
rateLimitBackoff,
14+
}
15+
16+
extension OutcomeExtension on DiscardReason {
17+
String toStringValue() {
18+
switch (this) {
19+
case DiscardReason.beforeSend:
20+
return 'before_send';
21+
case DiscardReason.eventProcessor:
22+
return 'event_processor';
23+
case DiscardReason.sampleRate:
24+
return 'sample_rate';
25+
case DiscardReason.networkError:
26+
return 'network_error';
27+
case DiscardReason.queueOverflow:
28+
return 'queue_overflow';
29+
case DiscardReason.cacheOverflow:
30+
return 'cache_overflow';
31+
case DiscardReason.rateLimitBackoff:
32+
return 'ratelimit_backoff';
33+
}
34+
}
35+
}

dart/lib/src/client_reports/discarded_event.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import 'package:meta/meta.dart';
22

3-
import 'outcome.dart';
4-
import '../transport/rate_limit_category.dart';
3+
import 'discard_reason.dart';
4+
import '../transport/data_category.dart';
55

66
@internal
77
class DiscardedEvent {
88
DiscardedEvent(this.reason, this.category, this.quantity);
99

10-
final Outcome reason;
11-
final RateLimitCategory category;
10+
final DiscardReason reason;
11+
final DataCategory category;
1212
final int quantity;
1313

1414
Map<String, dynamic> toJson() {

dart/lib/src/client_reports/outcome.dart

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/// Different category types of data sent to Sentry. Used for rate limiting and client reports.
2+
enum DataCategory {
3+
all,
4+
data_category_default, // default
5+
error,
6+
session,
7+
transaction,
8+
attachment,
9+
security,
10+
unknown
11+
}
12+
13+
extension DataCategoryExtension on DataCategory {
14+
static DataCategory fromStringValue(String stringValue) {
15+
switch (stringValue) {
16+
case '__all__':
17+
return DataCategory.all;
18+
case 'default':
19+
return DataCategory.data_category_default;
20+
case 'error':
21+
return DataCategory.error;
22+
case 'session':
23+
return DataCategory.session;
24+
case 'transaction':
25+
return DataCategory.transaction;
26+
case 'attachment':
27+
return DataCategory.attachment;
28+
case 'security':
29+
return DataCategory.security;
30+
}
31+
return DataCategory.unknown;
32+
}
33+
34+
String toStringValue() {
35+
switch (this) {
36+
case DataCategory.all:
37+
return '__all__';
38+
case DataCategory.data_category_default:
39+
return 'default';
40+
case DataCategory.error:
41+
return 'error';
42+
case DataCategory.session:
43+
return 'session';
44+
case DataCategory.transaction:
45+
return 'transaction';
46+
case DataCategory.attachment:
47+
return 'attachment';
48+
case DataCategory.security:
49+
return 'security';
50+
case DataCategory.unknown:
51+
return 'unknown';
52+
}
53+
}
54+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import 'rate_limit_category.dart';
1+
import 'data_category.dart';
22

3-
/// `RateLimit` containing limited `RateLimitCategory` and duration in milliseconds.
3+
/// `RateLimit` containing limited `DataCategory` and duration in milliseconds.
44
class RateLimit {
55
RateLimit(this.category, this.duration);
66

7-
final RateLimitCategory category;
7+
final DataCategory category;
88
final Duration duration;
99
}

dart/lib/src/transport/rate_limit_category.dart

Lines changed: 0 additions & 54 deletions
This file was deleted.

dart/lib/src/transport/rate_limit_parser.dart

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import 'rate_limit_category.dart';
1+
import 'data_category.dart';
22
import 'rate_limit.dart';
33

44
/// Parse rate limit categories and times from response header payloads.
@@ -29,23 +29,20 @@ class RateLimitParser {
2929
if (allCategories.isNotEmpty) {
3030
final categoryValues = allCategories.split(';');
3131
for (final categoryValue in categoryValues) {
32-
final category =
33-
RateLimitCategoryExtension.fromStringValue(categoryValue);
34-
if (category != RateLimitCategory.unknown) {
32+
final category = DataCategoryExtension.fromStringValue(categoryValue);
33+
if (category != DataCategory.unknown) {
3534
rateLimits.add(RateLimit(category, duration));
3635
}
3736
}
3837
} else {
39-
rateLimits.add(RateLimit(RateLimitCategory.all, duration));
38+
rateLimits.add(RateLimit(DataCategory.all, duration));
4039
}
4140
}
4241
return rateLimits;
4342
}
4443

4544
List<RateLimit> parseRetryAfterHeader() {
46-
return [
47-
RateLimit(RateLimitCategory.all, _parseRetryAfterOrDefault(_header))
48-
];
45+
return [RateLimit(DataCategory.all, _parseRetryAfterOrDefault(_header))];
4946
}
5047

5148
// Helper

dart/lib/src/transport/rate_limiter.dart

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import '../sentry_options.dart';
44
import '../sentry_envelope.dart';
55
import '../sentry_envelope_item.dart';
66
import 'rate_limit.dart';
7-
import 'rate_limit_category.dart';
7+
import 'data_category.dart';
88

99
/// Controls retry limits on different category types sent to Sentry.
1010
class RateLimiter {
1111
RateLimiter(this._clockProvider);
1212

1313
final ClockProvider _clockProvider;
14-
final _rateLimitedUntil = <RateLimitCategory, DateTime>{};
14+
final _rateLimitedUntil = <DataCategory, DateTime>{};
1515

1616
/// Filter out envelopes that are rate limited.
1717
SentryEnvelope? filter(SentryEnvelope envelope) {
@@ -75,15 +75,15 @@ class RateLimiter {
7575
_clockProvider().millisecondsSinceEpoch);
7676

7777
// check all categories
78-
final dateAllCategories = _rateLimitedUntil[RateLimitCategory.all];
78+
final dateAllCategories = _rateLimitedUntil[DataCategory.all];
7979
if (dateAllCategories != null) {
8080
if (!currentDate.isAfter(dateAllCategories)) {
8181
return true;
8282
}
8383
}
8484

8585
// Unknown should not be rate limited
86-
if (RateLimitCategory.unknown == dataCategory) {
86+
if (DataCategory.unknown == dataCategory) {
8787
return false;
8888
}
8989

@@ -96,28 +96,27 @@ class RateLimiter {
9696
return false;
9797
}
9898

99-
RateLimitCategory _categoryFromItemType(String itemType) {
99+
DataCategory _categoryFromItemType(String itemType) {
100100
switch (itemType) {
101101
case 'event':
102-
return RateLimitCategory.error;
102+
return DataCategory.error;
103103
case 'session':
104-
return RateLimitCategory.session;
104+
return DataCategory.session;
105105
case 'attachment':
106-
return RateLimitCategory.attachment;
106+
return DataCategory.attachment;
107107
case 'transaction':
108-
return RateLimitCategory.transaction;
108+
return DataCategory.transaction;
109109
default:
110-
return RateLimitCategory.unknown;
110+
return DataCategory.unknown;
111111
}
112112
}
113113

114-
void _applyRetryAfterOnlyIfLonger(
115-
RateLimitCategory rateLimitCategory, DateTime date) {
116-
final oldDate = _rateLimitedUntil[rateLimitCategory];
114+
void _applyRetryAfterOnlyIfLonger(DataCategory dataCategory, DateTime date) {
115+
final oldDate = _rateLimitedUntil[dataCategory];
117116

118117
// only overwrite its previous date if the limit is even longer
119118
if (oldDate == null || date.isAfter(oldDate)) {
120-
_rateLimitedUntil[rateLimitCategory] = date;
119+
_rateLimitedUntil[dataCategory] = date;
121120
}
122121
}
123122
}

0 commit comments

Comments
 (0)