Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tag with caller info #24887

Merged
merged 8 commits into from
May 17, 2021
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
42 changes: 41 additions & 1 deletion src/EFCore/Extensions/EntityFrameworkQueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.ChangeTracking;
Expand Down Expand Up @@ -2562,7 +2563,15 @@ public static IQueryable<TEntity> AsTracking<TEntity>(

internal static readonly MethodInfo TagWithMethodInfo
= typeof(EntityFrameworkQueryableExtensions)
.GetRequiredDeclaredMethod(nameof(TagWith));
.GetRequiredDeclaredMethod(nameof(TagWith), mi => mi.GetParameters().Length == 2
&& mi.GetParameters().Select(p => p.ParameterType)
.SequenceEqual(new Type[] { typeof(IQueryable<>).MakeGenericType(mi.GetGenericArguments()), typeof(string) }));

internal static readonly MethodInfo TagWithCallerInfoMethodInfo
= typeof(EntityFrameworkQueryableExtensions)
.GetRequiredDeclaredMethod(nameof(TagWith), mi => mi.GetParameters().Length == 3
&& mi.GetParameters().Select(p => p.ParameterType)
.SequenceEqual(new Type[] { typeof(IQueryable<>).MakeGenericType(mi.GetGenericArguments()), typeof(string), typeof(int) }));

/// <summary>
/// Adds a tag to the collection of tags associated with an EF LINQ query. Tags are query annotations
Expand Down Expand Up @@ -2594,6 +2603,37 @@ source.Provider is EntityQueryProvider
: source;
}

/// <summary>
/// Adds a tag to the collection of tags associated with an EF LINQ query with source file name and line
/// where method was called that can provide contextual tracing information at different points in the query pipeline.
/// </summary>
/// <typeparam name="T"> The type of entity being queried. </typeparam>
/// <param name="source"> The source query. </param>
/// <param name="fromFile"> The file name where the method was called</param>
/// <param name="onLine"> The file line number where the method was called</param>
/// <returns> A new query annotated with the given tag. </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source" />
/// </exception>
public static IQueryable<T> TagWith<T>(
this IQueryable<T> source,
[NotParameterized][CallerFilePath] string? fromFile = null,
[NotParameterized][CallerLineNumber] int onLine = 0)
{
Check.NotNull(source, nameof(source));

return
source.Provider is EntityQueryProvider
? source.Provider.CreateQuery<T>(
Expression.Call(
instance: null,
method: TagWithCallerInfoMethodInfo.MakeGenericMethod(typeof(T)),
arg0: source.Expression,
arg1: Expression.Constant(fromFile),
arg2: Expression.Constant(onLine)))
: source;
}

#endregion

#region Load
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,17 @@ private void VerifyReturnType(Expression expression, ParameterExpression lambdaP
return visitedExpression;
}

if (genericMethodDefinition == EntityFrameworkQueryableExtensions.TagWithCallerInfoMethodInfo)
{
var visitedExpression = Visit(methodCallExpression.Arguments[0]);

var fileName = methodCallExpression.Arguments[1].GetConstantValue<string>();
var lineNo = methodCallExpression.Arguments[2].GetConstantValue<int>();
_queryCompilationContext.AddTag($"file: {fileName}:{lineNo}");

return visitedExpression;
}

if (genericMethodDefinition == EntityFrameworkQueryableExtensions.IgnoreQueryFiltersMethodInfo)
{
var visitedExpression = Visit(methodCallExpression.Arguments[0]);
Expand Down
7 changes: 7 additions & 0 deletions src/Shared/SharedTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ public static MethodInfo GetRequiredDeclaredMethod(this Type type, string name)
return method;
}

public static MethodInfo GetRequiredDeclaredMethod(this Type type, string name, Func<MethodInfo, bool> methodSelector)
{
var method = type.GetTypeInfo().GetDeclaredMethods(name).Single(methodSelector);

return method;
}

public static PropertyInfo GetRequiredDeclaredProperty(this Type type, string name)
{
var property = type.GetTypeInfo().GetDeclaredProperty(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Diagnostics;
Expand Down Expand Up @@ -65,6 +66,26 @@ public virtual async Task From_sql_queryable_stored_procedure_with_tag(bool asyn
&& mep.UnitPrice == 263.50m);
}

[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
public virtual async Task From_sql_queryable_stored_procedure_with_caller_info_tag(bool async)
{
using var context = CreateContext();
var query = context
.Set<MostExpensiveProduct>()
.FromSqlRaw(TenMostExpensiveProductsSproc, GetTenMostExpensiveProductsParameters())
.TagWith("SampleFileName", 13);

var queryResult = async
? await query.ToArrayAsync()
: query.ToArray();

var actual = query.ToQueryString().Split(Environment.NewLine).First();

Assert.Equal("-- file: SampleFileName:13", actual);
}

[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ public override async Task From_sql_queryable_stored_procedure_with_tag(bool asy
AssertSql(
@"-- Stored Procedure

[dbo].[Ten Most Expensive Products]");
}

public override async Task From_sql_queryable_stored_procedure_with_caller_info_tag(bool async)
{
await base.From_sql_queryable_stored_procedure_with_caller_info_tag(async);

AssertSql(
@"-- file: SampleFileName:13

[dbo].[Ten Most Expensive Products]");
}

Expand Down