-
-
Notifications
You must be signed in to change notification settings - Fork 203
/
SentrySqlListener.cs
273 lines (224 loc) · 9.64 KB
/
SentrySqlListener.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
using Sentry.Extensibility;
using Sentry.Internal.Extensions;
namespace Sentry.Internal.DiagnosticSource;
internal class SentrySqlListener : IObserver<KeyValuePair<string, object?>>
{
private enum SentrySqlSpanType
{
Connection,
Execution
};
internal const string SqlDataWriteConnectionOpenBeforeCommand = "System.Data.SqlClient.WriteConnectionOpenBefore";
internal const string SqlMicrosoftWriteConnectionOpenBeforeCommand = "Microsoft.Data.SqlClient.WriteConnectionOpenBefore";
internal const string SqlMicrosoftWriteConnectionOpenAfterCommand = "Microsoft.Data.SqlClient.WriteConnectionOpenAfter";
internal const string SqlDataWriteConnectionOpenAfterCommand = "System.Data.SqlClient.WriteConnectionOpenAfter";
internal const string SqlMicrosoftWriteConnectionCloseAfterCommand = "Microsoft.Data.SqlClient.WriteConnectionCloseAfter";
internal const string SqlDataWriteConnectionCloseAfterCommand = "System.Data.SqlClient.WriteConnectionCloseAfter";
internal const string SqlDataBeforeExecuteCommand = "System.Data.SqlClient.WriteCommandBefore";
internal const string SqlMicrosoftBeforeExecuteCommand = "Microsoft.Data.SqlClient.WriteCommandBefore";
internal const string SqlDataAfterExecuteCommand = "System.Data.SqlClient.WriteCommandAfter";
internal const string SqlMicrosoftAfterExecuteCommand = "Microsoft.Data.SqlClient.WriteCommandAfter";
internal const string SqlDataWriteCommandError = "System.Data.SqlClient.WriteCommandError";
internal const string SqlMicrosoftWriteCommandError = "Microsoft.Data.SqlClient.WriteCommandError";
private readonly IHub _hub;
private readonly SentryOptions _options;
public SentrySqlListener(IHub hub, SentryOptions options)
{
_hub = hub;
_options = options;
}
private static void SetDatabaseName(ISpanTracer span, string databaseName)
{
Debug.Assert(databaseName != string.Empty);
span.SetExtra(OTelKeys.DbName, databaseName);
}
private static void SetDatabaseAddress(ISpanTracer span, string databaseAddress)
{
Debug.Assert(databaseAddress != string.Empty);
span.SetExtra(OTelKeys.DbServer, databaseAddress);
}
private static void SetConnectionId(ISpanTracer span, Guid? connectionId)
{
Debug.Assert(connectionId != Guid.Empty);
span.SetExtra(SqlKeys.DbConnectionId, connectionId);
}
private static void SetOperationId(ISpanTracer span, Guid? operationId)
{
Debug.Assert(operationId != Guid.Empty);
span.SetExtra(SqlKeys.DbOperationId, operationId);
}
private static Guid? TryGetOperationId(ISpanTracer span) => span.Extra.TryGetValue<string, Guid?>(SqlKeys.DbOperationId);
private static Guid? TryGetConnectionId(ISpanTracer span) => span.Extra.TryGetValue<string, Guid?>(SqlKeys.DbConnectionId);
private void AddSpan(string operation, object? value)
{
var transaction = _hub.GetTransactionIfSampled();
if (transaction == null)
{
return;
}
var parent = transaction.GetDbParentSpan();
var span = parent.StartChild(operation);
span.SetExtra(OTelKeys.DbSystem, "sql");
SetOperationId(span, value?.GetGuidProperty("OperationId"));
SetConnectionId(span, value?.GetGuidProperty("ConnectionId"));
}
private ISpanTracer? GetSpan(SentrySqlSpanType type, object? value)
{
var transaction = _hub.GetTransactionIfSampled();
if (transaction == null)
{
return null;
}
switch (type)
{
case SentrySqlSpanType.Execution:
var operationId = value?.GetGuidProperty("OperationId");
if (TryGetQuerySpan(transaction, operationId) is { } querySpan)
{
return querySpan;
}
_options.LogWarning(
"Trying to get an execution span with operation id {0}, but it was not found.",
operationId);
return null;
case SentrySqlSpanType.Connection:
var connectionId = value?.GetGuidProperty("ConnectionId");
if (TryGetConnectionSpan(transaction, connectionId) is { } connectionSpan)
{
return connectionSpan;
}
_options.LogWarning(
"Trying to get a connection span with connection id {0}, but it was not found.",
connectionId);
return null;
default:
throw new ArgumentOutOfRangeException(nameof(type), type, null);
}
}
private static ISpanTracer? TryGetConnectionSpan(ITransactionTracer transaction, Guid? connectionId) =>
connectionId == null
? null
: transaction.Spans
.FirstOrDefault(span =>
span is {IsFinished: false, Operation: "db.connection"} &&
TryGetConnectionId(span) == connectionId);
private static ISpanTracer? TryGetQuerySpan(ITransactionTracer transaction, Guid? operationId) =>
operationId == null
? null
: transaction.Spans.FirstOrDefault(span => TryGetOperationId(span) == operationId);
private void UpdateConnectionSpan(object? value)
{
if (value == null)
{
return;
}
var transaction = _hub.GetTransactionIfSampled();
if (transaction == null)
{
return;
}
var operationId = value.GetGuidProperty("OperationId");
if (operationId == null)
{
return;
}
var spans = transaction.Spans.Where(span => span.Operation is "db.connection").ToList();
if (spans.Find(span => !span.IsFinished && TryGetOperationId(span) == operationId) is { } connectionSpan)
{
if (value.GetGuidProperty("ConnectionId") is { } connectionId)
{
SetConnectionId(connectionSpan, connectionId);
}
if (value.GetStringProperty("Connection.Database") is { } dbName)
{
connectionSpan.Description = dbName;
SetDatabaseName(connectionSpan, dbName);
}
if (value.GetStringProperty("Connection.DataSource") is { } dbSource)
{
SetDatabaseAddress(connectionSpan, dbSource);
}
}
}
private void FinishCommandSpan(object? value, SpanStatus spanStatus)
{
if (value == null)
{
return;
}
if (GetSpan(SentrySqlSpanType.Execution, value) is not { } commandSpan)
{
return;
}
// Try to lookup the associated connection span so that we can store the db.name in
// the command span as well. This will be easier for users to read/identify than the
// ConnectionId (which is a Guid)
var connectionId = value.GetGuidProperty("ConnectionId");
var transaction = _hub.GetTransactionIfSampled();
if (TryGetConnectionSpan(transaction!, connectionId) is { } connectionSpan)
{
if (connectionSpan.Extra.TryGetValue<string, string>(OTelKeys.DbName) is { } dbName)
{
SetDatabaseName(commandSpan, dbName);
}
}
commandSpan.Description = value.GetStringProperty("Command.CommandText");
commandSpan.Finish(spanStatus);
}
public void OnCompleted() { }
public void OnError(Exception error) { }
public void OnNext(KeyValuePair<string, object?> kvp)
{
try
{
switch (kvp.Key)
{
// Query
case SqlMicrosoftBeforeExecuteCommand or SqlDataBeforeExecuteCommand:
AddSpan("db.query", kvp.Value);
return;
case SqlMicrosoftAfterExecuteCommand or SqlDataAfterExecuteCommand:
FinishCommandSpan(kvp.Value, SpanStatus.Ok);
return;
case SqlMicrosoftWriteCommandError or SqlDataWriteCommandError:
FinishCommandSpan(kvp.Value, SpanStatus.InternalError);
return;
// Connection
case SqlMicrosoftWriteConnectionOpenBeforeCommand or SqlDataWriteConnectionOpenBeforeCommand:
AddSpan("db.connection", kvp.Value);
return;
case SqlMicrosoftWriteConnectionOpenAfterCommand or SqlDataWriteConnectionOpenAfterCommand:
UpdateConnectionSpan(kvp.Value);
return;
case SqlMicrosoftWriteConnectionCloseAfterCommand or SqlDataWriteConnectionCloseAfterCommand
when GetSpan(SentrySqlSpanType.Connection, kvp.Value) is { } closeSpan:
TrySetConnectionStatistics(closeSpan, kvp.Value);
closeSpan.Finish(SpanStatus.Ok);
return;
}
}
catch (Exception ex)
{
_options.LogError(ex, "Failed to intercept SQL event.");
}
}
private static void TrySetConnectionStatistics(ISpanTracer span, object? value)
{
if (value?.GetProperty("Statistics") is not Dictionary<object, object> statistics)
{
return;
}
if (statistics["SelectRows"] is long selectRows)
{
span.SetExtra("rows_sent", selectRows);
}
if (statistics["BytesReceived"] is long bytesReceived)
{
span.SetExtra("bytes_received", bytesReceived);
}
if (statistics["BytesSent"] is long bytesSent)
{
span.SetExtra("bytes_sent ", bytesSent);
}
}
}