-
Notifications
You must be signed in to change notification settings - Fork 1
/
scratch.ex
65 lines (53 loc) · 1.58 KB
/
scratch.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
defmodule MetricsDemo.Metrics do
import Metrics
def start_link do
Metrics.Supervisor.start_link(__MODULE__, [
gauge(:memory, every: {1, :second}),
gauge(:cpu, every: {1, :second}),
gauge(:processes, every: {1, :second}),
meter(:requests, every: {1, :second}),
], strategy: :one_for_one)
end
## Gauges
def memory(state) do
mem = :memsup.get_system_memory_data()
value = Enum.into(mem, %{
usage: (1 - (mem[:free_memory] / mem[:total_memory])) * 100
})
{:ok, value, state}
end
def processes(state) do
count = :erlang.system_info(:process_count)
limit = :erlang.system_info(:process_limit)
value = %{count: count, limit: limit, usage: (count / limit) * 100}
{:ok, value, state}
end
def cpu(state) do
{:ok, %{usage: :cpu_sup.util()}, state}
end
## Instrumenters
def phoenix_controller_call(:start, _compile, _runtime), do: :ok
def phoenix_controller_call(:stop, _time_diff, :ok) do
Metrics.Meter.mark(__MODULE__, :requests)
:ok
end
end
defmodule DemoWeb.MetricChannel do
use MetricsDemoWeb, :channel
def join("metric:" <> _, _params, socket) do
Metrics.register(Demo.Metrics)
{:ok, socket}
end
def handle_info({:metric, :gauge, name, value, _timeframe}, socket) do
push socket, "gauge", %{name: name, value: value}
{:noreply, socket}
end
def handle_info({:metric, :meter, name, value, {count, unit}}, socket) do
push socket, "meter", %{
name: name,
value: value,
duration: %{count: count, unit: unit}
}
{:noreply, socket}
end
end