Create a gist now

Instantly share code, notes, and snippets.

# lib/monitor.ex
defmodule MyApp.Monitor do
def health_check do
ExStatsD.histogram(get_concache_mem_size(:css_cache), "css.cache.memory", ["css_cache_memory"])
ExStatsD.histogram(get_concache_mem_size(:sass_cache), "sass.cache.memory", ["sass_cache_memory"])
ExStatsD.histogram(ConCache.size(:css_cache), "css.cache.size", ["css_cache_size"])
ExStatsD.histogram(ConCache.size(:sass_cache), "sass.cache.size", ["sass_cache_size"])
ExStatsD.histogram(:erlang.memory(:ets), "erlang.ets.memory", ["ets_size"])
ExStatsD.histogram(:erlang.memory |> Keyword.get(:total), "erlang.memory.memory", ["ets_size"])
end
defp get_concache_mem_size(name) do
name |> ConCache.ets |> :ets.info |> Keyword.get(:memory) |> (fn(x) -> x * :erlang.system_info(:wordsize) end).()
end
end
# config/prod.ex
config :quantum, cron: [
# Every minute
"* * * * *": {MyApp.Monitor, :health_check}
]
config :ex_statsd,
host: "localhost",
port: 8125,
namespace: "MyApp"
# web/plugs/request_status_plug.ex
defmodule MyApp.RequestStatusPlug do
@behaviour Plug
import Plug.Conn, only: [register_before_send: 2]
def init(opts), do: opts
def call(conn, _config) do
req_start_time = :os.timestamp
register_before_send conn, fn conn ->
# increment count
ExStatsD.increment("response.count")
# log response time in microseconds
req_end_time = :os.timestamp
duration = :timer.now_diff(req_end_time, req_start_time)
ExStatsD.timer(duration, "response.time")
conn
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment