feat: expose system metrics (#4)
Use prom_ex to expose system metrics to prometheus and create dashboards. Co-authored-by: Joao P Dubas <joao.dubas@gmail.com> Reviewed-on: #4
This commit is contained in:
@@ -7,6 +7,8 @@ defmodule Wabanex.Application do
|
||||
|
||||
def start(_type, _args) do
|
||||
children = [
|
||||
# Start the PromEx supervisor
|
||||
Wabanex.PromEx,
|
||||
# Start the Ecto repository
|
||||
Wabanex.Repo,
|
||||
# Start the Telemetry supervisor
|
||||
|
102
lib/wabanex/prom_ex.ex
Normal file
102
lib/wabanex/prom_ex.ex
Normal file
@@ -0,0 +1,102 @@
|
||||
defmodule Wabanex.PromEx do
|
||||
@moduledoc """
|
||||
Be sure to add the following to finish setting up PromEx:
|
||||
|
||||
1. Update your configuration (config.exs, dev.exs, prod.exs, releases.exs, etc) to
|
||||
configure the necessary bit of PromEx. Be sure to check out `PromEx.Config` for
|
||||
more details regarding configuring PromEx:
|
||||
```
|
||||
config :wabanex, Wabanex.PromEx,
|
||||
disabled: false,
|
||||
manual_metrics_start_delay: :no_delay,
|
||||
drop_metrics_groups: [],
|
||||
grafana: :disabled,
|
||||
metrics_server: :disabled
|
||||
```
|
||||
|
||||
2. Add this module to your application supervision tree. It should be one of the first
|
||||
things that is started so that no Telemetry events are missed. For example, if PromEx
|
||||
is started after your Repo module, you will miss Ecto's init events and the dashboards
|
||||
will be missing some data points:
|
||||
```
|
||||
def start(_type, _args) do
|
||||
children = [
|
||||
Wabanex.PromEx,
|
||||
|
||||
...
|
||||
]
|
||||
|
||||
...
|
||||
end
|
||||
```
|
||||
|
||||
3. Update your `endpoint.ex` file to expose your metrics (or configure a standalone
|
||||
server using the `:metrics_server` config options). Be sure to put this plug before
|
||||
your `Plug.Telemetry` entry so that you can avoid having calls to your `/metrics`
|
||||
endpoint create their own metrics and logs which can pollute your logs/metrics given
|
||||
that Prometheus will scrape at a regular interval and that can get noisy:
|
||||
```
|
||||
defmodule WabanexWeb.Endpoint do
|
||||
use Phoenix.Endpoint, otp_app: :wabanex
|
||||
|
||||
...
|
||||
|
||||
plug PromEx.Plug, prom_ex_module: Wabanex.PromEx
|
||||
|
||||
...
|
||||
end
|
||||
```
|
||||
|
||||
4. Update the list of plugins in the `plugins/0` function return list to reflect your
|
||||
application's dependencies. Also update the list of dashboards that are to be uploaded
|
||||
to Grafana in the `dashboards/0` function.
|
||||
"""
|
||||
|
||||
use PromEx, otp_app: :wabanex
|
||||
|
||||
alias PromEx.Plugins
|
||||
|
||||
@impl true
|
||||
def plugins do
|
||||
[
|
||||
# PromEx built in plugins
|
||||
Plugins.Application,
|
||||
Plugins.Beam,
|
||||
# {Plugins.Phoenix, router: WabanexWeb.Router, endpoint: WabanexWeb.Endpoint},
|
||||
Plugins.Ecto,
|
||||
# Plugins.Oban,
|
||||
# Plugins.PhoenixLiveView,
|
||||
Plugins.Absinthe
|
||||
# Plugins.Broadway,
|
||||
|
||||
# Add your own PromEx metrics plugins
|
||||
# Wabanex.Users.PromExPlugin
|
||||
]
|
||||
end
|
||||
|
||||
@impl true
|
||||
def dashboard_assigns do
|
||||
[
|
||||
datasource_id: "prometheus",
|
||||
default_selected_interval: "30s"
|
||||
]
|
||||
end
|
||||
|
||||
@impl true
|
||||
def dashboards do
|
||||
[
|
||||
# PromEx built in Grafana dashboards
|
||||
{:prom_ex, "application.json"},
|
||||
{:prom_ex, "beam.json"},
|
||||
# {:prom_ex, "phoenix.json"},
|
||||
{:prom_ex, "ecto.json"},
|
||||
# {:prom_ex, "oban.json"},
|
||||
# {:prom_ex, "phoenix_live_view.json"},
|
||||
{:prom_ex, "absinthe.json"}
|
||||
# {:prom_ex, "broadway.json"},
|
||||
|
||||
# Add your dashboard definitions here with the format: {:otp_app, "path_in_priv"}
|
||||
# {:wabanex, "/grafana_dashboards/user_metrics.json"}
|
||||
]
|
||||
end
|
||||
end
|
@@ -16,6 +16,8 @@ defmodule WabanexWeb.Endpoint do
|
||||
|
||||
socket "/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]]
|
||||
|
||||
plug PromEx.Plug, prom_ex_module: Wabanex.PromEx
|
||||
|
||||
# Serve at "/" the static files from "priv/static" directory.
|
||||
#
|
||||
# You should set gzip to true if you are running phx.digest
|
||||
|
@@ -17,10 +17,11 @@ defmodule WabanexWeb.Schema.Types.Custom.DateRange do
|
||||
end
|
||||
|
||||
@spec serialize_range(PgRanges.DateRange.t()) :: String.t()
|
||||
@spec serialize_range(list(DateTime.t() | nil)) :: String.t()
|
||||
@spec serialize_range(list(DateTime.t() | nil | atom)) :: String.t()
|
||||
defp serialize_range(%PgRanges.DateRange{lower: start_range, upper: end_range}),
|
||||
do: serialize_range([start_range, end_range])
|
||||
|
||||
defp serialize_range([start_range, :unbound]), do: serialize_range([start_range, nil])
|
||||
defp serialize_range([start_range, nil]), do: "#{Date.to_iso8601(start_range)},"
|
||||
|
||||
defp serialize_range([start_range, end_range]),
|
||||
|
Reference in New Issue
Block a user