Skip to content

Commit caa08c6

Browse files
committed
Code for step 6
1 parent fd8dc84 commit caa08c6

File tree

6 files changed

+91
-2
lines changed

6 files changed

+91
-2
lines changed

config/config.exs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,27 @@ config :elixir_monitoring_prom, ElixirMonitoringPromWeb.Endpoint,
1515
url: [host: "localhost"],
1616
secret_key_base: "nFSyXdlKCXuXTXWhnmEeVnA9VLzzbpuqX3UvpdaPo8uqpgjxd+cZorH+0GobASx8",
1717
render_errors: [view: ElixirMonitoringPromWeb.ErrorView, accepts: ~w(html json)],
18-
pubsub: [name: ElixirMonitoringProm.PubSub, adapter: Phoenix.PubSub.PG2]
18+
pubsub: [name: ElixirMonitoringProm.PubSub, adapter: Phoenix.PubSub.PG2],
19+
instrumenters: [ElixirMonitoringProm.PhoenixInstrumenter]
20+
21+
config :prometheus, ElixirMonitoringProm.PipelineInstrumenter,
22+
labels: [:status_class, :method, :host, :scheme, :request_path],
23+
duration_buckets: [
24+
10,
25+
100,
26+
1_000,
27+
10_000,
28+
100_000,
29+
300_000,
30+
500_000,
31+
750_000,
32+
1_000_000,
33+
1_500_000,
34+
2_000_000,
35+
3_000_000
36+
],
37+
registry: :default,
38+
duration_unit: :microseconds
1939

2040
# Configures Elixir's Logger
2141
config :logger, :console,

lib/elixir_monitoring_prom/application.ex

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@ defmodule ElixirMonitoringProm.Application do
66
use Application
77

88
def start(_type, _args) do
9+
# Start all the instrumenters
10+
ElixirMonitoringProm.PhoenixInstrumenter.setup()
11+
ElixirMonitoringProm.PipelineInstrumenter.setup()
12+
ElixirMonitoringProm.RepoInstrumenter.setup()
13+
ElixirMonitoringProm.PrometheusExporter.setup()
14+
15+
# NOTE: Only for FreeBSD, Linux and OSX (experimental)
16+
# https://github.com/deadtrickster/prometheus_process_collector
17+
Prometheus.Registry.register_collector(:prometheus_process_collector)
18+
19+
:telemetry.attach(
20+
"prometheus-ecto",
21+
[:elixir_monitoring_prom, :repo, :query],
22+
&ElixirMonitoringProm.RepoInstrumenter.handle_event/4,
23+
nil
24+
)
25+
926
# List all child processes to be supervised
1027
children = [
1128
# Start the Ecto repository

lib/elixir_monitoring_prom_web/endpoint.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,9 @@ defmodule ElixirMonitoringPromWeb.Endpoint do
4242
key: "_elixir_monitoring_prom_key",
4343
signing_salt: "iPbxnMI/"
4444

45+
# Creates the /metrics endpoint for prometheus & collect stats
46+
plug ElixirMonitoringProm.PrometheusExporter
47+
plug ElixirMonitoringProm.PipelineInstrumenter
48+
4549
plug ElixirMonitoringPromWeb.Router
4650
end

lib/instrumenters.ex

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
defmodule ElixirMonitoringProm.PhoenixInstrumenter do
2+
@moduledoc "Prometheus instrmenter for Phoenix"
3+
4+
use Prometheus.PhoenixInstrumenter
5+
end
6+
7+
defmodule ElixirMonitoringProm.PipelineInstrumenter do
8+
@moduledoc "Prometheus instrmenter for Phoenix"
9+
10+
use Prometheus.PlugPipelineInstrumenter
11+
12+
def label_value(:request_path, conn) do
13+
case Phoenix.Router.route_info(
14+
ElixirMonitoringPromWeb.Router,
15+
conn.method,
16+
conn.request_path,
17+
""
18+
) do
19+
%{route: path} -> path
20+
_ -> "unkown"
21+
end
22+
end
23+
end
24+
25+
defmodule ElixirMonitoringProm.RepoInstrumenter do
26+
@moduledoc "Prometheus instrmenter for Phoenix"
27+
28+
use Prometheus.EctoInstrumenter
29+
end
30+
31+
defmodule ElixirMonitoringProm.PrometheusExporter do
32+
@moduledoc "Prometheus instrmenter for Phoenix"
33+
34+
use Prometheus.PlugExporter
35+
end

mix.exs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@ defmodule ElixirMonitoringProm.MixProject do
4545
{:plug_cowboy, "~> 2.0"},
4646
{:geo, "~> 3.1.0"},
4747
{:geo_postgis, "~> 3.1.0"},
48-
{:faker, "~> 0.12.0"}
48+
{:faker, "~> 0.12.0"},
49+
{:prometheus, "~> 4.4.1"},
50+
{:prometheus_ex, "~> 3.0.5"},
51+
{:prometheus_ecto, "~> 1.4.3"},
52+
{:prometheus_phoenix, "~> 1.3.0"},
53+
{:prometheus_plugs, "~> 1.1.5"},
54+
{:prometheus_process_collector, "~> 1.4.3"}
4955
]
5056
end
5157

mix.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
%{
2+
"accept": {:hex, :accept, "0.3.5", "b33b127abca7cc948bbe6caa4c263369abf1347cfa9d8e699c6d214660f10cd1", [:rebar3], [], "hexpm"},
23
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm"},
34
"cowboy": {:hex, :cowboy, "2.6.3", "99aa50e94e685557cad82e704457336a453d4abcb77839ad22dbe71f311fcc06", [:rebar3], [{:cowlib, "~> 2.7.3", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"},
45
"cowlib": {:hex, :cowlib, "2.7.3", "a7ffcd0917e6d50b4d5fb28e9e2085a0ceb3c97dea310505f7460ff5ed764ce9", [:rebar3], [], "hexpm"},
@@ -22,6 +23,12 @@
2223
"plug_cowboy": {:hex, :plug_cowboy, "2.1.0", "b75768153c3a8a9e8039d4b25bb9b14efbc58e9c4a6e6a270abff1cd30cbe320", [:mix], [{:cowboy, "~> 2.5", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
2324
"plug_crypto": {:hex, :plug_crypto, "1.0.0", "18e49317d3fa343f24620ed22795ec29d4a5e602d52d1513ccea0b07d8ea7d4d", [:mix], [], "hexpm"},
2425
"postgrex": {:hex, :postgrex, "0.15.0", "dd5349161019caeea93efa42f9b22f9d79995c3a86bdffb796427b4c9863b0f0", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"},
26+
"prometheus": {:hex, :prometheus, "4.4.1", "1e96073b3ed7788053768fea779cbc896ddc3bdd9ba60687f2ad50b252ac87d6", [:mix, :rebar3], [], "hexpm"},
27+
"prometheus_ecto": {:hex, :prometheus_ecto, "1.4.3", "3dd4da1812b8e0dbee81ea58bb3b62ed7588f2eae0c9e97e434c46807ff82311", [:mix], [{:ecto, "~> 2.0 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:prometheus_ex, "~> 1.1 or ~> 2.0 or ~> 3.0", [hex: :prometheus_ex, repo: "hexpm", optional: false]}], "hexpm"},
28+
"prometheus_ex": {:hex, :prometheus_ex, "3.0.5", "fa58cfd983487fc5ead331e9a3e0aa622c67232b3ec71710ced122c4c453a02f", [:mix], [{:prometheus, "~> 4.0", [hex: :prometheus, repo: "hexpm", optional: false]}], "hexpm"},
29+
"prometheus_phoenix": {:hex, :prometheus_phoenix, "1.3.0", "c4b527e0b3a9ef1af26bdcfbfad3998f37795b9185d475ca610fe4388fdd3bb5", [:mix], [{:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}, {:prometheus_ex, "~> 1.3 or ~> 2.0 or ~> 3.0", [hex: :prometheus_ex, repo: "hexpm", optional: false]}], "hexpm"},
30+
"prometheus_plugs": {:hex, :prometheus_plugs, "1.1.5", "25933d48f8af3a5941dd7b621c889749894d8a1082a6ff7c67cc99dec26377c5", [:mix], [{:accept, "~> 0.1", [hex: :accept, repo: "hexpm", optional: false]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}, {:prometheus_ex, "~> 1.1 or ~> 2.0 or ~> 3.0", [hex: :prometheus_ex, repo: "hexpm", optional: false]}, {:prometheus_process_collector, "~> 1.1", [hex: :prometheus_process_collector, repo: "hexpm", optional: true]}], "hexpm"},
31+
"prometheus_process_collector": {:hex, :prometheus_process_collector, "1.4.3", "657386e8f142fc817347d95c1f3a05ab08710f7df9e7f86db6facaed107ed929", [:rebar3], [{:prometheus, "~> 4.0", [hex: :prometheus, repo: "hexpm", optional: false]}], "hexpm"},
2532
"ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm"},
2633
"telemetry": {:hex, :telemetry, "0.4.0", "8339bee3fa8b91cb84d14c2935f8ecf399ccd87301ad6da6b71c09553834b2ab", [:rebar3], [], "hexpm"},
2734
}

0 commit comments

Comments
 (0)