我有宏,它应该可以帮助我减少延迟:
defmacro instrument_duration(endpoint_name, block) do
quote do
{time, value} = :timer.tc(fn -> unquote(block) end)
Histogram.observe(
[name: :endpoint_duration_seconds, labels: [endpoint_name]],
time
)
value
end
end下面是使用它的代码:
response =
Instrumenter.instrument_duration(id,
do_handle(params, context)
|> prepare_response())但我得到了Reason:undef\n', options: []错误。我在这里做错了什么?这条路对吗?
发布于 2020-11-26 07:31:04
我要在这里回答这个问题。
TL;DR: labels: [endpoint_name]→labels: [unquote(endpoint_name)]
您的代码不会取消引用块中的endpoint_name,导致不成功的编译器试图在引用块的上下文中解析endpoint_name。
幸运的是,长生药在编译阶段提供了警告,而且一定有一些类似的
warning: variable "endpoint_name" is unused (if the variable
is not meant to be used, prefix it with an underscore)如果想要有一个健壮的代码,编译器警告是不能被忽略的,它们是故意提供的,并且是要考虑的。
https://stackoverflow.com/questions/65000145
复制相似问题