Help using erlang :counters - (ArgumentError) issue

Hi, is anyone using the new-ish erlang counters, please?

Could you please show some examples? I was able to create new counter with {:atomics, counter_ref} = :counters.new(1, []) but then :counters.info(counter_ref)is throwing (ArgumentError) – I guess I’m just wrongly “translating” from Erlang…

Also, what are good and bad use cases for these counters, please?

Thank you!

Yes, the counter_ref is whole value returned by :counters.new/2, not just second value in tuple. In fact you should never pattern match on it and treat is as an opaque value.

6 Likes

ah bingo, that works! thank you

any hints when to use these, and on the contrary, when not?

I was thinking of using Redis for concurrent counter recently, but was looking also for ETS alternatives, when I found these counters. I assume that unlike in Redis, these are not persisted in any way by default?

1 Like

Yes, these aren’t persisted and aren’t distributed in any way.

It is handy in situations like metrics where you just want to gather counts as cheaply as possible. Other use cases are logging facility where you want some kind of overload protection, that will cheaply count incoming requests and then react respectively in case of overload. You can also use them for implementing circuit breakers or any other situation where you need to count things cross process as cheaply as possible.

5 Likes