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 Switch mode

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?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement