Skip to content

Commit

Permalink
Merge branch 'main' into feat/user-and-breadcrumb-from-map
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored May 16, 2023
2 parents 00385e1 + 0a82a1e commit df5cbe7
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Features

- Add `sent_at` to envelope header ([#1428](https://github.com/getsentry/sentry-dart/pull/1428))

### Fixes

- Fix battery level conversion for iOS 16.4 ([#1433](https://github.com/getsentry/sentry-dart/pull/1433))
Expand Down
12 changes: 11 additions & 1 deletion dart/lib/src/sentry_envelope_header.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'protocol/sentry_id.dart';
import 'protocol/sdk_version.dart';
import 'sentry_trace_context_header.dart';
import 'utils.dart';

/// Header containing `SentryId` and `SdkVersion`.
class SentryEnvelopeHeader {
Expand All @@ -9,12 +10,14 @@ class SentryEnvelopeHeader {
this.sdkVersion, {
this.dsn,
this.traceContext,
this.sentAt,
});
SentryEnvelopeHeader.newEventId()
: eventId = SentryId.newId(),
sdkVersion = null,
dsn = null,
traceContext = null;
traceContext = null,
sentAt = null;

/// The identifier of encoded `SentryEvent`.
final SentryId? eventId;
Expand All @@ -27,6 +30,8 @@ class SentryEnvelopeHeader {
/// The `DSN` of the Sentry project.
final String? dsn;

DateTime? sentAt;

/// Header encoded as JSON
Map<String, dynamic> toJson() {
final json = <String, dynamic>{};
Expand All @@ -49,6 +54,11 @@ class SentryEnvelopeHeader {
if (dsn != null) {
json['dsn'] = dsn;
}

if (sentAt != null) {
json['sent_at'] = formatDateAsIso8601WithMillisPrecision(sentAt!);
}

return json;
}
}
19 changes: 5 additions & 14 deletions dart/lib/src/transport/http_transport.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class HttpTransport implements Transport {
_credentialBuilder = _CredentialBuilder(
_dsn,
_options.sentryClientName,
_options.clock,
);
}

Expand All @@ -56,6 +55,7 @@ class HttpTransport implements Transport {
if (filteredEnvelope == null) {
return SentryId.empty();
}
filteredEnvelope.header.sentAt = _options.clock();

final streamedRequest = await _createStreamedRequest(filteredEnvelope);
final response = await _options.httpClient
Expand Down Expand Up @@ -135,23 +135,16 @@ class HttpTransport implements Transport {
class _CredentialBuilder {
final String _authHeader;

final ClockProvider _clock;
_CredentialBuilder._(String authHeader) : _authHeader = authHeader;

int get timestamp => _clock().millisecondsSinceEpoch;

_CredentialBuilder._(String authHeader, ClockProvider clock)
: _authHeader = authHeader,
_clock = clock;

factory _CredentialBuilder(
Dsn dsn, String sdkIdentifier, ClockProvider clock) {
factory _CredentialBuilder(Dsn dsn, String sdkIdentifier) {
final authHeader = _buildAuthHeader(
publicKey: dsn.publicKey,
secretKey: dsn.secretKey,
sdkIdentifier: sdkIdentifier,
);

return _CredentialBuilder._(authHeader, clock);
return _CredentialBuilder._(authHeader);
}

static String _buildAuthHeader({
Expand All @@ -172,9 +165,7 @@ class _CredentialBuilder {
Map<String, String> configure(Map<String, String> headers) {
return headers
..addAll(
<String, String>{
'X-Sentry-Auth': '$_authHeader, sentry_timestamp=$timestamp'
},
<String, String>{'X-Sentry-Auth': _authHeader},
);
}
}
Expand Down
4 changes: 4 additions & 0 deletions dart/test/sentry_envelope_header_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:sentry/sentry.dart';
import 'package:sentry/src/sentry_envelope_header.dart';
import 'package:sentry/src/utils.dart';
import 'package:test/test.dart';

import 'mocks.dart';
Expand All @@ -22,18 +23,21 @@ void main() {
'trace_id': '${SentryId.newId()}',
'public_key': '123',
});
final timestamp = DateTime.utc(2019);
final sut = SentryEnvelopeHeader(
eventId,
sdkVersion,
dsn: fakeDsn,
traceContext: context,
sentAt: timestamp,
);
final expextedSkd = sdkVersion.toJson();
final expected = <String, dynamic>{
'event_id': eventId.toString(),
'sdk': expextedSkd,
'trace': context.toJson(),
'dsn': fakeDsn,
'sent_at': formatDateAsIso8601WithMillisPrecision(timestamp),
};
expect(sut.toJson(), expected);
});
Expand Down
7 changes: 2 additions & 5 deletions dart/test/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,14 @@ void testHeaders(
'Content-Type': 'application/x-sentry-envelope',
'X-Sentry-Auth': 'Sentry sentry_version=7, '
'sentry_client=$sdkName/$sdkVersion, '
'sentry_key=public, '
'sentry_key=public'
};

if (withSecret) {
expectedHeaders['X-Sentry-Auth'] =
'${expectedHeaders['X-Sentry-Auth']!}sentry_secret=secret, ';
'${expectedHeaders['X-Sentry-Auth']!}, sentry_secret=secret';
}

expectedHeaders['X-Sentry-Auth'] =
'${expectedHeaders['X-Sentry-Auth']!}sentry_timestamp=${fakeClockProvider().millisecondsSinceEpoch}';

if (withUserAgent) {
expectedHeaders['User-Agent'] = '$sdkName/$sdkVersion';
}
Expand Down
28 changes: 28 additions & 0 deletions dart/test/transport/http_transport_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,31 @@ void main() {
});
});

group('sent_at', () {
late Fixture fixture;

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

test('capture envelope sets sent_at in header', () async {
final sentryEvent = SentryEvent();
final envelope = SentryEnvelope.fromEvent(
sentryEvent,
fixture.options.sdk,
dsn: fixture.options.dsn,
);

final httpMock = MockClient((http.Request request) async {
return http.Response('{}', 200);
});
final sut = fixture.getSut(httpMock, MockRateLimiter());
await sut.send(envelope);

expect(envelope.header.sentAt, DateTime.utc(2019));
});
});

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

Expand Down Expand Up @@ -232,6 +257,9 @@ class Fixture {
HttpTransport getSut(http.Client client, RateLimiter rateLimiter) {
options.httpClient = client;
options.recorder = clientReportRecorder;
options.clock = () {
return DateTime.utc(2019);
};
return HttpTransport(options, rateLimiter);
}
}

0 comments on commit df5cbe7

Please sign in to comment.