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
restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 11030 135
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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

Other Trending Topics Top

mudasobwa
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
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
lawik
I was thinking since Goatmire Elixir turned out pretty good I should maybe do another one. 30th of Sep - 2nd of Oct this year./ The firs...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews