From 8839d6dadde287edc61c15db7d54b5a58821a300 Mon Sep 17 00:00:00 2001 From: Kevin Stewart Date: Wed, 26 Aug 2026 11:13:18 -0700 Subject: [PATCH 1/2] Emit accurate Ruby worker slot metrics --- lib/temporal/activity/poller.rb | 8 +++ lib/temporal/metric_keys.rb | 2 + lib/temporal/thread_pool.rb | 63 +++++++++++++++++++++- lib/temporal/workflow/poller.rb | 8 +++ spec/unit/lib/temporal/thread_pool_spec.rb | 60 +++++++++++++++++++++ 5 files changed, 140 insertions(+), 1 deletion(-) diff --git a/lib/temporal/activity/poller.rb b/lib/temporal/activity/poller.rb index 859fb6882..589eeaae4 100644 --- a/lib/temporal/activity/poller.rb +++ b/lib/temporal/activity/poller.rb @@ -129,6 +129,14 @@ def thread_pool pool_name: 'activity_task_poller', namespace: namespace, task_queue: task_queue + }, + worker_metrics_tags: { + namespace: namespace, + task_queue: task_queue, + worker_type: 'ActivityWorker', + # DogStatsD gauges from multiple worker processes otherwise share + # one metric context and overwrite each other. + worker_id: config.for_connection.identity.to_s.strip } ) end diff --git a/lib/temporal/metric_keys.rb b/lib/temporal/metric_keys.rb index e945f0b64..a99f2b35c 100644 --- a/lib/temporal/metric_keys.rb +++ b/lib/temporal/metric_keys.rb @@ -12,5 +12,7 @@ module MetricKeys WORKFLOW_TASK_EXECUTION_FAILED = 'workflow_task.execution_failed'.freeze THREAD_POOL_AVAILABLE_THREADS = 'thread_pool.available_threads'.freeze + WORKER_TASK_SLOTS_AVAILABLE = 'worker_task_slots_available'.freeze + WORKER_TASK_SLOTS_USED = 'worker_task_slots_used'.freeze end end diff --git a/lib/temporal/thread_pool.rb b/lib/temporal/thread_pool.rb index 3febbf825..b9e20adb3 100644 --- a/lib/temporal/thread_pool.rb +++ b/lib/temporal/thread_pool.rb @@ -9,11 +9,21 @@ # module Temporal class ThreadPool + DEFAULT_METRICS_REPORT_INTERVAL_SECONDS = 10 + attr_reader :size - def initialize(size, config, metrics_tags) + def initialize( + size, + config, + metrics_tags, + worker_metrics_tags: nil, + metrics_report_interval_seconds: DEFAULT_METRICS_REPORT_INTERVAL_SECONDS + ) @size = size @metrics_tags = metrics_tags + @worker_metrics_tags = worker_metrics_tags + @metrics_report_interval_seconds = metrics_report_interval_seconds @queue = Queue.new @mutex = Mutex.new @config = config @@ -22,6 +32,11 @@ def initialize(size, config, metrics_tags) @pool = Array.new(size) do |_i| Thread.new { poll } end + + # Task transitions can be shorter than a metrics aggregation interval. A + # periodic snapshot makes this a stable saturation signal and reports idle + # workers even when they have not received a task recently. + start_metrics_reporter if worker_metrics_tags end def report_metrics @@ -44,6 +59,8 @@ def schedule(&block) end def shutdown + stop_metrics_reporter + size.times do schedule { throw EXIT_SYMBOL } end @@ -55,6 +72,50 @@ def shutdown EXIT_SYMBOL = :exit + def report_worker_slot_metrics + available_threads = @mutex.synchronize { @available_threads } + + Temporal.metrics.gauge( + Temporal::MetricKeys::WORKER_TASK_SLOTS_AVAILABLE, + available_threads, + @worker_metrics_tags + ) + Temporal.metrics.gauge( + Temporal::MetricKeys::WORKER_TASK_SLOTS_USED, + size - available_threads, + @worker_metrics_tags + ) + end + + def start_metrics_reporter + @metrics_reporter_mutex = Mutex.new + @metrics_reporter_condition = ConditionVariable.new + @metrics_reporter_shutdown = false + + report_worker_slot_metrics + @metrics_reporter_thread = Thread.new do + loop do + shutting_down = @metrics_reporter_mutex.synchronize do + @metrics_reporter_condition.wait(@metrics_reporter_mutex, @metrics_report_interval_seconds) + @metrics_reporter_shutdown + end + break if shutting_down + + report_worker_slot_metrics + end + end + end + + def stop_metrics_reporter + return unless @metrics_reporter_thread + + @metrics_reporter_mutex.synchronize do + @metrics_reporter_shutdown = true + @metrics_reporter_condition.signal + end + @metrics_reporter_thread.join + end + def poll Thread.current.abort_on_exception = true diff --git a/lib/temporal/workflow/poller.rb b/lib/temporal/workflow/poller.rb index 198f45027..812dd1c34 100644 --- a/lib/temporal/workflow/poller.rb +++ b/lib/temporal/workflow/poller.rb @@ -125,6 +125,14 @@ def thread_pool pool_name: 'workflow_task_poller', namespace: namespace, task_queue: task_queue + }, + worker_metrics_tags: { + namespace: namespace, + task_queue: task_queue, + worker_type: 'WorkflowWorker', + # DogStatsD gauges from multiple worker processes otherwise share + # one metric context and overwrite each other. + worker_id: config.for_connection.identity.to_s.strip } ) end diff --git a/spec/unit/lib/temporal/thread_pool_spec.rb b/spec/unit/lib/temporal/thread_pool_spec.rb index 5de5b03a4..a42ae98f3 100644 --- a/spec/unit/lib/temporal/thread_pool_spec.rb +++ b/spec/unit/lib/temporal/thread_pool_spec.rb @@ -9,6 +9,14 @@ let(:size) { 2 } let(:tags) { { foo: 'bar', bat: 'baz' } } let(:thread_pool) { described_class.new(size, config, tags) } + let(:worker_metrics_tags) do + { + namespace: 'test-namespace', + task_queue: 'test-task-queue', + worker_type: 'ActivityWorker', + worker_id: 'worker-1' + } + end describe '#new' do it 'executes one task on a thread and exits' do @@ -71,5 +79,57 @@ ) .at_least(:once) end + + it 'periodically reports available and used task slots for an individual worker' do + pool = described_class.new( + size, + config, + tags, + worker_metrics_tags: worker_metrics_tags, + metrics_report_interval_seconds: 0.01 + ) + + sleep 0.02 + pool.shutdown + + expect(Temporal.metrics) + .to have_received(:gauge) + .with(Temporal::MetricKeys::WORKER_TASK_SLOTS_AVAILABLE, size, worker_metrics_tags) + .at_least(:twice) + expect(Temporal.metrics) + .to have_received(:gauge) + .with(Temporal::MetricKeys::WORKER_TASK_SLOTS_USED, 0, worker_metrics_tags) + .at_least(:twice) + end + + it 'reports task slot usage while a task is running' do + task_started = Queue.new + finish_task = Queue.new + pool = described_class.new( + size, + config, + tags, + worker_metrics_tags: worker_metrics_tags, + metrics_report_interval_seconds: 0.01 + ) + + pool.schedule do + task_started << true + finish_task.pop + end + task_started.pop + sleep 0.02 + finish_task << true + pool.shutdown + + expect(Temporal.metrics) + .to have_received(:gauge) + .with(Temporal::MetricKeys::WORKER_TASK_SLOTS_AVAILABLE, size - 1, worker_metrics_tags) + .at_least(:once) + expect(Temporal.metrics) + .to have_received(:gauge) + .with(Temporal::MetricKeys::WORKER_TASK_SLOTS_USED, 1, worker_metrics_tags) + .at_least(:once) + end end end From 85c0991c1df4362e5d2b176ed1a4c351c3bd9c5c Mon Sep 17 00:00:00 2001 From: Kevin Stewart Date: Tue, 17 Nov 2026 16:28:40 -0800 Subject: [PATCH 2/2] Keep worker metrics distinct when connection identities are shared --- lib/temporal/activity/poller.rb | 7 ++++--- lib/temporal/workflow/poller.rb | 7 ++++--- spec/unit/lib/temporal/activity/poller_spec.rb | 18 ++++++++++++++++++ spec/unit/lib/temporal/workflow/poller_spec.rb | 18 ++++++++++++++++++ 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/lib/temporal/activity/poller.rb b/lib/temporal/activity/poller.rb index 589eeaae4..5616d3322 100644 --- a/lib/temporal/activity/poller.rb +++ b/lib/temporal/activity/poller.rb @@ -1,3 +1,4 @@ +require 'securerandom' require 'temporal/activity/task_processor' require 'temporal/connection' require 'temporal/error_handler' @@ -134,9 +135,9 @@ def thread_pool namespace: namespace, task_queue: task_queue, worker_type: 'ActivityWorker', - # DogStatsD gauges from multiple worker processes otherwise share - # one metric context and overwrite each other. - worker_id: config.for_connection.identity.to_s.strip + # Custom connection identities may be shared across processes. + # Keep each pool in a separate DogStatsD gauge context. + worker_id: "#{config.for_connection.identity.to_s.strip}:#{SecureRandom.uuid}" } ) end diff --git a/lib/temporal/workflow/poller.rb b/lib/temporal/workflow/poller.rb index 812dd1c34..011dda546 100644 --- a/lib/temporal/workflow/poller.rb +++ b/lib/temporal/workflow/poller.rb @@ -1,3 +1,4 @@ +require 'securerandom' require 'grpc/errors' require 'temporal/connection' require 'temporal/thread_pool' @@ -130,9 +131,9 @@ def thread_pool namespace: namespace, task_queue: task_queue, worker_type: 'WorkflowWorker', - # DogStatsD gauges from multiple worker processes otherwise share - # one metric context and overwrite each other. - worker_id: config.for_connection.identity.to_s.strip + # Custom connection identities may be shared across processes. + # Keep each pool in a separate DogStatsD gauge context. + worker_id: "#{config.for_connection.identity.to_s.strip}:#{SecureRandom.uuid}" } ) end diff --git a/spec/unit/lib/temporal/activity/poller_spec.rb b/spec/unit/lib/temporal/activity/poller_spec.rb index 3e5d24c7b..d0b882ce1 100644 --- a/spec/unit/lib/temporal/activity/poller_spec.rb +++ b/spec/unit/lib/temporal/activity/poller_spec.rb @@ -54,6 +54,24 @@ def poll(task, times: 1) end describe '#start' do + it 'keeps metrics separate for pools with the same custom connection identity' do + config.identity = 'shared-worker' + ids = [] + allow(Temporal::ThreadPool).to receive(:new) do |*args| + ids << args.last.fetch(:worker_metrics_tags).fetch(:worker_id) + thread_pool + end + other = described_class.new(namespace, task_queue, lookup, config, middleware) + + subject.send(:thread_pool) + subject.send(:thread_pool) + other.send(:thread_pool) + + expect(ids.size).to eq(2) + expect(ids.uniq.size).to eq(2) + expect(config.for_connection.identity).to eq('shared-worker') + end + it 'measures time between polls' do # if it doesn't poll, this test will loop forever times = poll(nil, times: 2) diff --git a/spec/unit/lib/temporal/workflow/poller_spec.rb b/spec/unit/lib/temporal/workflow/poller_spec.rb index 020e2e910..69f959c4a 100644 --- a/spec/unit/lib/temporal/workflow/poller_spec.rb +++ b/spec/unit/lib/temporal/workflow/poller_spec.rb @@ -64,6 +64,24 @@ def poll(task, times: 1) end describe '#start' do + it 'keeps metrics separate for pools with the same custom connection identity' do + config.identity = 'shared-worker' + ids = [] + allow(Temporal::ThreadPool).to receive(:new) do |*args| + ids << args.last.fetch(:worker_metrics_tags).fetch(:worker_id) + instance_double(Temporal::ThreadPool) + end + other = described_class.new(namespace, task_queue, lookup, config, middleware, workflow_middleware) + + subject.send(:thread_pool) + subject.send(:thread_pool) + other.send(:thread_pool) + + expect(ids.size).to eq(2) + expect(ids.uniq.size).to eq(2) + expect(config.for_connection.identity).to eq('shared-worker') + end + it 'polls for workflow tasks' do subject.start times = poll(nil, times: 2)