4

What Are Atoms in Elixir and How To Monitor Them With AppSignal

 2 years ago
source link: https://blog.appsignal.com/2021/09/21/what-are-atoms-in-elixir-and-how-to-monitor-them-with-appsignal.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

In this post, we’ll show you what atoms are in Elixir, why you should monitor them, and how to do so with AppSignal. Let’s prevent your app from crashing with a pop up of an error such as:

no more index entries in atom_tab (max=1048576)

What Is an Atom in Elixir and How Are They Stored?

An atom is one of the basic types in Elixir. They are named constants, and their value is their name. Atoms are great for sending messages and representing constants; they’re meant to be used as a developer tool.

One of the most common use cases of atoms is to use them as the key of a key-value tuple pair, e.g., the response of an HTTP request.

url = 'http://www.example.com/users'

case HTTPoison.get(url) do
  {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
    IO.puts body
  {:ok, %HTTPoison.Response{status_code: 404}} ->
    IO.puts "Not found :("
  {:error, %HTTPoison.Error{reason: reason}} ->
    IO.inspect reason
end

Atoms are everywhere in Elixir. If you’re using an Erlang module, you’ll notice that they’re accessed through an atom. Likewise, Elixir modules are atoms as well!

iex> Integer == :"Elixir.Integer"
true

true, false, nil, and aliases are atoms as well.

iex> true == :true
true
iex> false == :false
true
iex> nil == :nil
true

Once declared, atoms are stored in the atom table using an integer reference. This table has a default limit of 1048576. If the limit is hit at runtime, the application will crash and a crash dump generated to inform you about it.

As previously mentioned, atoms are meant to be used as developer tools and they should never be created dynamically during an application’s runtime.

Creating the Atom Tracking Dashboard on AppSignal

Since AppSignal’s motto is “doing all the heavy-lifting so that developers can focus on code”, we’ve added atoms to our Erlang Magic Dashboard. Magic dashboards are automatically created when you add AppSignal to your Elixir application.

Tracking atom creation in your application is a good way of discovering issues in your codebase related to dynamic atom creation. Hitting the atom table entries limit will cause your application to crash.

With AppSignal, you can also set up a dashboard to track atom limit and count. This dashboard will show a graphic with two lines: one for the defined atom limit from your application and the other for the current atom count.

You can also add this graph to a new or existing dashboard with the following JSON:

{
  "title": "Erlang Atoms",
  "description": "",
  "visuals": [
    {
      "title": "Atom usage",
      "description": "Usage of atoms in Erlang and the configured limit.",
      "line_label": "%name% %hostname%-%type%",
      "display": "LINE",
      "format": "number",
      "draw_null_as_zero": true,
      "metrics": [
        {
          "name": "erlang_atoms",
          "fields": [
            {
              "field": "GAUGE"
            }
          ],
          "tags": [
            {
              "key": "hostname",
              "value": "*"
            },
            {
              "key": "type",
              "value": "*"
            }
          ]
        }
      ],
      "type": "timeseries"
    }
  ]
}

We’ve set up a couple of scenarios demonstrating how a healthy and an unhealthy situation can look like.

A Healthy Application With a Constant Number of Atoms in Elixir

This example shows a healthy situation. When the application starts up, the atom count is increased and then it maintains a constant value over time.

healthy atoms count

Increasing the Number of Atoms in Elixir Over Time

In this example, you can see how the atom counter increases over time, hitting the limit and thus making the application crash.

This might be because we’re creating atoms dynamically at runtime. i.e. creating atoms based on arbitrary data sent in a web request, such as user attributes.

unhealthy atoms count

What Did We Learn?

If your app is crashing with the error no more index entries in atom_tab (max=1048576) or not performing as expected, you can take a look at the atom tracking graphics that can help you find some dynamically created atoms.

Check Out AppSignal, We Have Stroopwafels 🍪

If you haven’t yet tried AppSignal for monitoring your Elixir apps, take five minutes and check it out. There is a 30 day trial with no credit card required.

As we’re a Dutch company, we also ship stroopwafels (crazy good Dutch cookies) worldwide to our users. All you need to do to get your box is start your trial and reach out to our support 📧➡️😋


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK