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

achempion
Hi, I would like to tell about my initiative to further maintain and develop Waffle project which is the fork of Arc library. The progre...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
danschultzer
I’ve moved the strategies out of PowAssent and created a multi-provider framework for Elixir. This way the strategies can be used in any ...
New
gtcode
Aloha gang, There’s a new class of AI model out from TypeSafe. Jev is their flagship model. You can learn about it here: ...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
AstonJ
With advancements in AI gathering pace and set to increasingly accelerate, perhaps it’s worth having an annual thread on the topic - feel...
New
bartblast
If you haven’t tried Hologram yet, what’s stopped you? Here’s why I’m asking. Building a framework turns out to be two jobs, the framewo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews