-
-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathsentry_baggage.dart
More file actions
222 lines (181 loc) · 5.55 KB
/
sentry_baggage.dart
File metadata and controls
222 lines (181 loc) · 5.55 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import 'package:meta/meta.dart';
import 'protocol.dart';
import 'scope.dart';
import 'sentry_options.dart';
class SentryBaggage {
static const String _sampleRateKeyName = 'sentry-sample_rate';
static const String _sampleRandKeyName = 'sentry-sample_rand';
static const int _maxChars = 8192;
static const int _maxListMember = 64;
SentryBaggage(
this._keyValues, {
this.logger,
});
final Map<String, String> _keyValues;
final SentryLogger? logger;
String toHeaderString() {
final buffer = StringBuffer();
var listMemberCount = 0;
var separator = '';
for (final entry in _keyValues.entries) {
if (listMemberCount >= _maxListMember) {
logger?.call(
SentryLevel.info,
'Baggage key ${entry.key} dropped because of max list member.',
);
break;
}
try {
final encodedKey = _urlEncode(entry.key);
final encodedValue = _urlEncode(entry.value);
final encodedKeyValue = '$separator$encodedKey=$encodedValue';
final totalLengthIfValueAdded = buffer.length + encodedKeyValue.length;
if (totalLengthIfValueAdded >= _maxChars) {
logger?.call(
SentryLevel.info,
'Baggage key ${entry.key} dropped because of max baggage chars.',
);
continue;
}
listMemberCount++;
buffer.write(encodedKeyValue);
separator = ',';
} catch (exception, stackTrace) {
logger?.call(
SentryLevel.error,
'Failed to parse the baggage key ${entry.key}.',
exception: exception,
stackTrace: stackTrace,
);
// TODO rethrow in options.automatedTestMode (currently not available here to check)
}
}
return buffer.toString();
}
factory SentryBaggage.fromHeaderList(
List<String> headerValues, {
SentryLogger? logger,
}) {
final keyValues = <String, String>{};
for (final headerValue in headerValues) {
final keyValuesToAdd = _extractKeyValuesFromBaggageString(
headerValue,
logger: logger,
);
keyValues.addAll(keyValuesToAdd);
}
return SentryBaggage(keyValues, logger: logger);
}
factory SentryBaggage.fromHeader(
String headerValue, {
SentryLogger? logger,
}) {
final keyValues = _extractKeyValuesFromBaggageString(
headerValue,
logger: logger,
);
return SentryBaggage(keyValues, logger: logger);
}
@internal
setValuesFromScope(Scope scope, SentryOptions options) {
final propagationContext = scope.propagationContext;
setTraceId(propagationContext.traceId.toString());
setPublicKey(options.parsedDsn.publicKey);
if (options.release != null) {
setRelease(options.release!);
}
if (options.environment != null) {
setEnvironment(options.environment!);
}
if (scope.user?.id != null) {
setUserId(scope.user!.id!);
}
if (scope.replayId != null && scope.replayId != SentryId.empty()) {
setReplayId(scope.replayId.toString());
}
}
static Map<String, String> _extractKeyValuesFromBaggageString(
String headerValue, {
SentryLogger? logger,
}) {
final keyValues = <String, String>{};
final keyValueStrings = headerValue.split(',');
for (final keyValueString in keyValueStrings) {
// TODO: Note, value MAY contain any number of the equal sign (=) characters.
// Parsers MUST NOT assume that the equal sign is only used to separate key and value.
final keyAndValue = keyValueString.split('=');
if (keyAndValue.length == 2) {
try {
final key = _urlDecode(keyAndValue.first.trim());
final value = _urlDecode(keyAndValue.last.trim());
keyValues[key] = value;
} catch (exception, stackTrace) {
logger?.call(
SentryLevel.error,
'Failed to parse the baggage entry $keyAndValue.',
exception: exception,
stackTrace: stackTrace,
);
}
}
}
return keyValues;
}
static String _urlDecode(String uri) {
return Uri.decodeComponent(uri);
}
String _urlEncode(String uri) {
return Uri.encodeComponent(uri);
}
String? get(String key) => _keyValues[key];
void set(String key, String value) {
_keyValues[key] = value;
}
void setTraceId(String value) {
set('sentry-trace_id', value);
}
void setPublicKey(String value) {
set('sentry-public_key', value);
}
void setEnvironment(String value) {
set('sentry-environment', value);
}
void setRelease(String value) {
set('sentry-release', value);
}
void setUserId(String value) {
set('sentry-user_id', value);
}
void setTransaction(String value) {
set('sentry-transaction', value);
}
void setSampleRate(String value) {
set(_sampleRateKeyName, value);
}
void setSampleRand(String value) {
set(_sampleRandKeyName, value);
}
void setSampled(String value) {
set('sentry-sampled', value);
}
double? getSampleRate() {
final sampleRate = get(_sampleRateKeyName);
if (sampleRate == null) {
return null;
}
return double.tryParse(sampleRate);
}
double? getSampleRand() {
final sampleRand = get(_sampleRandKeyName);
if (sampleRand == null) {
return null;
}
return double.tryParse(sampleRand);
}
void setReplayId(String value) => set('sentry-replay_id', value);
SentryId? getReplayId() {
final replayId = get('sentry-replay_id');
return replayId == null ? null : SentryId.fromId(replayId);
}
Map<String, String> get keyValues => Map.unmodifiable(_keyValues);
}