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?