@@ -22,7 +22,7 @@ def initialize(options)
2222
2323 def start_working
2424 config = RakeConfig . config
25- setup_metrics_endpoint ( config ) if @publish_metrics
25+ setup_metrics ( config ) if @publish_metrics
2626 BackgroundJobEnvironment . new ( config ) . setup_environment ( readiness_port )
2727
2828 logger = Steno . logger ( 'cc-worker' )
@@ -99,36 +99,39 @@ def is_first_generic_worker_on_machine?
9999 RakeConfig . context != :api && ENV [ 'INDEX' ] &.to_i == 1
100100 end
101101
102- def setup_metrics_endpoint ( config )
102+ def setup_metrics ( config )
103103 prometheus_dir = File . join ( config . get ( :directories , :tmpdir ) , 'prometheus' )
104104 Prometheus ::Client . config . data_store = Prometheus ::Client ::DataStores ::DirectFileStore . new ( dir : prometheus_dir )
105105
106- if is_first_generic_worker_on_machine?
107- FileUtils . mkdir_p ( prometheus_dir )
106+ setup_webserver ( config , prometheus_dir ) if is_first_generic_worker_on_machine?
108107
109- # Resetting metrics on startup
110- Dir [ "#{ prometheus_dir } /*.bin" ] . each do |file_path |
111- File . unlink ( file_path )
112- end
108+ # initialize metric with 0 for discoverability, because it likely won't get updated on healthy systems
109+ CloudController ::DependencyLocator . instance . cc_worker_prometheus_updater . update_gauge_metric ( :cc_db_connection_pool_timeouts_total , 0 , labels : { process_type : 'cc-worker' } )
110+ end
113111
114- metrics_app = Rack :: Builder . new do
115- use Prometheus :: Middleware :: Exporter , path : '/metrics'
112+ def setup_webserver ( config , prometheus_dir )
113+ FileUtils . mkdir_p ( prometheus_dir )
116114
117- map '/' do
118- run lambda { | env |
119- # Return 404 for any other request
120- [ '404' , { 'Content-Type' => 'text/plain' } , [ 'Not Found' ] ]
121- }
122- end
123- end
115+ # Resetting metrics on startup
116+ Dir [ " #{ prometheus_dir } /*.bin" ] . each do | file_path |
117+ File . unlink ( file_path )
118+ end
119+
120+ metrics_app = Rack :: Builder . new do
121+ use Prometheus :: Middleware :: Exporter , path : '/metrics'
124122
125- Thread . new do
126- server = Puma ::Server . new ( metrics_app )
127- server . add_tcp_listener '0.0.0.0' , config . get ( :prometheus_port ) || 9394
128- server . run
123+ map '/' do
124+ run lambda { |_env |
125+ # Return 404 for any other request
126+ [ '404' , { 'Content-Type' => 'text/plain' } , [ 'Not Found' ] ]
127+ }
129128 end
130129 end
131130
132- CloudController ::DependencyLocator . instance . cc_worker_prometheus_updater . update_gauge_metric ( :cc_db_connection_pool_timeouts_total , 0 , labels : { process_type : 'cc-worker' } )
131+ Thread . new do
132+ server = Puma ::Server . new ( metrics_app )
133+ server . add_tcp_listener '0.0.0.0' , config . get ( :prometheus_port ) || 9394
134+ server . run
135+ end
133136 end
134137end
0 commit comments