arjan
Hi,
I am writing the Elixir integration for AppSignal, an application metrics solution. As of such, I am looking for a way to decorate functions in a developer-friendly way, so that these functions are automagically wrapped with library calls to measure the time it took to execute them (and send that info off to the backend).
As instrumenting functions is a common task while analyzing an application’s performance I want to minimize the amount of work the developer has to do to add instrumentation. As of such I have found two ways to do the instrumentation, both of which have its drawbacks:
1 - an instrumented do .... end block, in which you define the functions example here. Drawback of this method is that inside the block, everything is indented one level deeper, causing all the code to change while you have in fact just added 2 lines of code;making merging code changes harder;
2 - replace def foo() by def_instrument foo() (or similar) and use Kernel.def (like suggested here); the downside of this method is that it just “feels” weird to not read def, plus editor syntax highlighting breaks.
Are there any alternatives to tackle this? Ideally my solution would be a (Python / Java) decorator kind of syntax like this:
@instrumented
def foo(bar) do
...
But I am not sure that this is technically possible. I would love some input from the community on this!
cheers,
Arjan
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 11 to 20- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
arjan
Using an
@attribute does not work, I think, but creating adecorate()macro does, see this gist.That syntax is pretty OK, if you ask me:
Next step: creating macros to create decorator macros
OvermindDL1
Nah, that is fine if it is only for the AST to go over, it only matters to set something if you want to access it post-ast.
You could always do something like this too:
Hah, yep, could make a library for it all.
arjan
Doing just that! Right now I’m generating macros. Does not look so nice (no @) but it works pretty well. Let me know if you have a better way of doing this
benwilson512
One this thread hasn’t really talked about is whether the decorator pattern is actually a good idea in Elixir. I’m not entirely convinced that it is.
With instrumentation for example very frequently the caller site is the thing that should care about whether or not a given function call is instrumented. If you mark a function as instrumented with a decorator then every call to it is gonna have some kind of side effect whether that is actually desired or not.
arjan
I partly agree with you… however, I use Java a lot which got my mind twisted in the wrong direction maybe
The use cases that I see right now are:
Anyway, I’ve wrapped it into a library: GitHub - arjan/decorator: Function decorators for Elixir · GitHub
OvermindDL1
I think it was the
:tracemodule could allow you to hook functions, so you could define things to instrument based on module name and function name from the config or so?arjan
Also found a way to do it with
@but downside there is that it breaks “regular” module attributes..OvermindDL1
How so?
arjan
Because I do
import Kernel, except: [@: 1]and from my own
@macro I cant seem to call theKernel.@macroarjan
Fixed!