I have macro which should help me to meaaure latency:
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
And following code that is using it:
response =
Instrumenter.instrument_duration(id,
do_handle(params, context)
|> prepare_response())
But I get Reason:undef\n', options: []
error. What I did wrong here? Is it correct way at all?
I am to put this as an answer here.
TL;DR: labels: [endpoint_name]
→ labels: [unquote(endpoint_name)]
Your code does not unquote endpoint_name
inside the quoted block, resulting in the unsuccessful compiler’s attempt to resolve endpoint_name
within the context of the quoted block.
Luckily enough, elixir provides warnings during the compilation stage, and there must have been somewhat like
warning: variable "endpoint_name" is unused (if the variable
is not meant to be used, prefix it with an underscore)
Compiler warnings are not to be ignored if one wants to have a robust code, they are provided on purpose and are to be taken into consideration.