Hi,
I want to make my web app to have an option to start telemetry or not. So, here’s I setup my config and release files first:
config :my_app,
app_title: System.get_env("APP_TITLE") || "MyApp",
app_image_url: System.get_env("APP_IMAGE_URL"),
live_dashboard: System.get_env("LIVE_DASHBOARD") || false
and in application.ex
I did this:
defmodule MyApp.Application do
@moduledoc false
use Application
def start(_type, _args) do
children = [
{Phoenix.PubSub, name: MyAppWeb.PubSub},
MyApp.Repo,
MyAppWeb.Endpoint
]
# Here I put my condition (optional)
children =
if Application.get_env(:my_app, :live_dashboard) == true,
do: List.insert_at(children, 0, MyAppWeb.Telemetry),
else: children
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
end
def config_change(changed, _new, removed) do
MyAppWeb.Endpoint.config_change(changed, removed)
:ok
end
end
However the Telemetry still works, sending data to dashboard.
So, I put same conditions in telemetry.ex
module but still happening. Still receiving the metrics.
defmodule ColivingWeb.Telemetry do
use Supervisor
import Telemetry.Metrics
def start_link(arg) do
if Application.get_env(:coliving, :live_dashboard) == true do
Supervisor.start_link(__MODULE__, arg, name: __MODULE__)
end
end
@impl true
def init(_arg) do
if Application.get_env(:coliving, :live_dashboard) == true do
children = [
{:telemetry_poller, measurements: periodic_measurements(), period: 10_000}
# Add reporters as children of your supervision tree.
# {Telemetry.Metrics.ConsoleReporter, metrics: metrics()}
]
Supervisor.init(children, strategy: :one_for_one)
end
end
def metrics do
[
# Phoenix Metrics
summary("phoenix.endpoint.stop.duration",
unit: {:native, :millisecond}
),
summary("phoenix.router_dispatch.stop.duration",
tags: [:route],
unit: {:native, :millisecond}
),
# Database Metrics
summary("coliving.repo.query.total_time", unit: {:native, :millisecond}),
summary("coliving.repo.query.decode_time", unit: {:native, :millisecond}),
summary("coliving.repo.query.query_time", unit: {:native, :millisecond}),
summary("coliving.repo.query.queue_time", unit: {:native, :millisecond}),
summary("coliving.repo.query.idle_time", unit: {:native, :millisecond}),
# VM Metrics
summary("vm.memory.total", unit: {:byte, :kilobyte}),
summary("vm.total_run_queue_lengths.total"),
summary("vm.total_run_queue_lengths.cpu"),
summary("vm.total_run_queue_lengths.io")
]
end
defp periodic_measurements do
[
# A module, function and arguments to be invoked periodically.
# This function must call :telemetry.execute/3 and a metric must be added above.
# {ColivingWeb, :count_users, []}
]
end
end
How can I stop it (make it optional)?
PS: Since, this is an open source project, I leave the project name in last code section. Any contributions are welcome.
Thank you!