Asd

Asd

ODCounter - :counters without ceremony

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

https://github.com/hissssst/odcounter

First Post!

tangui

tangui

Nice library, thanks for publishing it!

Two questions:

  • what does OD stands for?
  • have you considered making it an Erlang library (with a thin Elixir wrapper) so that our erlangist friends can use it as well?

Most Liked

Asd

Asd

Thanks. OD means On Demand. And its not possible to implement this functionality in Erlang, since ODCounter uses a lot of metaprogramming features available only in Elixir

tcoopman

tcoopman

Looks nice!

What are some example use cases counters are often used for?

Schultzer

Schultzer

You can use parse transform, and I actually would love to see that, at somepoint I want to rewrite my sql library to Erlang.

Last Post!

Schultzer

Schultzer

Hmm I thought parse transform just worked on the whole module vs Elixir hygienic macros.

Maybe one day Erlang we’ll have a better macro story.

Where Next?

Popular in Announcing Top

handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New
tmbb
I’ve been working on two packages (not on hex.pm yet) to build admin interfaces for phoenix apps: bureaucrat - which contains a bunch ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
kevinlang
Hey all, We have made an Ecto3 Adapter for SQLite3, ecto_sqlite3! We have successfully on-boarded the full suite of integration tests (...
New
zoltanszogyenyi
Hey everyone :waving_hand: Excited to join this forum - I am one of the founders and current project maintainers of a popular and open-s...
New
restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 10796 134
New
brainlid
LangChain is short for Language Chain. An LLM, or Large Language Model, is the “Language” part. This library makes it easier for Elixir a...
New

Other popular topics Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement