This repository was archived by the owner on Nov 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTraceDbCommand.cs
More file actions
173 lines (145 loc) · 5.19 KB
/
TraceDbCommand.cs
File metadata and controls
173 lines (145 loc) · 5.19 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
using System;
using System.Data;
using System.Data.Common;
namespace DataDog.Tracing.Sql.EntityFrameworkCore
{
public class TraceDbCommand : DbCommand
{
private const string DefaultServiceName = "sql";
private const string TypeName = "sql";
private string ServiceName { get; }
private readonly DbCommand _command;
private readonly ISpanSource _spanSource;
public IDbCommand InnerCommand => _command;
protected override DbParameterCollection DbParameterCollection => _command.Parameters;
public override bool DesignTimeVisible
{
get => _command.DesignTimeVisible;
set => _command.DesignTimeVisible = value;
}
public override string CommandText
{
get => _command.CommandText;
set => _command.CommandText = value;
}
public override int CommandTimeout
{
get => _command.CommandTimeout;
set => _command.CommandTimeout = value;
}
public override CommandType CommandType
{
get => _command.CommandType;
set => _command.CommandType = value;
}
protected override DbConnection DbConnection
{
get => _command.Connection;
set => _command.Connection = value;
}
public override UpdateRowSource UpdatedRowSource
{
get => _command.UpdatedRowSource;
set => _command.UpdatedRowSource = value;
}
protected override DbTransaction DbTransaction
{
get => _command.Transaction;
set => _command.Transaction =
value is TraceDbTransaction transaction
? transaction.Transaction
: value;
}
public TraceDbCommand(DbCommand command)
: this(command, DefaultServiceName, TraceContextSpanSource.Instance) { }
public TraceDbCommand(DbCommand command, string serviceName)
: this(command, serviceName, TraceContextSpanSource.Instance) { }
public TraceDbCommand(DbCommand command, ISpanSource spanSource)
: this(command, DefaultServiceName, spanSource) { }
public TraceDbCommand(DbCommand command, string serviceName, ISpanSource spanSource)
{
_command = command ?? throw new ArgumentNullException(nameof(command));
_spanSource = spanSource ?? throw new ArgumentNullException(nameof(spanSource));
ServiceName = string.IsNullOrWhiteSpace(serviceName)
? DefaultServiceName
: serviceName;
}
public new void Dispose() => _command.Dispose();
public override void Cancel() => _command.Cancel();
public override void Prepare() => _command.Prepare();
protected override DbParameter CreateDbParameter() => _command.CreateParameter();
protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
{
const string name = "sql." + nameof(ExecuteReader);
var span = _spanSource.Begin(name, ServiceName, _command.Connection.Database, TypeName);
try
{
if (span != null)
{
const string metaKey = "sql." + nameof(CommandBehavior);
span.SetMeta(metaKey, behavior.ToString("x"));
SetMeta(span);
}
return _command.ExecuteReader(behavior);
}
catch (Exception ex)
{
span?.SetError(ex);
throw;
}
finally
{
span?.Dispose();
}
}
public override int ExecuteNonQuery()
{
const string name = "sql." + nameof(ExecuteNonQuery);
var span = _spanSource.Begin(name, ServiceName, _command.Connection.Database, TypeName);
try
{
var result = _command.ExecuteNonQuery();
if (span != null)
{
span.SetMeta("sql.RowsAffected", result.ToString());
SetMeta(span);
}
return result;
}
catch (Exception ex)
{
span?.SetError(ex);
throw;
}
finally
{
span?.Dispose();
}
}
public override object ExecuteScalar()
{
const string name = "sql." + nameof(ExecuteScalar);
var span = _spanSource.Begin(name, ServiceName, _command.Connection.Database, TypeName);
try
{
if (span != null)
SetMeta(span);
return _command.ExecuteScalar();
}
catch (Exception ex)
{
span?.SetError(ex);
throw;
}
finally
{
span?.Dispose();
}
}
private void SetMeta(ISpan span)
{
span.SetMeta("sql.CommandText", CommandText);
span.SetMeta("sql.CommandType", CommandType.ToString());
}
}
}