Skip to content

Instantly share code, notes, and snippets.

@robmadole
Created February 12, 2020 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robmadole/36172031a2504402a69323cae1f6908a to your computer and use it in GitHub Desktop.
Save robmadole/36172031a2504402a69323cae1f6908a to your computer and use it in GitHub Desktop.
Example of a cachex hook being integrated with Prometheus
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