@@ -6,10 +6,27 @@ module VCAP::CloudController::Metrics
66 let ( :periodic_updater ) { PeriodicUpdater . new ( start_time , log_counter , logger , statsd_updater , prometheus_updater ) }
77 let ( :statsd_updater ) { double ( :statsd_updater ) }
88 let ( :prometheus_updater ) { double ( :prometheus_updater ) }
9+ let ( :threadqueue ) { double ( EventMachine ::Queue , size : 20 , num_waiting : 0 ) }
10+ let ( :resultqueue ) { double ( EventMachine ::Queue , size : 0 , num_waiting : 1 ) }
911 let ( :start_time ) { Time . now . utc - 90 }
1012 let ( :log_counter ) { double ( :log_counter , counts : { } ) }
1113 let ( :logger ) { double ( :logger ) }
1214
15+ before do
16+ allow ( EventMachine ) . to receive ( :connection_count ) . and_return ( 123 )
17+
18+ allow ( EventMachine ) . to receive ( :instance_variable_get ) do |instance_var |
19+ case instance_var
20+ when :@threadqueue
21+ threadqueue
22+ when :@resultqueue
23+ resultqueue
24+ else
25+ raise "Unexpected call: #{ instance_var } "
26+ end
27+ end
28+ end
29+
1330 describe 'task stats' do
1431 before do
1532 allow ( statsd_updater ) . to receive ( :update_task_stats )
@@ -56,6 +73,7 @@ module VCAP::CloudController::Metrics
5673 allow ( statsd_updater ) . to receive ( :update_user_count )
5774 allow ( statsd_updater ) . to receive ( :update_job_queue_length )
5875 allow ( statsd_updater ) . to receive ( :update_job_queue_load )
76+ allow ( statsd_updater ) . to receive ( :update_thread_info_thin )
5977 allow ( statsd_updater ) . to receive ( :update_failed_job_count )
6078 allow ( statsd_updater ) . to receive ( :update_vitals )
6179 allow ( statsd_updater ) . to receive ( :update_log_counts )
@@ -65,11 +83,14 @@ module VCAP::CloudController::Metrics
6583 allow ( prometheus_updater ) . to receive ( :update_user_count )
6684 allow ( prometheus_updater ) . to receive ( :update_job_queue_length )
6785 allow ( prometheus_updater ) . to receive ( :update_job_queue_load )
86+ allow ( prometheus_updater ) . to receive ( :update_thread_info_thin )
6887 allow ( prometheus_updater ) . to receive ( :update_failed_job_count )
6988 allow ( prometheus_updater ) . to receive ( :update_vitals )
7089 allow ( prometheus_updater ) . to receive ( :update_log_counts )
7190 allow ( prometheus_updater ) . to receive ( :update_task_stats )
7291 allow ( prometheus_updater ) . to receive ( :update_deploying_count )
92+
93+ allow ( EventMachine ) . to receive ( :add_periodic_timer )
7394 end
7495
7596 it 'bumps the number of users and sets periodic timer' do
@@ -92,6 +113,11 @@ module VCAP::CloudController::Metrics
92113 periodic_updater . setup_updates
93114 end
94115
116+ it 'updates thread count and event machine queues' do
117+ expect ( periodic_updater ) . to receive ( :update_thread_info ) . once
118+ periodic_updater . setup_updates
119+ end
120+
95121 it 'updates the vitals' do
96122 expect ( periodic_updater ) . to receive ( :update_vitals ) . once
97123 periodic_updater . setup_updates
@@ -112,16 +138,15 @@ module VCAP::CloudController::Metrics
112138 periodic_updater . setup_updates
113139 end
114140
115- context 'when Concurrent::TimerTasks are run' do
141+ context 'when EventMachine periodic_timer tasks are run' do
116142 before do
117143 @periodic_timers = [ ]
118144
119- allow ( Concurrent :: TimerTask ) . to receive ( :new ) do |opts , &block |
145+ allow ( EventMachine ) . to receive ( :add_periodic_timer ) do |interval , &block |
120146 @periodic_timers << {
121- interval : opts [ :execution_interval ] ,
122- block : block
147+ interval :,
148+ block :
123149 }
124- double ( 'TimerTask' , execute : nil , shutdown : nil , kill : nil , running? : false )
125150 end
126151
127152 periodic_updater . setup_updates
@@ -151,37 +176,45 @@ module VCAP::CloudController::Metrics
151176 @periodic_timers [ 2 ] [ :block ] . call
152177 end
153178
154- it 'bumps the length of cc failed job queues and sets periodic timer ' do
179+ it 'updates thread count and event machine queues ' do
155180 expect ( periodic_updater ) . to receive ( :catch_error ) . once . and_call_original
156- expect ( periodic_updater ) . to receive ( :update_failed_job_count ) . once
181+ expect ( periodic_updater ) . to receive ( :update_thread_info ) . once
157182 expect ( @periodic_timers [ 3 ] [ :interval ] ) . to eq ( 30 )
158183
159184 @periodic_timers [ 3 ] [ :block ] . call
160185 end
161186
162- it 'updates the vitals ' do
187+ it 'bumps the length of cc failed job queues and sets periodic timer ' do
163188 expect ( periodic_updater ) . to receive ( :catch_error ) . once . and_call_original
164- expect ( periodic_updater ) . to receive ( :update_vitals ) . once
189+ expect ( periodic_updater ) . to receive ( :update_failed_job_count ) . once
165190 expect ( @periodic_timers [ 4 ] [ :interval ] ) . to eq ( 30 )
166191
167192 @periodic_timers [ 4 ] [ :block ] . call
168193 end
169194
170- it 'updates the log counts ' do
195+ it 'updates the vitals ' do
171196 expect ( periodic_updater ) . to receive ( :catch_error ) . once . and_call_original
172- expect ( periodic_updater ) . to receive ( :update_log_counts ) . once
197+ expect ( periodic_updater ) . to receive ( :update_vitals ) . once
173198 expect ( @periodic_timers [ 5 ] [ :interval ] ) . to eq ( 30 )
174199
175200 @periodic_timers [ 5 ] [ :block ] . call
176201 end
177202
178- it 'updates the task stats ' do
203+ it 'updates the log counts ' do
179204 expect ( periodic_updater ) . to receive ( :catch_error ) . once . and_call_original
180- expect ( periodic_updater ) . to receive ( :update_task_stats ) . once
205+ expect ( periodic_updater ) . to receive ( :update_log_counts ) . once
181206 expect ( @periodic_timers [ 6 ] [ :interval ] ) . to eq ( 30 )
182207
183208 @periodic_timers [ 6 ] [ :block ] . call
184209 end
210+
211+ it 'updates the task stats' do
212+ expect ( periodic_updater ) . to receive ( :catch_error ) . once . and_call_original
213+ expect ( periodic_updater ) . to receive ( :update_task_stats ) . once
214+ expect ( @periodic_timers [ 7 ] [ :interval ] ) . to eq ( 30 )
215+
216+ @periodic_timers [ 7 ] [ :block ] . call
217+ end
185218 end
186219 end
187220
@@ -505,6 +538,75 @@ module VCAP::CloudController::Metrics
505538 end
506539 end
507540
541+ describe '#update_thread_info' do
542+ before do
543+ allow ( statsd_updater ) . to receive ( :update_thread_info_thin )
544+ allow ( prometheus_updater ) . to receive ( :update_thread_info_thin )
545+ end
546+
547+ it 'contains EventMachine data and send it to all updaters' do
548+ expected_thread_info = {
549+ thread_count : Thread . list . size ,
550+ event_machine : {
551+ connection_count : 123 ,
552+ threadqueue : {
553+ size : 20 ,
554+ num_waiting : 0
555+ } ,
556+ resultqueue : {
557+ size : 0 ,
558+ num_waiting : 1
559+ }
560+ }
561+ }
562+
563+ periodic_updater . update_thread_info
564+
565+ expect ( statsd_updater ) . to have_received ( :update_thread_info_thin ) . with ( expected_thread_info )
566+ expect ( prometheus_updater ) . to have_received ( :update_thread_info_thin ) . with ( expected_thread_info )
567+ end
568+
569+ context 'when resultqueue and/or threadqueue is not a queue' do
570+ let ( :resultqueue ) { [ ] }
571+ let ( :threadqueue ) { nil }
572+
573+ it 'does not blow up' do
574+ expected_thread_info = {
575+ thread_count : Thread . list . size ,
576+ event_machine : {
577+ connection_count : 123 ,
578+ threadqueue : {
579+ size : 0 ,
580+ num_waiting : 0
581+ } ,
582+ resultqueue : {
583+ size : 0 ,
584+ num_waiting : 0
585+ }
586+ }
587+ }
588+
589+ periodic_updater . update_thread_info
590+
591+ expect ( statsd_updater ) . to have_received ( :update_thread_info_thin ) . with ( expected_thread_info )
592+ expect ( prometheus_updater ) . to have_received ( :update_thread_info_thin ) . with ( expected_thread_info )
593+ end
594+ end
595+
596+ context 'when Puma is configured as webserver' do
597+ before do
598+ TestConfig . override ( webserver : 'puma' )
599+ end
600+
601+ it 'does not send EventMachine data to updaters' do
602+ periodic_updater . update_thread_info
603+
604+ expect ( statsd_updater ) . not_to have_received ( :update_thread_info_thin )
605+ expect ( prometheus_updater ) . not_to have_received ( :update_thread_info_thin )
606+ end
607+ end
608+ end
609+
508610 describe '#update_vitals' do
509611 before do
510612 allow ( statsd_updater ) . to receive ( :update_vitals )
@@ -634,6 +736,7 @@ module VCAP::CloudController::Metrics
634736 expect ( periodic_updater ) . to receive ( :update_user_count ) . once
635737 expect ( periodic_updater ) . to receive ( :update_job_queue_length ) . once
636738 expect ( periodic_updater ) . to receive ( :update_job_queue_load ) . once
739+ expect ( periodic_updater ) . to receive ( :update_thread_info ) . once
637740 expect ( periodic_updater ) . to receive ( :update_failed_job_count ) . once
638741 expect ( periodic_updater ) . to receive ( :update_vitals ) . once
639742 expect ( periodic_updater ) . to receive ( :update_log_counts ) . once
0 commit comments