-
-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathtransport.dart
More file actions
109 lines (87 loc) · 2.6 KB
/
transport.dart
File metadata and controls
109 lines (87 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import 'dart:async';
import 'dart:convert';
import 'package:meta/meta.dart';
import 'package:sentry/src/utils.dart';
import '../protocol.dart';
import '../sentry_options.dart';
import '../stack_trace.dart';
import 'body_encoder_browser.dart' if (dart.library.io) 'body_encoder.dart';
import 'header_builder_browser.dart' if (dart.library.io) 'header_builder.dart';
typedef BodyEncoder = List<int> Function(
Map<String, dynamic> data,
Map<String, String> headers, {
bool compressPayload,
});
/// A transport is in charge of sending the event to the Sentry server.
class Transport {
final SentryOptions _options;
@visibleForTesting
final Dsn dsn;
/// Use for browser stacktrace
final String origin;
/// Used by sentry to differentiate browser from io environment
final String platform;
final Sdk sdk;
Transport({
@required SentryOptions options,
@required this.sdk,
@required this.platform,
this.origin,
}) : _options = options,
dsn = Dsn.parse(options.dsn);
Future<SentryId> send(
SentryEvent event, {
StackFrameFilter stackFrameFilter,
}) async {
final now = _options.clock();
var authHeader = dsn.buildAuthHeader(
timestamp: now.millisecondsSinceEpoch, clientId: sdk.identifier);
final headers = buildHeaders(authHeader, sdk: sdk);
final data = _getEventData(
event,
timeStamp: now,
stackFrameFilter: stackFrameFilter,
);
final body = bodyEncoder(
data,
headers,
compressPayload: _options.compressPayload,
);
final response = await _options.httpClient.post(
dsn.postUri,
headers: headers,
body: body,
);
if (response.statusCode != 200) {
return SentryId.empty();
}
final eventId = json.decode(response.body)['id'];
return eventId != null ? SentryId.fromId(eventId) : SentryId.empty();
}
Map<String, dynamic> _getEventData(
SentryEvent event, {
DateTime timeStamp,
StackFrameFilter stackFrameFilter,
}) {
final data = <String, dynamic>{
'event_id': event.eventId.toString(),
};
if (_options.environmentAttributes != null) {
mergeAttributes(_options.environmentAttributes.toJson(), into: data);
}
mergeAttributes(
event.toJson(
stackFrameFilter: stackFrameFilter,
origin: origin,
),
into: data,
);
mergeAttributes(_getContext(timeStamp), into: data);
return data;
}
Map<String, dynamic> _getContext(DateTime now) => {
'project': dsn.projectId,
'timestamp': formatDateAsIso8601WithSecondPrecision(now),
'platform': platform,
};
}