-
-
Notifications
You must be signed in to change notification settings - Fork 221
/
sentry_span.dart
203 lines (165 loc) · 4.39 KB
/
sentry_span.dart
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
import 'dart:async';
import '../hub.dart';
import '../protocol.dart';
import '../sentry_tracer.dart';
import '../tracing.dart';
import '../utils.dart';
class SentrySpan extends ISentrySpan {
final SentrySpanContext _context;
DateTime? _endTimestamp;
late final DateTime _startTimestamp;
final Hub _hub;
final SentryTracer _tracer;
final Map<String, dynamic> _data = {};
dynamic _throwable;
SpanStatus? _status;
final Map<String, String> _tags = {};
Function({DateTime? endTimestamp})? _finishedCallback;
@override
final SentryTracesSamplingDecision? samplingDecision;
SentrySpan(
this._tracer,
this._context,
this._hub, {
DateTime? startTimestamp,
this.samplingDecision,
Function({DateTime? endTimestamp})? finishedCallback,
}) {
_startTimestamp = startTimestamp?.toUtc() ?? _hub.options.clock();
_finishedCallback = finishedCallback;
}
@override
Future<void> finish({SpanStatus? status, DateTime? endTimestamp}) async {
if (finished) {
return;
}
if (status != null) {
_status = status;
}
if (endTimestamp == null) {
_endTimestamp = _hub.options.clock();
} else if (endTimestamp.isBefore(_startTimestamp)) {
_hub.options.logger(
SentryLevel.warning,
'End timestamp ($endTimestamp) cannot be before start timestamp ($_startTimestamp)',
);
_endTimestamp = _hub.options.clock();
} else {
_endTimestamp = endTimestamp.toUtc();
}
// associate error
if (_throwable != null) {
_hub.setSpanContext(_throwable, this, _tracer.name);
}
await _finishedCallback?.call(endTimestamp: _endTimestamp);
return super.finish(status: status, endTimestamp: _endTimestamp);
}
@override
void removeData(String key) {
if (finished) {
return;
}
_data.remove(key);
}
@override
void removeTag(String key) {
if (finished) {
return;
}
_tags.remove(key);
}
@override
void setData(String key, value) {
if (finished) {
return;
}
_data[key] = value;
}
@override
void setTag(String key, String value) {
if (finished) {
return;
}
_tags[key] = value;
}
@override
ISentrySpan startChild(
String operation, {
String? description,
DateTime? startTimestamp,
}) {
if (finished) {
return NoOpSentrySpan();
}
if (startTimestamp?.isBefore(_startTimestamp) ?? false) {
_hub.options.logger(
SentryLevel.warning,
"Start timestamp ($startTimestamp) cannot be before parent span's start timestamp ($_startTimestamp). Returning NoOpSpan.",
);
return NoOpSentrySpan();
}
return _tracer.startChildWithParentSpanId(
_context.spanId,
operation,
description: description,
startTimestamp: startTimestamp,
);
}
@override
SpanStatus? get status => _status;
@override
set status(SpanStatus? status) => _status = status;
@override
DateTime get startTimestamp => _startTimestamp;
@override
DateTime? get endTimestamp => _endTimestamp;
@override
SentrySpanContext get context => _context;
Map<String, dynamic> toJson() {
final json = _context.toJson();
json['start_timestamp'] =
formatDateAsIso8601WithMillisPrecision(_startTimestamp);
if (_endTimestamp != null) {
json['timestamp'] =
formatDateAsIso8601WithMillisPrecision(_endTimestamp!);
}
if (_data.isNotEmpty) {
json['data'] = _data;
}
if (status != null) {
json['status'] = status.toString();
}
if (_tags.isNotEmpty) {
json['tags'] = _tags;
}
return json;
}
@override
bool get finished => _endTimestamp != null;
@override
dynamic get throwable => _throwable;
@override
set throwable(throwable) => _throwable = throwable;
Map<String, String> get tags => _tags;
Map<String, dynamic> get data => _data;
@override
SentryTraceHeader toSentryTrace() => SentryTraceHeader(
_context.traceId,
_context.spanId,
sampled: samplingDecision?.sampled,
);
@override
void setMeasurement(
String name,
num value, {
SentryMeasurementUnit? unit,
}) {
_tracer.setMeasurement(name, value, unit: unit);
}
@override
SentryBaggageHeader? toBaggageHeader() => _tracer.toBaggageHeader();
@override
SentryTraceContextHeader? traceContext() => _tracer.traceContext();
@override
void scheduleFinish() {}
}