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?

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.

2 Likes

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.
3 Likes

Thanks for the feedback @benwilson512, I was starting to get the idea that that might be the case. I’ll take a look at other options as you mentioned.

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.

1 Like