Asd

Asd

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

Showing Posts 1 to 6

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?
Asd

Asd OP

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.

Asd

Asd OP

Parse transform is much more limited. And using merl is not easy. Doing it in Erlang would mean rewriting the whole thing from scratch. I don’t want to do this. Though any contributions are welcome

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.

— All posts loaded —

Where Next? Top

Trending in Announcing Top

wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
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
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
fuelen
Hi all! I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas. You...
New
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
akoutmos
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews