Last active
March 11, 2021 19:25
-
-
Save bruceharrison1984/6cd2768fd2a3c865d17b16f29787b2fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.EntityFrameworkCore.Diagnostics; | |
using System.Data.Common; | |
using System.Threading; | |
using System.Threading.Tasks; | |
// copied from https://github.com/aws/aws-xray-sdk-dotnet/blob/master/sdk/src/Handlers/EntityFramework/EFInterceptor.cs | |
// Xray doesn't support EF5 OOTB right now, so this lets us hack it back in. | |
// All we did was change all the Task<> to ValueTask<> | |
namespace MyProject.Data | |
{ | |
class XRayInterceptor : DbCommandInterceptor | |
{ | |
private readonly bool? _collectSqlQueriesOverride; | |
public XRayInterceptor(bool? collectSqlQueries = null) : base() | |
{ | |
_collectSqlQueriesOverride = collectSqlQueries; | |
} | |
/// <summary> | |
/// Trace before executing reader. | |
/// </summary> | |
/// <param name="command">Instance of <see cref="DbCommand"/>.</param> | |
/// <param name="eventData">Instance of <see cref="CommandEventData"/>.</param> | |
/// <param name="result">Result from <see cref="IInterceptor"/>.</param> | |
/// <returns>Result from <see cref="IInterceptor"/>.</returns> | |
public override InterceptionResult<DbDataReader> ReaderExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result) | |
{ | |
EFUtil.ProcessBeginCommand(command, _collectSqlQueriesOverride); | |
return base.ReaderExecuting(command, eventData, result); | |
} | |
/// <summary> | |
/// Trace after executing reader. | |
/// </summary> | |
/// <param name="command">Instance of <see cref="DbCommand"/>.</param> | |
/// <param name="eventData">Instance of <see cref="CommandExecutedEventData"/>.</param> | |
/// <param name="result">Instance of <see cref="DbDataReader"/>.</param> | |
/// <returns>Instance of <see cref="DbDataReader"/>.</returns> | |
public override DbDataReader ReaderExecuted(DbCommand command, CommandExecutedEventData eventData, DbDataReader result) | |
{ | |
EFUtil.ProcessEndCommand(); | |
return base.ReaderExecuted(command, eventData, result); | |
} | |
/// <summary> | |
/// Trace before executing reader asynchronously. | |
/// </summary> | |
/// <param name="command">Instance of <see cref="DbCommand"/>.</param> | |
/// <param name="eventData">Instance of <see cref="CommandEventData"/>.</param> | |
/// <param name="result">Result from <see cref="IInterceptor"/>.</param> | |
/// <param name="cancellationToken">Instance of <see cref="CancellationToken"/>.</param> | |
/// <returns>Task representing the async operation.</returns> | |
public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result, CancellationToken cancellationToken = default) | |
{ | |
EFUtil.ProcessBeginCommand(command, _collectSqlQueriesOverride); | |
return base.ReaderExecutingAsync(command, eventData, result, cancellationToken); | |
} | |
/// <summary> | |
/// Trace after executing reader asynchronously. | |
/// </summary> | |
/// <param name="command">Instance of <see cref="DbCommand"/>.</param> | |
/// <param name="eventData">Instance of <see cref="CommandExecutedEventData"/>.</param> | |
/// <param name="result">Result from <see cref="DbDataReader"/>.</param> | |
/// <param name="cancellationToken">Instance of <see cref="CancellationToken"/>.</param> | |
/// <returns>Task representing the async operation.</returns> | |
public override ValueTask<DbDataReader> ReaderExecutedAsync(DbCommand command, CommandExecutedEventData eventData, DbDataReader result, CancellationToken cancellationToken = default) | |
{ | |
EFUtil.ProcessEndCommand(); | |
return base.ReaderExecutedAsync(command, eventData, result, cancellationToken); | |
} | |
/// <summary> | |
/// Trace after command fails. | |
/// </summary> | |
/// <param name="command">Instance of <see cref="DbCommand"/>.</param> | |
/// <param name="eventData">Instance of <see cref="CommandErrorEventData"/>.</param> | |
public override void CommandFailed(DbCommand command, CommandErrorEventData eventData) | |
{ | |
EFUtil.ProcessCommandError(eventData.Exception); | |
base.CommandFailed(command, eventData); | |
} | |
/// <summary> | |
/// Trace after async command fails. | |
/// </summary> | |
/// <param name="command">Instance of <see cref="DbCommand"/>.</param> | |
/// <param name="eventData">Instance of <see cref="CommandErrorEventData"/>.</param> | |
/// <param name="cancellationToken">Instance of <see cref="CancellationToken"/>.</param> | |
/// <returns>Task representing the async operation.</returns> | |
public override Task CommandFailedAsync(DbCommand command, CommandErrorEventData eventData, CancellationToken cancellationToken = default) | |
{ | |
EFUtil.ProcessCommandError(eventData.Exception); | |
return base.CommandFailedAsync(command, eventData, cancellationToken); | |
} | |
/// <summary> | |
/// Trace before excuting. | |
/// </summary> | |
/// <param name="command">Instance of <see cref="DbCommand"/>.</param> | |
/// <param name="eventData">Instance of <see cref="CommandEventData"/>.</param> | |
/// <param name="result">Result from <see cref="IInterceptor"/>.</param> | |
/// <returns>Task representing the operation.</returns> | |
public override InterceptionResult<int> NonQueryExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<int> result) | |
{ | |
EFUtil.ProcessBeginCommand(command, _collectSqlQueriesOverride); | |
return base.NonQueryExecuting(command, eventData, result); | |
} | |
/// <summary> | |
/// Trace before executing asynchronously. | |
/// </summary> | |
/// <param name="command">Instance of <see cref="DbCommand"/>.</param> | |
/// <param name="eventData">Instance of <see cref="CommandEventData"/>.</param> | |
/// <param name="result">Result from <see cref="IInterceptor"/>.</param> | |
/// <param name="cancellationToken">Instance of <see cref="CancellationToken"/>.</param> | |
/// <returns>Task representing the async operation.</returns> | |
public override ValueTask<InterceptionResult<int>> NonQueryExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<int> result, CancellationToken cancellationToken = default) | |
{ | |
EFUtil.ProcessBeginCommand(command, _collectSqlQueriesOverride); | |
return base.NonQueryExecutingAsync(command, eventData, result, cancellationToken); | |
} | |
/// <summary> | |
/// Trace after executing. | |
/// </summary> | |
/// <param name="command">Instance of <see cref="DbCommand"/>.</param> | |
/// <param name="eventData">Instance of <see cref="CommandExecutedEventData"/>.</param> | |
/// <param name="result">Result as integer.</param> | |
/// <returns>Result as integer.</returns> | |
public override int NonQueryExecuted(DbCommand command, CommandExecutedEventData eventData, int result) | |
{ | |
EFUtil.ProcessEndCommand(); | |
return base.NonQueryExecuted(command, eventData, result); | |
} | |
/// <summary> | |
/// Trace after executing asynchronously. | |
/// </summary> | |
/// <param name="command">Instance of <see cref="DbCommand"/>.</param> | |
/// <param name="eventData">Instance of <see cref="CommandExecutedEventData"/>.</param> | |
/// <param name="result">Result as integer.</param> | |
/// <param name="cancellationToken">Instance of <see cref="CancellationToken"/>.</param> | |
/// <returns>Task representing the async operation.</returns> | |
public override ValueTask<int> NonQueryExecutedAsync(DbCommand command, CommandExecutedEventData eventData, int result, CancellationToken cancellationToken = default) | |
{ | |
EFUtil.ProcessEndCommand(); | |
return base.NonQueryExecutedAsync(command, eventData, result, cancellationToken); | |
} | |
/// <summary> | |
/// Trace before executing scalar. | |
/// </summary> | |
/// <param name="command">Instance of <see cref="DbCommand"/>.</param> | |
/// <param name="eventData">Instance of <see cref="CommandEventData"/>.</param> | |
/// <param name="result">Result from <see cref="IInterceptor"/>.</param> | |
/// <returns>Result from <see cref="IInterceptor"/>.</returns> | |
public override InterceptionResult<object> ScalarExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<object> result) | |
{ | |
EFUtil.ProcessBeginCommand(command, _collectSqlQueriesOverride); | |
return base.ScalarExecuting(command, eventData, result); | |
} | |
/// <summary> | |
/// Trace before executing scalar asynchronously. | |
/// </summary> | |
/// <param name="command">Instance of <see cref="DbCommand"/>.</param> | |
/// <param name="eventData">Instance of <see cref="CommandEventData"/>.</param> | |
/// <param name="result">Result from <see cref="IInterceptor"/>.</param> | |
/// <param name="cancellationToken">Instance of <see cref="CancellationToken"/>.</param> | |
/// <returns>Task representing the async operation.</returns> | |
public override ValueTask<InterceptionResult<object>> ScalarExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<object> result, CancellationToken cancellationToken = default) | |
{ | |
EFUtil.ProcessBeginCommand(command, _collectSqlQueriesOverride); | |
return base.ScalarExecutingAsync(command, eventData, result, cancellationToken); | |
} | |
/// <summary> | |
/// Trace after executing scalar. | |
/// </summary> | |
/// <param name="command">Instance of <see cref="DbCommand"/>.</param> | |
/// <param name="eventData">Instance of <see cref="CommandExecutedEventData"/>.</param> | |
/// <param name="result">Result object.</param> | |
/// <returns>Result object.</returns> | |
public override object ScalarExecuted(DbCommand command, CommandExecutedEventData eventData, object result) | |
{ | |
EFUtil.ProcessEndCommand(); | |
return base.ScalarExecuted(command, eventData, result); | |
} | |
/// <summary> | |
/// Trace after executing scalar asynchronously. | |
/// </summary> | |
/// <param name="command">Instance of <see cref="DbCommand"/>.</param> | |
/// <param name="eventData">Instance of <see cref="CommandExecutedEventData"/>.</param> | |
/// <param name="result">Result object.</param> | |
/// <param name="cancellationToken">Instance of <see cref="CancellationToken"/>.</param> | |
/// <returns>Task representing the async operation.</returns> | |
public override ValueTask<object> ScalarExecutedAsync(DbCommand command, CommandExecutedEventData eventData, object result, CancellationToken cancellationToken = default) | |
{ | |
EFUtil.ProcessEndCommand(); | |
return base.ScalarExecutedAsync(command, eventData, result, cancellationToken); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment