ODCounter
It is a :counters, but with arbitrary terms instead of indices. It has the performance of :counters with key-to-index associations resolved at compile-time. Plus it adds namespaces and significantly reduces amount of boilerplate around :counters.
The problem
You want to have global counters for things. Most common use-case is metrics. For example, you want to count amount of successful and failed requests to some service. You heard about :counters and you want to try it out in your project.
First thing you need to do is to initialize :counters and make it accessible from any process. Most common way to do this is via :persistent_term. So, you make a wrapper:
defmodule RequestsCounter do
@size 1024
def new do
counters = :counters.new(@size, [])
:persistent_term.put(:requests_counter, counters)
end
end
Okay, next, you wanna start counting. But the problem is that :counters works with indices, not with keys. So you gotta translate meaningful terms to cryptic. And you come up with idea to do this with a function like
defmodule RequestsCounter do
...
def index_of(:successful), do: 1
def index_of(:failed), do: 2
end
And now you can start doing something like
counters = :persistent_term.get(:requests_counter)
:counters.add(counters, RequestsCounter.index_of(:successful), 1)
Hooray, it works. But this ceremony will now be present in every place you want to change the counter. Plus, if you want to add a new counter, you will have to add another clause to index_of. And if you want to add some counters in runtime, well, you will need much more code than this.
The solution
Just use ODCounter.
First, you have to initialize it once with ODCounter.init(RequestsCounter).
Second, you just use the counters
# If you provide counter atom as the argument, it will be translated to index at compile-time
ODCounter.add(RequestsCounter, :successful, 3)
ODCounter.add(RequestsCounter, :failed, 6)
# ^ code above compiles to something like
# ref = :persistent_term.get(RequestsCounter)
# :counter.add(ref, 1, 3)
# :counters.add(ref, 2, 6)`
# And it fallbacks to runtime implmenetation in case the counter was created at runtime
ODCounter.add(RequestsCounter, {:status, request.status}, 1)
# ^ code above compiles to something like
# ref = :persistent_term.get(RequestsCounter)
# index = ODCounter.create_or_get_index(RequestsCounter, {:status, request.status})
# :counter.add(ref, index, 1)
Voila. No ceremony, efficient, easy to use






















