Skip to content

Instantly share code, notes, and snippets.

@PaulDMendoza
Last active February 18, 2018 02:17
Show Gist options
  • Save PaulDMendoza/7249f13857f4c412d793bd61a5fd0f66 to your computer and use it in GitHub Desktop.
Save PaulDMendoza/7249f13857f4c412d793bd61a5fd0f66 to your computer and use it in GitHub Desktop.
Profiler for any ADO.NET connection and XRay
/// <summary>
/// This must be called NpgsqlConnection for Dapper to detect the connection as being an NpgsqlConnection.
/// Otherwise Dapper doesn't handle arrays correctly for Postgresql connections.
/// </summary>
public class NpgsqlConnection : StackExchange.Profiling.Data.ProfiledDbConnection
{
public NpgsqlConnection(DbConnection connection, IDbProfiler profiler) : base(connection, profiler)
{
}
}
public class AWSIDbProfiler : StackExchange.Profiling.Data.IDbProfiler
{
public static AWSIDbProfiler Instance { get; } = new AWSIDbProfiler();
public bool IsActive => true;
public void ExecuteStart(IDbCommand profiledDbCommand, SqlExecuteType executeType)
{
AWSXRayRecorder.Instance.BeginSubsegment("SQL Command");
AWSXRayRecorder.Instance.AddSqlInformation("sanitized_query", profiledDbCommand.CommandText);
AWSXRayRecorder.Instance.AddSqlInformation("connection_string", profiledDbCommand.Connection.ConnectionString);
}
public void ExecuteFinish(IDbCommand profiledDbCommand, SqlExecuteType executeType, DbDataReader reader)
{
AWSXRayRecorder.Instance.EndSubsegment();
}
public void OnError(IDbCommand profiledDbCommand, SqlExecuteType executeType, Exception exception)
{
AWSXRayRecorder.Instance.AddException(exception);
}
public void ReaderFinish(IDataReader reader)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment