Created
February 12, 2020 19:04
-
-
Save robmadole/36172031a2504402a69323cae1f6908a to your computer and use it in GitHub Desktop.
Example of a cachex hook being integrated with Prometheus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Kits.Metrics.Cachex do | |
@moduledoc """ | |
Metrics related to our use of Cachex | |
""" | |
use Cachex.Hook | |
use Prometheus.Metric | |
alias Kits.Metrics | |
def init(_), do: {:ok, nil} | |
@doc false | |
def setup do | |
Counter.declare( | |
name: :kit_settings_cache_hit, | |
labels: [:hostname], | |
help: "Cache hits" | |
) | |
Counter.declare( | |
name: :kit_settings_cache_miss, | |
labels: [:hostname], | |
help: "Cache miss" | |
) | |
Counter.declare( | |
name: :kit_settings_cache_last_purge, | |
labels: [:hostname], | |
help: "Number of entries that were purged by the Janitor" | |
) | |
end | |
def handle_notify(msg = {:purge, _}, {:ok, number}, _last) do | |
# Janitor removed these tokens | |
Counter.inc( | |
[ | |
name: :kit_settings_cache_last_purge, | |
labels: [Metrics.hostname()] | |
], | |
number | |
) | |
{:ok, msg} | |
end | |
def handle_notify(msg = {:fetch, _}, {:commit, _}, _last) do | |
Counter.inc( | |
[ | |
name: :kit_settings_cache_miss, | |
labels: [Metrics.hostname()] | |
], | |
1 | |
) | |
{:ok, msg} | |
end | |
def handle_notify(msg = {:fetch, _}, {:ok, _}, _last) do | |
Counter.inc( | |
[ | |
name: :kit_settings_cache_hit, | |
labels: [Metrics.hostname()] | |
], | |
1 | |
) | |
{:ok, msg} | |
end | |
def handle_notify(msg, _, _), do: {:ok, msg} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment