mrclumsy

mrclumsy

Summarising maximum, minimum and averages on database state/values as telemetry events

I’ve just started learning elixir and phoenix and am exploring telemetry and all that it’s capable off. So far I’ve followed the phoenix docs and some blog posts. I’ve set up a simple application to mimic a book store. There’s one table called Books and it holds information about author, genre, title, price etc.

A lot of the reference material I’ve looked at talks about sending information like query execution time as metrics and that’s super useful. As a learning exercise I’m trying to also send something like the max, min and avg price per genre. I’d like to view this information in Live Dashboard if possible.

The majority of my progress so far was made by first taking the steps outlined in the docs, and then by following this blog post to customise even further.

Here is my setup so far. I hope I’ve provided enough details!

lib/books_api/metrics_generator.ex

  def handle_info(:work, state) do
    books = BooksApi.Store.list_books()

    :telemetry.execute([:books_api, :work],
      %{
        duration: Enum.random(0..10),
        books: books
      }
    )

    schedule_work()

    {:noreply, state}
  end

lib/books_api/store.ex

  def list_books do
    Repo.all(Book)
  end

lib/books_api/application.ex

  def start(_type, _args) do
    children = [
      # Start the Ecto repository
      BooksApi.Repo,
      # Start the Telemetry supervisor
      BooksApiWeb.Telemetry,
      # Start the PubSub system
      {Phoenix.PubSub, name: BooksApi.PubSub},
      # Start the Endpoint (http/https)
      BooksApiWeb.Endpoint,
      # Start a worker by calling: BooksApi.Worker.start_link(arg)
      # {BooksApi.Worker, arg}
      BooksApi.MetricsGenerator
    ]

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: BooksApi.Supervisor]
    Supervisor.start_link(children, opts)
  end

lib/books_api_web/telemetry.ex

  def metrics do
    [
 
      .....

      # Custom Metrics
      summary("books_api.work.books")
    ]
  end

When I visit http://localhost:4000/dashboard/metrics?nav=books_api I can see all the default metrics that come with phoenix but nothing for books_api.work.books

I think this is because summary/2 can’t parse the list returned by BooksApi.Store.list_books but I’m struggling to figure out how to format the data so that summary/2 can provide max, min and avg information price and also split that by genre.

Have I missed a step (or ten) or is what I’m attempting not within the scope of what telemetry metrics were intended for?

Marked As Solved

kartheek

kartheek

Telemetry events are sent to storage using a collector. All this data ends up in some sort of time series database. Once in time series databases, data can be queried and displayed in dashboards or tables, etc.

I have presented 10000ft view of what happens with telemetry pipeline. All this is spread across different systems and timelines.

https://docs.timescale.com/promscale/latest/about-promscale/#architecture will provide you with some more details.

Build a Weather Station with Elixir and Nerves uses phoenix for processing weather data collected using hardware. Section excerpt - Time-Series Sensor Hub in the book provides you with an overview of its architecture.

You can use https://contex-charts.org/ to display charts in dashboards in elixir.

Based on intent of your learning exercise - you can try out one of the below:

  • learning about telemetry pipeline in production - you can look at Telemetry/Opentelemetry, TimescaleDB, Graphana, etc.
  • books data has something relating to time series like sales, popularity, ratings, etc - you can look at TimescaleDB and tool like graphana to display data.
  • visualise books data in dashboard using phoenix - use charting library like contex, etc with sql queries.
  • or learn more about phoenix live dashboard.

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I don’t think what you’re attempting is within the scope of telemetry metrics. Telemetry metrics is about reporting measurement information about the performance of your application, it isn’t about providing information about data in your database. That is to say, Telemetry exists to tell you how long your queries are taking, but not tell you how many rows are in the tables or what those rows contain.

What you’re looking for is more like a “Business Intelligence” (BI) tool that lets you explore data in your database and derive metrics, visualizations, and so forth about the data contained therein.

mrclumsy

mrclumsy

Thank you for all the links and resources @kartheek, I’ll be sure to take the time to look through them all properly. And thank you for the advice on next steps. I think I might start with looking at libraries like contex since that fits the immediate my immediate aim and then move on the rest.

Where Next?

Popular in Questions Top

fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one ("skipping" all the other ones)? Would mix ecto.rollback -v 2008090...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "eq...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53578 245
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement