-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransactionWorker.cs
159 lines (146 loc) · 5.63 KB
/
TransactionWorker.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
using ContribSentry.Enums;
using ContribSentry.Extensibility;
using ContribSentry.Extensions;
using ContribSentry.Interface;
using ContribSentry.Internals.EventProcessor;
using Sentry;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace ContribSentry.Internals
{
internal class TransactionWorker : ITransactionWorker
{
internal AsyncLocal<SentryTracing> CurrentTransaction = new AsyncLocal<SentryTracing>();
internal AsyncLocal<ISpanBase> CurrentSpan = new AsyncLocal<ISpanBase>();
private SentryTracingEventProcessor _tracingEventProcessor;
private ContribSentryOptions _options;
public void Init(ContribSentryOptions options)
{
_options = options;
_tracingEventProcessor = new SentryTracingEventProcessor(options);
}
public void Close() { }
public ISentryTracing GetCurrentTransaction()
=> CurrentTransaction.Value ?? (ISentryTracing)DisabledTracing.Instance;
public ISpanBase GetCurrentTracingSpan()
=> CurrentSpan.Value ?? (ISpanBase)DisabledSession.Instance;
public Task StartTransaction(string name, string method, Action<ISentryTracing> action)
=> Task.Run(async () =>
{
ISentryTracing tracing = DisabledTracing.Instance;
if (CurrentTransaction.Value is null)
{
CurrentTransaction.Value = new SentryTracing(name, method);
tracing = CurrentTransaction.Value;
}
try
{
action(tracing);
tracing.Finish();
}
catch (Exception ex)
{
tracing.Finish(ex);
throw;
}
});
public Task StartChild(string url, ESpanRequest requestType, Action<ISpanBase> action)
=> Task.Run(() =>
{
if (CurrentSpan.Value is null)
{
var transaction = GetCurrentTransaction();
CurrentSpan.Value = transaction.StartChild(url, requestType);
}
else
{
CurrentSpan.Value = CurrentSpan.Value.StartChild(url, requestType);
}
try
{
action(CurrentSpan.Value);
CurrentSpan.Value.Finish();
}
catch (Exception ex)
{
// CurrentSpan.Value.Finish();
}
});
public Task StartChild(string description, string op, Action<ISpanBase> action)
=> Task.Run(() =>
{
if (CurrentSpan.Value is null)
{
var transaction = GetCurrentTransaction();
CurrentSpan.Value = transaction.StartChild(description, op);
}
else
{
CurrentSpan.Value = CurrentSpan.Value.StartChild(description, op);
}
try
{
action(CurrentSpan.Value);
CurrentSpan.Value.Finish();
}
catch (Exception ex)
{
CurrentSpan.Value.Finish(ex);
}
});
public Task StartChild(string description, string op, Func<ISpanBase, Task> func)
=> Task.Run(async () =>
{
if (CurrentSpan.Value is null)
{
var transaction = GetCurrentTransaction();
CurrentSpan.Value = transaction.StartChild(description, op);
}
else
{
CurrentSpan.Value = CurrentSpan.Value.StartChild(description, op);
}
try
{
await func(CurrentSpan.Value);
CurrentSpan.Value.Finish();
}
catch (Exception ex)
{
CurrentSpan.Value.Finish(ex);
}
});
/// <summary>
/// Captures the Transaction that was completed.
/// </summary>
/// <param name="tracing">The transaction.</param>
/// <param name="ex">The exception.</param>
public void CaptureTransaction(SentryTracing tracing, Exception ex)
{
var hasError = ex != null;
if (!hasError)
{
hasError = tracing.Spans.Any(p => p.Error);
tracing.Trace.SetStatus(tracing.Spans.LastOrDefault()?.Status);
}
else
{
tracing.Trace.SetStatus(SpanStatus.FromException(ex).ToString());
}
if (_options.RegisterTracingBreadcrumb)
{
SentrySdk.AddBreadcrumb(tracing.EventId.ToString(), "sentry.transaction");
}
SentrySdk.WithScope(scope =>
{
tracing = _tracingEventProcessor.Process(tracing, scope);
if (tracing != null)
{
ContribSentrySdk.Transport.CaptureTracing(tracing);
}
});
}
}
}