From 46740321d62ca2af8ed2b4ebe0e385d77cf00ecf Mon Sep 17 00:00:00 2001 From: Aleksandr Sysoev Date: Mon, 25 Mar 2024 14:11:04 +0700 Subject: [PATCH] Add `overflowed` boolean metric --- TODO.md | 2 +- lib/poolex/private/metrics.ex | 3 ++- test/poolex_metrics_test.exs | 24 +++++++++++++++++++++--- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/TODO.md b/TODO.md index 40efb17..3ec5cd6 100644 --- a/TODO.md +++ b/TODO.md @@ -11,7 +11,7 @@ Using metrics, we see that typically, our application uses 10-20 processes, and - [ ] Pool size - [x] Idle workers count - [x] Busy workers count - - [ ] Is max_overflow used? + - [x] Is max_overflow used? - [ ] Maximum count of "overflowed" workers - [ ] Usage time - [ ] How long are workers busy? diff --git a/lib/poolex/private/metrics.ex b/lib/poolex/private/metrics.ex index 161ff89..53afc83 100644 --- a/lib/poolex/private/metrics.ex +++ b/lib/poolex/private/metrics.ex @@ -14,7 +14,8 @@ defmodule Poolex.Private.Metrics do [:poolex, :metrics, :pool_size], %{ idle_workers_count: debug_info.idle_workers_count, - busy_workers_count: debug_info.busy_workers_count + busy_workers_count: debug_info.busy_workers_count, + overflowed: debug_info.overflow > 0 }, %{pool_id: pool_id} ) diff --git a/test/poolex_metrics_test.exs b/test/poolex_metrics_test.exs index ec3f0e0..092ed57 100644 --- a/test/poolex_metrics_test.exs +++ b/test/poolex_metrics_test.exs @@ -7,11 +7,17 @@ defmodule PoolexMetricsTest do @tag telemetry_events: [[:poolex, :metrics, :pool_size]] test "pool size metrics" do - pool_id = start_pool(worker_module: SomeWorker, workers_count: 5, pool_size_metrics: true) + pool_id = + start_pool( + worker_module: SomeWorker, + workers_count: 5, + pool_size_metrics: true, + max_overflow: 5 + ) assert_telemetry_event( [:poolex, :metrics, :pool_size], - %{idle_workers_count: 5, busy_workers_count: 0}, + %{idle_workers_count: 5, busy_workers_count: 0, overflowed: false}, %{pool_id: ^pool_id} ) @@ -21,7 +27,19 @@ defmodule PoolexMetricsTest do assert_telemetry_event( [:poolex, :metrics, :pool_size], - %{idle_workers_count: 4}, + %{idle_workers_count: 4, busy_workers_count: 1, overflowed: false}, + %{pool_id: ^pool_id} + ) + + Enum.each(1..5, fn _ -> + launch_long_task(pool_id) + end) + + Metrics.dispatch_pool_size_metrics(pool_id) + + assert_telemetry_event( + [:poolex, :metrics, :pool_size], + %{idle_workers_count: 0, busy_workers_count: 6, overflowed: true}, %{pool_id: ^pool_id} ) end