Skip to content

Commit 9d4afc7

Browse files
committed
Support Grafana via prom_ex
1 parent 468b821 commit 9d4afc7

File tree

7 files changed

+81
-15
lines changed

7 files changed

+81
-15
lines changed

config/runtime.exs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,18 @@ if System.get_env("SENTRY_DSN_URL") do
358358
before_send: {NervesHubWeb.SentryEventFilter, :filter_non_500}
359359
end
360360

361+
if System.get_env("GRAFANA_HOST") do
362+
config :nerves_hub, NervesHub.PromEx,
363+
manual_metrics_start_delay: :no_delay,
364+
grafana: [
365+
host: System.get_env("GRAFANA_HOST"),
366+
auth_token: System.get_env("GRAFANA_TOKEN") || raise("GRAFANA_TOKEN is required"),
367+
upload_dashboards_on_start: true,
368+
folder_name: "NervesHub App Dashboards",
369+
annotate_app_lifecycle: true
370+
]
371+
end
372+
361373
config :nerves_hub, :statsd,
362374
host: System.get_env("STATSD_HOST", "localhost"),
363375
port: String.to_integer(System.get_env("STATSD_PORT", "8125"))

lib/nerves_hub/application.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ defmodule NervesHub.Application do
3939
{Oban, Application.fetch_env!(:nerves_hub, Oban)}
4040
] ++
4141
deployments_supervisor(deploy_env()) ++
42-
endpoints(deploy_env())
42+
endpoints(deploy_env()) ++
43+
[
44+
NervesHub.PromEx
45+
]
4346

4447
opts = [strategy: :one_for_one, name: NervesHub.Supervisor]
4548
Supervisor.start_link(children, opts)

lib/nerves_hub/prom_ex.ex

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
defmodule NervesHub.PromEx do
2+
use PromEx, otp_app: :nerves_hub
3+
4+
alias PromEx.Plugins
5+
6+
@impl true
7+
def plugins do
8+
[
9+
# PromEx built in plugins
10+
Plugins.Application,
11+
Plugins.Beam,
12+
{Plugins.Phoenix, router: NervesHubWeb.Router},
13+
Plugins.PhoenixLiveView,
14+
Plugins.Ecto,
15+
Plugins.Oban
16+
]
17+
end
18+
19+
@impl true
20+
def dashboard_assigns do
21+
[
22+
datasource_id: "prometheus"
23+
]
24+
end
25+
26+
@impl true
27+
def dashboards do
28+
[
29+
# PromEx built in Grafana dashboards
30+
{:prom_ex, "application.json"},
31+
{:prom_ex, "beam.json"},
32+
{:prom_ex, "phoenix.json"},
33+
{:prom_ex, "phoenix_live_view.json"},
34+
{:prom_ex, "ecto.json"},
35+
{:prom_ex, "oban.json"}
36+
]
37+
end
38+
end

lib/nerves_hub_web/device_endpoint.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ defmodule NervesHubWeb.DeviceEndpoint do
3333

3434
plug(NervesHubWeb.Plugs.ImAlive)
3535

36+
plug(PromEx.Plug, prom_ex_module: NervesHub.PromEx)
37+
3638
plug(Sentry.PlugContext)
3739

3840
plug(NervesHubWeb.Plugs.Logger)

lib/nerves_hub_web/endpoint.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ defmodule NervesHubWeb.Endpoint do
6060
cookie_key: "request_logger"
6161
)
6262

63+
plug(PromEx.Plug, prom_ex_module: NervesHub.PromEx)
64+
6365
plug(Plug.RequestId)
6466
plug(Plug.Telemetry, event_prefix: [:phoenix, :endpoint])
6567
plug(NervesHubWeb.Plugs.Logger)

mix.exs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,17 @@ defmodule NervesHub.MixProject do
9595
{:phoenix_pubsub, "~> 2.0"},
9696
{:phoenix_swoosh, "~> 1.0"},
9797
{:phoenix_view, "~> 2.0"},
98-
{:phoenix_test, "~> 0.3.0", only: :test, runtime: false},
98+
{:phoenix_test, "0.3.1", only: :test, runtime: false},
9999
{:plug, "~> 1.7"},
100100
{:postgrex, "~> 0.14"},
101+
{:prom_ex, "~> 1.10"},
101102
{:scrivener_ecto, "~> 2.7"},
102103
{:scrivener_html, git: "https://github.com/nerves-hub/scrivener_html", branch: "phx-1.5"},
103104
{:sentry, "~> 10.0"},
104105
{:slipstream, "~> 1.0", only: [:test, :dev]},
105106
{:sweet_xml, "~> 0.6"},
106107
{:swoosh, "~> 1.12"},
107-
{:telemetry_metrics, "~> 0.4"},
108+
{:telemetry_metrics, "~> 1.0"},
108109
{:telemetry_metrics_statsd, "~> 0.7.0"},
109110
{:telemetry_poller, "~> 1.0"},
110111
{:timex, "~> 3.1"},

mix.lock

Lines changed: 20 additions & 12 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)