Skip to content

Commit cd3fdba

Browse files
committed
Filter XEvent session by client_connection_id
1 parent fa82d38 commit cd3fdba

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

src/Microsoft.Data.SqlClient/tests/ManualTests/DataCommon/DataTestUtility.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ private void SetupXEvent(string eventSpecification, string targetSpecification)
11501150
{eventSpecification}
11511151
{targetSpecification}
11521152
WITH (
1153-
MAX_MEMORY=4096 KB,
1153+
MAX_MEMORY=16 MB,
11541154
EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,
11551155
MAX_DISPATCH_LATENCY={MaxXEventsLatencyS} SECONDS,
11561156
MAX_EVENT_SIZE=0 KB,

src/Microsoft.Data.SqlClient/tests/ManualTests/TracingTests/XEventsTracingTest.cs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,35 @@ public void XEventActivityIDConsistentWithTracing(string query, System.Data.Comm
2121
// where it can be recorded in an XEvent session. This is documented at:
2222
// https://learn.microsoft.com/en-us/sql/relational-databases/native-client/features/accessing-diagnostic-information-in-the-extended-events-log
2323

24-
using (SqlConnection xEventManagementConnection = new SqlConnection(DataTestUtility.TCPConnectionString))
25-
using (DataTestUtility.XEventScope xEventSession = new DataTestUtility.XEventScope(xEventManagementConnection,
26-
@"ADD EVENT SQL_STATEMENT_STARTING (ACTION (client_connection_id)),
27-
ADD EVENT RPC_STARTING (ACTION (client_connection_id))",
28-
"ADD TARGET ring_buffer"))
29-
{
30-
Guid connectionId;
31-
HashSet<string> ids;
24+
using SqlConnection activityConnection = new(DataTestUtility.TCPConnectionString);
25+
activityConnection.Open();
3226

33-
using (DataTestUtility.MDSEventListener TraceListener = new())
34-
using (SqlConnection connection = new(DataTestUtility.TCPConnectionString))
35-
{
36-
connection.Open();
37-
connectionId = connection.ClientConnectionId;
27+
Guid connectionId = activityConnection.ClientConnectionId;
28+
HashSet<string> ids;
3829

39-
using SqlCommand command = new(query, connection) { CommandType = commandType };
40-
using SqlDataReader reader = command.ExecuteReader();
41-
while (reader.Read())
42-
{
43-
// Flush data
44-
}
30+
using SqlConnection xEventManagementConnection = new(DataTestUtility.TCPConnectionString);
31+
using DataTestUtility.XEventScope xEventSession = new(xEventManagementConnection,
32+
$@"ADD EVENT SQL_STATEMENT_STARTING (ACTION (client_connection_id) WHERE (client_connection_id='{connectionId}')),
33+
ADD EVENT RPC_STARTING (ACTION (client_connection_id) WHERE (client_connection_id='{connectionId}'))",
34+
"ADD TARGET ring_buffer");
4535

46-
ids = TraceListener.ActivityIDs;
36+
using (DataTestUtility.MDSEventListener TraceListener = new())
37+
{
38+
using SqlCommand command = new(query, activityConnection) { CommandType = commandType };
39+
using SqlDataReader reader = command.ExecuteReader();
40+
while (reader.Read())
41+
{
42+
// Flush data
4743
}
4844

49-
XmlDocument eventList = xEventSession.GetEvents();
50-
// Get the associated activity ID from the XEvent session. We expect to see the same ID in the trace as well.
51-
string activityId = GetCommandActivityId(query, xEvent, connectionId, eventList);
52-
53-
Assert.Contains(activityId, ids);
45+
ids = TraceListener.ActivityIDs;
5446
}
47+
48+
XmlDocument eventList = xEventSession.GetEvents();
49+
// Get the associated activity ID from the XEvent session. We expect to see the same ID in the trace as well.
50+
string activityId = GetCommandActivityId(query, xEvent, connectionId, eventList);
51+
52+
Assert.Contains(activityId, ids);
5553
}
5654

5755
private static string GetCommandActivityId(string commandText, string eventName, Guid connectionId, XmlDocument xEvents)

0 commit comments

Comments
 (0)