Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

counters for cache stats #5127

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/plausible/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ defmodule Plausible.Application do
children =
[
Plausible.Session.BalancerSupervisor,
Plausible.Cache.Stats,
Plausible.PromEx,
{Plausible.Auth.TOTP.Vault, key: totp_vault_key()},
Plausible.Repo,
Expand Down Expand Up @@ -126,7 +125,10 @@ defmodule Plausible.Application do
Plausible.Ingestion.Source.init()
Plausible.Geo.await_loader()

Supervisor.start_link(List.flatten(children), opts)
with {:ok, _pid} = ok <- Supervisor.start_link(List.flatten(children), opts) do
Plausible.Cache.Stats.attach()
ok
end
end

def config_change(changed, _new, removed) do
Expand Down
1 change: 1 addition & 0 deletions lib/plausible/cache/adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ defmodule Plausible.Cache.Adapter do
cache_name = Keyword.get(opts, :cache_name, name)
child_id = Keyword.get(opts, :child_id, child_id)
ttl_check_interval = Keyword.get(opts, :ttl_check_interval, false)
Plausible.Cache.Stats.create_counters(cache_name)

opts =
opts
Expand Down
48 changes: 15 additions & 33 deletions lib/plausible/cache/stats.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,13 @@ defmodule Plausible.Cache.Stats do
Keeps track of hit/miss ratio for various caches.
"""

use GenServer

@hit :hit
@miss :miss
@telemetry_hit ConCache.Operations.telemetry_hit()
@telemetry_miss ConCache.Operations.telemetry_miss()
@telemetry_events [@telemetry_hit, @telemetry_miss]

def start_link(_opts) do
GenServer.start_link(__MODULE__, nil)
end

def init(nil) do
__MODULE__ =
:ets.new(__MODULE__, [
:public,
:named_table,
:set,
read_concurrency: true,
write_concurrency: true
])

def attach do
:telemetry.attach_many(
"plausible-cache-stats",
@telemetry_events,
Expand All @@ -35,6 +20,16 @@ defmodule Plausible.Cache.Stats do
{:ok, nil}
end

def create_counters(cache_name) do
:persistent_term.put({__MODULE__, cache_name, @hit}, :counters.new(1, []))
:persistent_term.put({__MODULE__, cache_name, @miss}, :counters.new(1, []))
end

defp counter(cache_name, type) do
:persistent_term.get({__MODULE__, cache_name, type}, nil) ||
raise "counter not found for #{cache_name} #{type}"
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all very quick and dirty. The API can be improved if this works out.


def handle_telemetry_event(@telemetry_hit, _measurments, %{cache: %{name: cache_name}}, _) do
bump(cache_name, @hit)
end
Expand All @@ -54,12 +49,7 @@ defmodule Plausible.Cache.Stats do
defdelegate size(cache_name), to: Plausible.Cache.Adapter

def bump(cache_name, type) do
:ets.update_counter(
__MODULE__,
{cache_name, type},
1,
{{cache_name, type}, 0}
)
:counters.add(counter(cache_name, type), 1, 1)
end

def hit_rate(cache_name) do
Expand All @@ -68,23 +58,15 @@ defmodule Plausible.Cache.Stats do
|> Enum.reduce(
%{hit: 0, miss: 0, hit_miss: 0.0},
fn name, acc ->
hit =
acc.hit + :ets.lookup_element(__MODULE__, {name, @hit}, 2, 0)

miss =
acc.miss + :ets.lookup_element(__MODULE__, {name, @miss}, 2, 0)

hit = acc.hit + :counters.get(counter(name, @hit), 1)
miss = acc.miss + :counters.get(counter(name, @miss), 1)
hit_miss = hit + miss

hit_miss = if(hit_miss == 0, do: 0.0, else: hit / hit_miss * 100)

acc
|> Map.put(:hit, hit)
|> Map.put(:miss, miss)
|> Map.put(
:hit_miss,
hit_miss
)
|> Map.put(:hit_miss, hit_miss)
end
)
|> Map.fetch!(:hit_miss)
Expand Down
Loading