Skip to content

Commit

Permalink
Support Grafana via prom_ex
Browse files Browse the repository at this point in the history
  • Loading branch information
joshk committed Sep 13, 2024
1 parent 468b821 commit 9d4afc7
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 15 deletions.
12 changes: 12 additions & 0 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,18 @@ if System.get_env("SENTRY_DSN_URL") do
before_send: {NervesHubWeb.SentryEventFilter, :filter_non_500}
end

if System.get_env("GRAFANA_HOST") do
config :nerves_hub, NervesHub.PromEx,
manual_metrics_start_delay: :no_delay,
grafana: [
host: System.get_env("GRAFANA_HOST"),
auth_token: System.get_env("GRAFANA_TOKEN") || raise("GRAFANA_TOKEN is required"),
upload_dashboards_on_start: true,
folder_name: "NervesHub App Dashboards",
annotate_app_lifecycle: true
]
end

config :nerves_hub, :statsd,
host: System.get_env("STATSD_HOST", "localhost"),
port: String.to_integer(System.get_env("STATSD_PORT", "8125"))
Expand Down
5 changes: 4 additions & 1 deletion lib/nerves_hub/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ defmodule NervesHub.Application do
{Oban, Application.fetch_env!(:nerves_hub, Oban)}
] ++
deployments_supervisor(deploy_env()) ++
endpoints(deploy_env())
endpoints(deploy_env()) ++
[
NervesHub.PromEx
]

opts = [strategy: :one_for_one, name: NervesHub.Supervisor]
Supervisor.start_link(children, opts)
Expand Down
38 changes: 38 additions & 0 deletions lib/nerves_hub/prom_ex.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
defmodule NervesHub.PromEx do
use PromEx, otp_app: :nerves_hub

alias PromEx.Plugins

@impl true
def plugins do
[
# PromEx built in plugins
Plugins.Application,
Plugins.Beam,
{Plugins.Phoenix, router: NervesHubWeb.Router},
Plugins.PhoenixLiveView,
Plugins.Ecto,
Plugins.Oban
]
end

@impl true
def dashboard_assigns do
[
datasource_id: "prometheus"
]
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, "phoenix_live_view.json"},
{:prom_ex, "ecto.json"},
{:prom_ex, "oban.json"}
]
end
end
2 changes: 2 additions & 0 deletions lib/nerves_hub_web/device_endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ defmodule NervesHubWeb.DeviceEndpoint do

plug(NervesHubWeb.Plugs.ImAlive)

plug(PromEx.Plug, prom_ex_module: NervesHub.PromEx)

plug(Sentry.PlugContext)

plug(NervesHubWeb.Plugs.Logger)
Expand Down
2 changes: 2 additions & 0 deletions lib/nerves_hub_web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ defmodule NervesHubWeb.Endpoint do
cookie_key: "request_logger"
)

plug(PromEx.Plug, prom_ex_module: NervesHub.PromEx)

plug(Plug.RequestId)
plug(Plug.Telemetry, event_prefix: [:phoenix, :endpoint])
plug(NervesHubWeb.Plugs.Logger)
Expand Down
5 changes: 3 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,17 @@ defmodule NervesHub.MixProject do
{:phoenix_pubsub, "~> 2.0"},
{:phoenix_swoosh, "~> 1.0"},
{:phoenix_view, "~> 2.0"},
{:phoenix_test, "~> 0.3.0", only: :test, runtime: false},
{:phoenix_test, "0.3.1", only: :test, runtime: false},
{:plug, "~> 1.7"},
{:postgrex, "~> 0.14"},
{:prom_ex, "~> 1.10"},
{:scrivener_ecto, "~> 2.7"},
{:scrivener_html, git: "https://github.com/nerves-hub/scrivener_html", branch: "phx-1.5"},
{:sentry, "~> 10.0"},
{:slipstream, "~> 1.0", only: [:test, :dev]},
{:sweet_xml, "~> 0.6"},
{:swoosh, "~> 1.12"},
{:telemetry_metrics, "~> 0.4"},
{:telemetry_metrics, "~> 1.0"},
{:telemetry_metrics_statsd, "~> 0.7.0"},
{:telemetry_poller, "~> 1.0"},
{:timex, "~> 3.1"},
Expand Down
32 changes: 20 additions & 12 deletions mix.lock

Large diffs are not rendered by default.

0 comments on commit 9d4afc7

Please sign in to comment.