【发布时间】:2019-03-21 19:54:12
【问题描述】:
我一直在为我们的一些大型 .NET 解决方案实现 [<Trace>] 属性,这将允许将可配置分析轻松添加到任何被认为重要的功能/方法中。我正在使用 Fody 和MethodBoundaryAspect 来拦截每个函数的进入和退出并记录指标。这适用于同步函数,对于返回 Task 的方法,Task.ContinueWith 有一个可行的解决方案,但对于 F# Async-returning 函数,来自 MethodBoundaryAspect 的 OnExit 在 Async 返回后立即运行(而不是比实际执行异步时)。
为了捕获 F# 异步返回函数的正确指标,我试图想出一个等效的解决方案来使用 Task.ContinueWith,但我能想到的最接近的事情是创建一个新的 Async 来绑定第一个一,运行度量捕获函数,然后返回原始结果。由于我截获的 F# Async 返回值仅以obj 的形式呈现,这使情况更加复杂,此后我必须反思性地做所有事情,因为没有像 Async 那样的非通用版本Task 可以在不知道确切返回类型的情况下使用。
到目前为止,我最好的解决方案大致如下:
open System
open System.Diagnostics
open FSharp.Reflection
open MethodBoundaryAspect.Fody.Attributes
[<AllowNullLiteral>]
[<AttributeUsage(AttributeTargets.Method ||| AttributeTargets.Property, AllowMultiple = false)>]
type TraceAttribute () =
inherit OnMethodBoundaryAspect()
let traceEvent (args: MethodExecutionArgs) (timestamp: int64) =
// Capture metrics here
()
override __.OnEntry (args) =
Stopwatch.GetTimestamp() |> traceEvent args
override __.OnExit (args) =
let exit () = Stopwatch.GetTimestamp() |> traceEvent args
match args.ReturnValue with
| :? System.Threading.Tasks.Task as task ->
task.ContinueWith(fun _ -> exit()) |> ignore
| other -> // Here's where I could use some help
let clrType = other.GetType()
if clrType.IsGenericType && clrType.GetGenericTypeDefinition() = typedefof<Async<_>> then
// If the return type is an F# Async, replace it with a new Async that calls exit after the original return value is computed
let returnType = clrType.GetGenericArguments().[0]
let functionType = FSharpType.MakeFunctionType(returnType, typedefof<Async<_>>.MakeGenericType([| returnType |]))
let f = FSharpValue.MakeFunction(functionType, (fun _ -> exit(); other))
let result = typeof<AsyncBuilder>.GetMethod("Bind").MakeGenericMethod([|returnType; returnType|]).Invoke(async, [|other; f|])
args.ReturnValue <- result
else
exit()
不幸的是,这个解决方案不仅非常混乱,而且我相信异步计算的反射构造增加了不小的开销,尤其是当我试图跟踪在循环中调用的函数时深度嵌套的异步调用。有没有更好的方法来实现在实际评估异步计算后立即运行给定函数的相同结果?
【问题讨论】:
标签: f# fody continuewith async-workflow