Skip to content

Client Reports: Record Network Errors #830

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

Merged
merged 4 commits into from
Apr 19, 2022
Merged
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
15 changes: 13 additions & 2 deletions dart/lib/src/transport/http_transport.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import 'dart:convert';

import 'package:http/http.dart';

import '../client_reports/client_report_recorder.dart';
import '../client_reports/discard_reason.dart';
import 'data_category.dart';
import 'noop_encode.dart' if (dart.library.io) 'encode.dart';
import '../noop_client.dart';
import '../protocol.dart';
Expand All @@ -19,6 +22,8 @@ class HttpTransport implements Transport {

final RateLimiter _rateLimiter;

final ClientReportRecorder _recorder;

late _CredentialBuilder _credentialBuilder;

final Map<String, String> _headers;
Expand All @@ -28,10 +33,10 @@ class HttpTransport implements Transport {
options.httpClient = Client();
}

return HttpTransport._(options, rateLimiter);
return HttpTransport._(options, rateLimiter, options.recorder);
}

HttpTransport._(this._options, this._rateLimiter)
HttpTransport._(this._options, this._rateLimiter, this._recorder)
: _dsn = Dsn.parse(_options.dsn!),
_headers = _buildHeaders(
_options.platformChecker.isWeb,
Expand Down Expand Up @@ -68,6 +73,12 @@ class HttpTransport implements Transport {
'body = ${response.body}',
);
}

if (response.statusCode >= 400 && response.statusCode != 429) {
_recorder.recordLostEvent(
DiscardReason.networkError, DataCategory.error);
}

return SentryId.empty();
} else {
_options.logger(
Expand Down
3 changes: 2 additions & 1 deletion dart/test/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,8 @@ void runTest({Codec<List<int>, List<int>?>? gzip, bool isWeb = false}) {
..compressPayload = false
..serverName = 'test.server.com'
..release = '1.2.3'
..environment = 'staging';
..environment = 'staging'
..sendClientReports = false;

final client = SentryClient(options);

Expand Down
56 changes: 56 additions & 0 deletions dart/test/transport/http_transport_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
import 'package:sentry/src/client_reports/discard_reason.dart';
import 'package:sentry/src/sentry_envelope_header.dart';
import 'package:sentry/src/sentry_envelope_item_header.dart';
import 'package:sentry/src/sentry_item_type.dart';
import 'package:sentry/src/transport/data_category.dart';
import 'package:sentry/src/transport/rate_limiter.dart';
import 'package:test/test.dart';
import 'package:sentry/src/sentry_tracer.dart';
Expand Down Expand Up @@ -147,6 +149,59 @@ void main() {
'fixture-sentryRateLimitHeader');
});
});

group('client reports', () {
late Fixture fixture;

setUp(() {
fixture = Fixture();
});

test('does records lost event for error >= 400', () async {
final httpMock = MockClient((http.Request request) async {
return http.Response('{}', 400);
});
final sut = fixture.getSut(httpMock, MockRateLimiter());

final sentryEvent = SentryEvent();
final envelope =
SentryEnvelope.fromEvent(sentryEvent, fixture.options.sdk);
await sut.send(envelope);

expect(fixture.clientReportRecorder.reason, DiscardReason.networkError);
expect(fixture.clientReportRecorder.category, DataCategory.error);
});

test('does not record lost event for error 429', () async {
final httpMock = MockClient((http.Request request) async {
return http.Response('{}', 429);
});
final sut = fixture.getSut(httpMock, MockRateLimiter());

final sentryEvent = SentryEvent();
final envelope =
SentryEnvelope.fromEvent(sentryEvent, fixture.options.sdk);
await sut.send(envelope);

expect(fixture.clientReportRecorder.reason, null);
expect(fixture.clientReportRecorder.category, null);
});

test('does record lost event for error >= 500', () async {
final httpMock = MockClient((http.Request request) async {
return http.Response('{}', 500);
});
final sut = fixture.getSut(httpMock, MockRateLimiter());

final sentryEvent = SentryEvent();
final envelope =
SentryEnvelope.fromEvent(sentryEvent, fixture.options.sdk);
await sut.send(envelope);

expect(fixture.clientReportRecorder.reason, DiscardReason.networkError);
expect(fixture.clientReportRecorder.category, DataCategory.error);
});
});
}

class Fixture {
Expand All @@ -158,6 +213,7 @@ class Fixture {

HttpTransport getSut(http.Client client, RateLimiter rateLimiter) {
options.httpClient = client;
options.recorder = clientReportRecorder;
return HttpTransport(options, rateLimiter);
}

Expand Down