Skip to content

Commit

Permalink
Fix | Revert Event source changes on TryBeginExecuteEvent and WriteEn…
Browse files Browse the repository at this point in the history
…dExecuteEvent to address the failure on other MS products. (#1258)
  • Loading branch information
JRahnama committed Sep 17, 2021
1 parent 831287f commit a93f86a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6581,7 +6581,7 @@ public SqlCommand Clone()

private void WriteBeginExecuteEvent()
{
SqlClientEventSource.Log.TryBeginExecuteEvent(ObjectID, Connection?.ClientConnectionId, CommandText);
SqlClientEventSource.Log.TryBeginExecuteEvent(ObjectID, Connection?.DataSource, Connection?.Database, CommandText, Connection?.ClientConnectionId);
}

private void WriteEndExecuteEvent(bool success, int? sqlExceptionNumber, bool synchronous)
Expand All @@ -6602,7 +6602,7 @@ private void WriteEndExecuteEvent(bool success, int? sqlExceptionNumber, bool sy

int compositeState = successFlag | isSqlExceptionFlag | synchronousFlag;

SqlClientEventSource.Log.TryEndExecuteEvent(ObjectID, Connection?.ClientConnectionId, compositeState, sqlExceptionNumber.GetValueOrDefault());
SqlClientEventSource.Log.TryEndExecuteEvent(ObjectID, compositeState, sqlExceptionNumber.GetValueOrDefault(), Connection?.ClientConnectionId);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ protected override DbParameterCollection DbParameterCollection
return Parameters;
}
}

internal static void CancelIgnoreFailureCallback(object state)
{
SqlCommand command = (SqlCommand)state;
Expand Down Expand Up @@ -3012,9 +3012,9 @@ protected override Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBehavior b
throw result.Exception.InnerException;
}
return result.Result;
},
CancellationToken.None,
TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.NotOnCanceled,
},
CancellationToken.None,
TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.NotOnCanceled,
TaskScheduler.Default
);
}
Expand Down Expand Up @@ -7611,7 +7611,7 @@ private SmiRequestExecutor SetUpSmiRequest(SqlInternalConnectionSmi innerConnect

private void WriteBeginExecuteEvent()
{
SqlClientEventSource.Log.TryBeginExecuteEvent(ObjectID, Connection?.ClientConnectionId, CommandText);
SqlClientEventSource.Log.TryBeginExecuteEvent(ObjectID, Connection?.DataSource, Connection?.Database, CommandText, Connection?.ClientConnectionId);
}

/// <summary>
Expand All @@ -7638,7 +7638,7 @@ private void WriteEndExecuteEvent(bool success, int? sqlExceptionNumber, bool sy

int compositeState = successFlag | isSqlExceptionFlag | synchronousFlag;

SqlClientEventSource.Log.TryEndExecuteEvent(ObjectID, Connection?.ClientConnectionId, compositeState, sqlExceptionNumber.GetValueOrDefault());
SqlClientEventSource.Log.TryEndExecuteEvent(ObjectID, compositeState, sqlExceptionNumber.GetValueOrDefault(), Connection?.ClientConnectionId);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ internal virtual void ReclaimedConnectionRequest() { /*no-op*/ }
#endregion
}

// Any changes to event writers might be considered as a breaking change.
// Other libraries such as OpenTelemetry and ApplicationInsight have based part of their code on BeginExecute and EndExecute arguments number.
[EventSource(Name = "Microsoft.Data.SqlClient.EventSource")]
internal partial class SqlClientEventSource : SqlClientEventSourceBase
{
Expand Down Expand Up @@ -510,22 +512,20 @@ internal void TryScopeLeaveEvent(long scopeId)

#region Execution Trace
[NonEvent]
internal void TryBeginExecuteEvent(int objectId, Guid? connectionId, string commandText, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "")
internal void TryBeginExecuteEvent(int objectId, string dataSource, string database, string commandText, Guid? connectionId, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "")
{
if (Log.IsExecutionTraceEnabled())
{
BeginExecute(GetFormattedMessage(SqlCommand_ClassName, memberName, EventType.INFO,
string.Format("Object Id {0}, Client Connection Id {1}, Command Text {2}", objectId, connectionId, commandText)));
BeginExecute(objectId, dataSource, database, commandText, GetFormattedMessage(SqlCommand_ClassName, memberName, EventType.INFO, $"Object Id {objectId}, Client connection Id {connectionId}, Command Text {commandText}"));
}
}

[NonEvent]
internal void TryEndExecuteEvent(int objectId, Guid? connectionId, int compositeState, int sqlExceptionNumber, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "")
internal void TryEndExecuteEvent(int objectId, int compositeState, int sqlExceptionNumber, Guid? connectionId, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "")
{
if (Log.IsExecutionTraceEnabled())
{
EndExecute(GetFormattedMessage(SqlCommand_ClassName, memberName, EventType.INFO,
string.Format("Object Id {0}, Client Connection Id {1}, Composite State {2}, Sql Exception Number {3}", objectId, connectionId, compositeState, sqlExceptionNumber)));
EndExecute(objectId, compositeState, sqlExceptionNumber, GetFormattedMessage(SqlCommand_ClassName, memberName, EventType.INFO, $"Object Id {objectId}, Client Connection Id {connectionId}, Composite State {compositeState}, Sql Exception Number {sqlExceptionNumber}"));
}
}
#endregion
Expand Down Expand Up @@ -995,13 +995,22 @@ internal void TrySNIScopeLeaveEvent(long scopeId)
#endregion

#region Write Events
// Do not change the first 4 arguments in this Event writer as OpenTelemetry and ApplicationInsight are relating to the same format,
// unless you have checked with them and they are able to change their design. Additional items could be added at the end.
[Event(BeginExecuteEventId, Keywords = Keywords.ExecutionTrace, Task = Tasks.ExecuteCommand, Opcode = EventOpcode.Start)]
internal void BeginExecute(string message) =>
WriteEvent(BeginExecuteEventId, message);
internal void BeginExecute(int objectId, string dataSource, string database, string commandText, string message)
{
WriteEvent(BeginExecuteEventId, objectId, dataSource, database, commandText, message);
}

// Do not change the first 3 arguments in this Event writer as OpenTelemetry and ApplicationInsight are relating to the same format,
// unless you have checked with them and they are able to change their design. Additional items could be added at the end.
[Event(EndExecuteEventId, Keywords = Keywords.ExecutionTrace, Task = Tasks.ExecuteCommand, Opcode = EventOpcode.Stop)]
internal void EndExecute(string message) =>
WriteEvent(EndExecuteEventId, message);
internal void EndExecute(int objectId, int compositestate, int sqlExceptionNumber, string message)
{

WriteEvent(EndExecuteEventId, objectId, compositestate, sqlExceptionNumber, message);
}

[Event(TraceEventId, Level = EventLevel.Informational, Keywords = Keywords.Trace)]
internal void Trace(string message) =>
Expand Down Expand Up @@ -1106,7 +1115,7 @@ internal static class EventType
public const string INFO = " | INFO | ";
public const string ERR = " | ERR | ";
}

internal readonly struct TrySNIEventScope : IDisposable
{
private readonly long _scopeId;
Expand Down

0 comments on commit a93f86a

Please sign in to comment.