-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathTransaction.cs
410 lines (345 loc) · 13.6 KB
/
Transaction.cs
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
using Sentry.Extensibility;
using Sentry.Internal;
using Sentry.Internal.Extensions;
using Sentry.Protocol;
namespace Sentry;
// https://develop.sentry.dev/sdk/event-payloads/transaction
/// <summary>
/// Sentry performance transaction.
/// </summary>
public class Transaction : ITransactionData, IJsonSerializable
{
/// <summary>
/// Transaction's event ID.
/// </summary>
public SentryId EventId { get; private set; }
/// <inheritdoc />
public SpanId SpanId
{
get => Contexts.Trace.SpanId;
private set => Contexts.Trace.SpanId = value;
}
// A transaction normally does not have a parent because it represents
// the top node in the span hierarchy.
// However, a transaction may also be continued from a trace header
// (i.e. when another service sends a request to this service),
// in which case the newly created transaction refers to the incoming
// transaction as the parent.
/// <inheritdoc />
public SpanId? ParentSpanId
{
get => Contexts.Trace.ParentSpanId;
private set => Contexts.Trace.ParentSpanId = value;
}
/// <inheritdoc />
public SentryId TraceId
{
get => Contexts.Trace.TraceId;
private set => Contexts.Trace.TraceId = value;
}
/// <inheritdoc />
public string Name { get; private set; }
/// <inheritdoc />
public TransactionNameSource NameSource { get; }
/// <inheritdoc />
public bool? IsParentSampled { get; set; }
/// <inheritdoc />
public string? Platform { get; set; } = Constants.Platform;
/// <inheritdoc />
public string? Release { get; set; }
/// <inheritdoc />
public string? Distribution { get; set; }
/// <inheritdoc />
public DateTimeOffset StartTimestamp { get; private set; } = DateTimeOffset.UtcNow;
/// <inheritdoc />
public DateTimeOffset? EndTimestamp { get; internal set; } // internal for testing
// Not readonly because of deserialization
private Dictionary<string, Measurement>? _measurements;
/// <inheritdoc />
public IReadOnlyDictionary<string, Measurement> Measurements => _measurements ??= new Dictionary<string, Measurement>();
/// <inheritdoc />
public void SetMeasurement(string name, Measurement measurement) =>
(_measurements ??= new Dictionary<string, Measurement>())[name] = measurement;
/// <inheritdoc />
public string Operation
{
get => Contexts.Trace.Operation;
private set => Contexts.Trace.Operation = value;
}
/// <inheritdoc />
public string? Description
{
get => Contexts.Trace.Description;
set => Contexts.Trace.Description = value;
}
/// <inheritdoc />
public SpanStatus? Status
{
get => Contexts.Trace.Status;
private set => Contexts.Trace.Status = value;
}
/// <inheritdoc />
public bool? IsSampled
{
get => Contexts.Trace.IsSampled;
internal set
{
Contexts.Trace.IsSampled = value;
SampleRate ??= value == null ? null : value.Value ? 1.0 : 0.0;
}
}
/// <inheritdoc />
public double? SampleRate { get; internal set; }
/// <inheritdoc />
public SentryLevel? Level { get; set; }
private Request? _request;
/// <inheritdoc />
public Request Request
{
get => _request ??= new Request();
set => _request = value;
}
private readonly Contexts _contexts = new();
/// <inheritdoc />
public Contexts Contexts
{
get => _contexts;
set => _contexts.ReplaceWith(value);
}
private User? _user;
/// <inheritdoc />
public User User
{
get => _user ??= new User();
set => _user = value;
}
/// <inheritdoc />
public string? Environment { get; set; }
// This field exists on SentryEvent and Scope, but not on Transaction
string? IEventLike.TransactionName
{
get => Name;
set => Name = value ?? "";
}
/// <inheritdoc />
public SdkVersion Sdk { get; internal set; } = new();
private IReadOnlyList<string>? _fingerprint;
/// <inheritdoc />
public IReadOnlyList<string> Fingerprint
{
get => _fingerprint ?? Array.Empty<string>();
set => _fingerprint = value;
}
// Not readonly because of deserialization
private List<Breadcrumb> _breadcrumbs = new();
/// <inheritdoc />
public IReadOnlyCollection<Breadcrumb> Breadcrumbs => _breadcrumbs;
// Not readonly because of deserialization
private Dictionary<string, object?> _extra = new();
/// <inheritdoc />
public IReadOnlyDictionary<string, object?> Extra => _extra;
// Not readonly because of deserialization
private Dictionary<string, string> _tags = new();
/// <inheritdoc />
public IReadOnlyDictionary<string, string> Tags => _tags;
// Not readonly because of deserialization
private Span[] _spans = Array.Empty<Span>();
/// <summary>
/// Flat list of spans within this transaction.
/// </summary>
public IReadOnlyCollection<Span> Spans => _spans;
/// <inheritdoc />
public bool IsFinished => EndTimestamp is not null;
internal DynamicSamplingContext? DynamicSamplingContext { get; set; }
internal ITransactionProfiler? TransactionProfiler { get; set; }
// This constructor is used for deserialization purposes.
// It's required because some of the fields are mapped on 'contexts.trace'.
// When deserializing, we don't parse those fields explicitly, but
// instead just parse the trace context and resolve them later.
// Hence why we need a constructor that doesn't take the operation to avoid
// overwriting it.
private Transaction(string name, TransactionNameSource nameSource)
{
EventId = SentryId.Create();
Name = name;
NameSource = nameSource;
}
/// <summary>
/// Initializes an instance of <see cref="Transaction"/>.
/// </summary>
public Transaction(string name, string operation)
: this(name, TransactionNameSource.Custom)
{
SpanId = SpanId.Create();
TraceId = SentryId.Create();
Operation = operation;
}
/// <summary>
/// Initializes an instance of <see cref="Transaction"/>.
/// </summary>
public Transaction(string name, string operation, TransactionNameSource nameSource)
: this(name, nameSource)
{
SpanId = SpanId.Create();
TraceId = SentryId.Create();
Operation = operation;
}
/// <summary>
/// Initializes an instance of <see cref="Transaction"/>.
/// </summary>
public Transaction(ITransaction tracer)
: this(tracer.Name, tracer.NameSource)
{
// Contexts have to be set first because other fields use that
Contexts = tracer.Contexts;
ParentSpanId = tracer.ParentSpanId;
SpanId = tracer.SpanId;
TraceId = tracer.TraceId;
Operation = tracer.Operation;
Platform = tracer.Platform;
Release = tracer.Release;
Distribution = tracer.Distribution;
StartTimestamp = tracer.StartTimestamp;
EndTimestamp = tracer.EndTimestamp;
Description = tracer.Description;
Status = tracer.Status;
IsSampled = tracer.IsSampled;
Level = tracer.Level;
Request = tracer.Request;
User = tracer.User;
Environment = tracer.Environment;
Sdk = tracer.Sdk;
Fingerprint = tracer.Fingerprint;
_breadcrumbs = tracer.Breadcrumbs.ToList();
_extra = tracer.Extra.ToDictionary();
_tags = tracer.Tags.ToDictionary();
_spans = tracer.Spans
.Where(s => s is not SpanTracer { IsSentryRequest: true }) // Filter sentry requests created by Sentry.OpenTelemetry.SentrySpanProcessor
.Select(s => new Span(s)).ToArray();
_measurements = tracer.Measurements.ToDictionary();
// Some items are not on the interface, but we only ever pass in a TransactionTracer anyway.
if (tracer is TransactionTracer transactionTracer)
{
SampleRate = transactionTracer.SampleRate;
DynamicSamplingContext = transactionTracer.DynamicSamplingContext;
TransactionProfiler = transactionTracer.TransactionProfiler;
}
}
/// <inheritdoc />
public void AddBreadcrumb(Breadcrumb breadcrumb) =>
_breadcrumbs.Add(breadcrumb);
/// <inheritdoc />
public void SetExtra(string key, object? value) =>
_extra[key] = value;
/// <inheritdoc />
public void SetTag(string key, string value) =>
_tags[key] = value;
/// <inheritdoc />
public void UnsetTag(string key) =>
_tags.Remove(key);
/// <inheritdoc />
public SentryTraceHeader GetTraceHeader() => new(
TraceId,
SpanId,
IsSampled);
/// <summary>
/// Redacts PII from the transaction
/// </summary>
internal void Redact()
{
Description = Description?.RedactUrl();
foreach (var breadcrumb in Breadcrumbs)
{
breadcrumb.Redact();
}
foreach (var span in Spans)
{
span.Redact();
}
}
/// <inheritdoc />
public void WriteTo(Utf8JsonWriter writer, IDiagnosticLogger? logger)
{
writer.WriteStartObject();
writer.WriteString("type", "transaction");
writer.WriteSerializable("event_id", EventId, logger);
writer.WriteStringIfNotWhiteSpace("level", Level?.ToString().ToLowerInvariant());
writer.WriteStringIfNotWhiteSpace("platform", Platform);
writer.WriteStringIfNotWhiteSpace("release", Release);
writer.WriteStringIfNotWhiteSpace("dist", Distribution);
writer.WriteStringIfNotWhiteSpace("transaction", Name);
writer.WritePropertyName("transaction_info");
writer.WriteStartObject();
writer.WritePropertyName("source");
writer.WriteStringValue(NameSource.ToString().ToLowerInvariant());
writer.WriteEndObject();
writer.WriteString("start_timestamp", StartTimestamp);
writer.WriteStringIfNotNull("timestamp", EndTimestamp);
writer.WriteSerializableIfNotNull("request", _request, logger);
writer.WriteSerializableIfNotNull("contexts", _contexts.NullIfEmpty(), logger);
writer.WriteSerializableIfNotNull("user", _user, logger);
writer.WriteStringIfNotWhiteSpace("environment", Environment);
writer.WriteSerializable("sdk", Sdk, logger);
writer.WriteStringArrayIfNotEmpty("fingerprint", _fingerprint);
writer.WriteArrayIfNotEmpty("breadcrumbs", _breadcrumbs, logger);
writer.WriteDictionaryIfNotEmpty("extra", _extra, logger);
writer.WriteStringDictionaryIfNotEmpty("tags", _tags!);
writer.WriteArrayIfNotEmpty("spans", _spans, logger);
writer.WriteDictionaryIfNotEmpty("measurements", _measurements, logger);
writer.WriteEndObject();
}
/// <summary>
/// Parses transaction from JSON.
/// </summary>
public static Transaction FromJson(JsonElement json)
{
var eventId = json.GetPropertyOrNull("event_id")?.Pipe(SentryId.FromJson) ?? SentryId.Empty;
var name = json.GetProperty("transaction").GetStringOrThrow();
var nameSource = json.GetPropertyOrNull("transaction_info")?.GetPropertyOrNull("source")?
.GetString()?.ParseEnum<TransactionNameSource>() ?? TransactionNameSource.Custom;
var startTimestamp = json.GetProperty("start_timestamp").GetDateTimeOffset();
var endTimestamp = json.GetPropertyOrNull("timestamp")?.GetDateTimeOffset();
var level = json.GetPropertyOrNull("level")?.GetString()?.ParseEnum<SentryLevel>();
var platform = json.GetPropertyOrNull("platform")?.GetString();
var release = json.GetPropertyOrNull("release")?.GetString();
var distribution = json.GetPropertyOrNull("dist")?.GetString();
var request = json.GetPropertyOrNull("request")?.Pipe(Request.FromJson);
var contexts = json.GetPropertyOrNull("contexts")?.Pipe(Contexts.FromJson) ?? new();
var user = json.GetPropertyOrNull("user")?.Pipe(User.FromJson);
var environment = json.GetPropertyOrNull("environment")?.GetString();
var sdk = json.GetPropertyOrNull("sdk")?.Pipe(SdkVersion.FromJson) ?? new SdkVersion();
var fingerprint = json.GetPropertyOrNull("fingerprint")?
.EnumerateArray().Select(j => j.GetString()!).ToArray();
var breadcrumbs = json.GetPropertyOrNull("breadcrumbs")?
.EnumerateArray().Select(Breadcrumb.FromJson).ToList() ?? new();
var extra = json.GetPropertyOrNull("extra")?
.GetDictionaryOrNull() ?? new();
var tags = json.GetPropertyOrNull("tags")?
.GetStringDictionaryOrNull()?.WhereNotNullValue().ToDictionary() ?? new();
var measurements = json.GetPropertyOrNull("measurements")?
.GetDictionaryOrNull(Measurement.FromJson) ?? new();
var spans = json.GetPropertyOrNull("spans")?
.EnumerateArray().Select(Span.FromJson).ToArray() ?? Array.Empty<Span>();
return new Transaction(name, nameSource)
{
EventId = eventId,
StartTimestamp = startTimestamp,
EndTimestamp = endTimestamp,
Level = level,
Platform = platform,
Release = release,
Distribution = distribution,
_request = request,
Contexts = contexts,
_user = user,
Environment = environment,
Sdk = sdk,
_fingerprint = fingerprint,
_breadcrumbs = breadcrumbs,
_extra = extra,
_tags = tags,
_measurements = measurements,
_spans = spans
};
}
}