Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,8 @@ protected virtual void OnMatchingEventWritten(EventWrittenEventArgs eventData)

public readonly ref struct XEventScope // : IDisposable
{
private const int MaxXEventsLatencyS = 5;

private readonly SqlConnection _connection;
private readonly bool _useDatabaseSession;

Expand Down Expand Up @@ -1126,6 +1128,8 @@ INNER JOIN sys.dm_xe_sessions AS xe

using (SqlCommand command = new SqlCommand(xEventQuery, _connection))
{
Thread.Sleep(MaxXEventsLatencyS * 1000);
Comment thread
samsharma2700 marked this conversation as resolved.

if (_connection.State == ConnectionState.Closed)
{
_connection.Open();
Expand All @@ -1146,9 +1150,9 @@ private void SetupXEvent(string eventSpecification, string targetSpecification)
{eventSpecification}
{targetSpecification}
WITH (
MAX_MEMORY=4096 KB,
MAX_MEMORY=16 MB,
EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,
MAX_DISPATCH_LATENCY=30 SECONDS,
MAX_DISPATCH_LATENCY={MaxXEventsLatencyS} SECONDS,
MAX_EVENT_SIZE=0 KB,
MEMORY_PARTITION_MODE=NONE,
TRACK_CAUSALITY=ON,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,35 @@ public void XEventActivityIDConsistentWithTracing(string query, System.Data.Comm
// where it can be recorded in an XEvent session. This is documented at:
// https://learn.microsoft.com/en-us/sql/relational-databases/native-client/features/accessing-diagnostic-information-in-the-extended-events-log

using (SqlConnection xEventManagementConnection = new SqlConnection(DataTestUtility.TCPConnectionString))
using (DataTestUtility.XEventScope xEventSession = new DataTestUtility.XEventScope(xEventManagementConnection,
@"ADD EVENT SQL_STATEMENT_STARTING (ACTION (client_connection_id)),
ADD EVENT RPC_STARTING (ACTION (client_connection_id))",
"ADD TARGET ring_buffer"))
{
Guid connectionId;
HashSet<string> ids;
using SqlConnection activityConnection = new(DataTestUtility.TCPConnectionString);
activityConnection.Open();

using (DataTestUtility.MDSEventListener TraceListener = new())
using (SqlConnection connection = new(DataTestUtility.TCPConnectionString))
{
connection.Open();
connectionId = connection.ClientConnectionId;
Guid connectionId = activityConnection.ClientConnectionId;
HashSet<string> ids;
Comment thread
paulmedynski marked this conversation as resolved.

using SqlCommand command = new(query, connection) { CommandType = commandType };
using SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
// Flush data
}
using SqlConnection xEventManagementConnection = new(DataTestUtility.TCPConnectionString);
using DataTestUtility.XEventScope xEventSession = new(xEventManagementConnection,
$@"ADD EVENT SQL_STATEMENT_STARTING (ACTION (client_connection_id) WHERE (client_connection_id='{connectionId}')),
ADD EVENT RPC_STARTING (ACTION (client_connection_id) WHERE (client_connection_id='{connectionId}'))",
"ADD TARGET ring_buffer");

ids = TraceListener.ActivityIDs;
using (DataTestUtility.MDSEventListener TraceListener = new())
{
using SqlCommand command = new(query, activityConnection) { CommandType = commandType };
using SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
// Flush data
}

XmlDocument eventList = xEventSession.GetEvents();
// Get the associated activity ID from the XEvent session. We expect to see the same ID in the trace as well.
string activityId = GetCommandActivityId(query, xEvent, connectionId, eventList);

Assert.Contains(activityId, ids);
ids = TraceListener.ActivityIDs;
}

XmlDocument eventList = xEventSession.GetEvents();
// Get the associated activity ID from the XEvent session. We expect to see the same ID in the trace as well.
string activityId = GetCommandActivityId(query, xEvent, connectionId, eventList);

Assert.Contains(activityId, ids);
}

private static string GetCommandActivityId(string commandText, string eventName, Guid connectionId, XmlDocument xEvents)
Expand Down
Loading