Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Fix up Cachex metrics (#2418)
Adam Rutkowski9364cebb4b1c
diff --git a/lib/plausible/ingestion/event.ex b/lib/plausible/ingestion/event.exindex 577330e173..2a70623356 100644--- a/lib/plausible/ingestion/event.ex+++ b/lib/plausible/ingestion/event.ex@@ -20,7 +20,6 @@ defmodule Plausible.Ingestion.Event doevents when is_list(events) <- map_domains(event, request),events when is_list(events) <- put_user_id(events, request, salts),{:ok, events} <- validate_events(events),- :ok <- measured_sites_get(events),events when is_list(events) <- register_session(events, request, salts) doEnum.each(events, &Plausible.Event.WriteBuffer.insert/1)end@@ -333,27 +332,4 @@ defmodule Plausible.Ingestion.Event donilendend-- defp measured_sites_get(events) do- events- |> Enum.map(& &1.domain)- |> Enum.uniq()- |> Enum.each(fn domain ->- if should_lookup_site?(domain) do- start = System.monotonic_time()- Plausible.Sites.get_by_domain(domain)- stop = System.monotonic_time()-- :telemetry.execute(- [:plausible, :ingestion, :site, :lookup],- %{duration: stop - start},- %{}- )- end- end)- end-- defp should_lookup_site?(domain) do- FunWithFlags.enabled?(:ingestion_pg_lookup, for: domain)- endenddiff --git a/lib/plausible/telemetry/plausible_metrics.ex b/lib/plausible/telemetry/plausible_metrics.exindex 4f1d5dfd87..fa75a9f574 100644--- a/lib/plausible/telemetry/plausible_metrics.ex+++ b/lib/plausible/telemetry/plausible_metrics.ex@@ -7,6 +7,7 @@ defmodule Plausible.PromEx.Plugins.PlausibleMetrics do@impl truedef polling_metrics(opts) dopoll_rate = Keyword.get(opts, :poll_rate, 5_000)+otp_app = Keyword.fetch!(opts, :otp_app)metric_prefix =@@ -18,33 +19,6 @@ defmodule Plausible.PromEx.Plugins.PlausibleMetrics do]end- @impl true- def event_metrics(_opts) do- metric_prefix = [:plausible, :profiling]-- Event.build(- :plausible_event_metrics,- [- counter(- metric_prefix ++ [:ingestion, :site, :lookup, :counter],- event_name: [:plausible, :ingestion, :site, :lookup],- measurement: fn _ -> 1 end,- description: "Ingestion site lookup counter"- ),- distribution(- metric_prefix ++ [:ingestion, :site, :lookup, :duration, :milliseconds],- event_name: [:plausible, :ingestion, :site, :lookup],- description: "Ingestion site lookup duration",- measurement: :duration,- reporter_options: [- buckets: [5, 10, 50, 250, 1_000, 5_000]- ],- unit: {:native, :millisecond}- )- ]- )- end-@doc """Add telemetry events for Session and Event write buffers"""@@ -76,26 +50,23 @@ defmodule Plausible.PromEx.Plugins.PlausibleMetrics doAdd telemetry events for Cachex user agents and sessions"""def execute_cache_metrics do- user_agents_count =- case Cachex.stats(:user_agents) do- # https://github.com/whitfin/cachex/pull/301- {:ok, %{writes: w, evictions: e}} when is_integer(w) and is_integer(e) -> w - e- _ -> 0- end-- sessions_count =- case Cachex.stats(:sessions) do- # https://github.com/whitfin/cachex/pull/301- {:ok, %{writes: w, evictions: e}} when is_integer(w) and is_integer(e) -> w - e- _ -> 0- end-- :telemetry.execute([:prom_ex, :plugin, :cachex, :user_agents_count], %{- count: user_agents_count+ {:ok, user_agents_stats} = Cachex.stats(:user_agents)+ {:ok, sessions_stats} = Cachex.stats(:sessions)++ user_agents_hit_rate = Map.get(user_agents_stats, :hit_rate, 0.0)+ sessions_hit_rate = Map.get(sessions_stats, :hit_rate, 0.0)++ {:ok, user_agents_count} = Cachex.size(:user_agents)+ {:ok, sessions_count} = Cachex.size(:sessions)++ :telemetry.execute([:prom_ex, :plugin, :cachex, :user_agents], %{+ count: user_agents_count,+ hit_rate: user_agents_hit_rate})- :telemetry.execute([:prom_ex, :plugin, :cachex, :sessions_count], %{- count: sessions_count+ :telemetry.execute([:prom_ex, :plugin, :cachex, :sessions], %{+ count: sessions_count,+ hit_rate: sessions_hit_rate})end@@ -126,14 +97,26 @@ defmodule Plausible.PromEx.Plugins.PlausibleMetrics do{__MODULE__, :execute_cache_metrics, []},[last_value(- metric_prefix ++ [:events, :cache_size, :count],- event_name: [:prom_ex, :plugin, :cachex, :sessions_count],+ metric_prefix ++ [:cache, :sessions, :size],+ event_name: [:prom_ex, :plugin, :cachex, :sessions],measurement: :count),last_value(- metric_prefix ++ [:sessions, :cache_size, :count],- event_name: [:prom_ex, :plugin, :cachex, :user_agents_count],+ metric_prefix ++ [:cache, :user_agents, :size],+ event_name: [:prom_ex, :plugin, :cachex, :user_agents],measurement: :count+ ),+ last_value(+ metric_prefix ++ [:cache, :user_agents, :hit_ratio],+ event_name: [:prom_ex, :plugin, :cachex, :user_agents],+ description: "UA cache hit ratio",+ measurement: :hit_rate+ ),+ last_value(+ metric_prefix ++ [:cache, :sessions, :hit_ratio],+ event_name: [:prom_ex, :plugin, :cachex, :sessions],+ description: "Sessions cache hit ratio",+ measurement: :hit_rate)])