【发布时间】:2021-03-08 00:59:07
【问题描述】:
我有宏可以帮助我测量延迟:
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: [] 错误。我在这里做错了什么?这是正确的方法吗?
【问题讨论】:
-
labels: [endpoint_name]→labels: [unquote(endpoint_name)],因为在编译过程中你应该被警告告知。如果你想拥有一个健壮的代码,编译器警告是不容忽视的。另外,很高兴看到整个错误消息,请下次发布。 -
谢谢你,阿列克谢!顺便说一句,我的错误信息很差,就像这样:
[msg: 'Unexpected error during request processing. Handler:\'Elixir.App.Module\'; Reason:undef\n', options: []]我可以用那个做点什么吗?我的代码中有什么东西会吞下完整的错误消息吗? -
嗯。我不知道“请求”在这里是什么意思以及它是如何处理的,但我会明确隔离测试以仅测试这个特定的宏。喜欢模拟
Histogram并直接测试Instrumenter.instrument_duration/2。
标签: macros elixir benchmarking duration