11# frozen_string_literal: true
22
3- require 'socket'
4- require 'thread'
5- require 'logger'
3+ require "socket"
4+ require "logger"
65
76module PrometheusExporter
87 class Client
@@ -25,7 +24,9 @@ def standard_values(value, keys, prometheus_exporter_action = nil)
2524 keys : keys ,
2625 value : value
2726 }
28- values [ :prometheus_exporter_action ] = prometheus_exporter_action if prometheus_exporter_action
27+ values [
28+ :prometheus_exporter_action
29+ ] = prometheus_exporter_action if prometheus_exporter_action
2930 values [ :opts ] = @opts if @opts
3031 values
3132 end
@@ -57,8 +58,11 @@ def self.default=(client)
5758 attr_reader :logger
5859
5960 def initialize (
60- host : ENV . fetch ( 'PROMETHEUS_EXPORTER_HOST' , 'localhost' ) ,
61- port : ENV . fetch ( 'PROMETHEUS_EXPORTER_PORT' , PrometheusExporter ::DEFAULT_PORT ) ,
61+ host : ENV . fetch ( "PROMETHEUS_EXPORTER_HOST" , "localhost" ) ,
62+ port : ENV . fetch (
63+ "PROMETHEUS_EXPORTER_PORT" ,
64+ PrometheusExporter ::DEFAULT_PORT
65+ ) ,
6266 max_queue_size : nil ,
6367 thread_sleep : 0.5 ,
6468 json_serializer : nil ,
@@ -90,7 +94,8 @@ def initialize(
9094 @mutex = Mutex . new
9195 @thread_sleep = thread_sleep
9296
93- @json_serializer = json_serializer == :oj ? PrometheusExporter ::OjCompat : JSON
97+ @json_serializer =
98+ json_serializer == :oj ? PrometheusExporter ::OjCompat : JSON
9499
95100 @custom_labels = custom_labels
96101 end
@@ -100,7 +105,14 @@ def custom_labels=(custom_labels)
100105 end
101106
102107 def register ( type , name , help , opts = nil )
103- metric = RemoteMetric . new ( type : type , name : name , help : help , client : self , opts : opts )
108+ metric =
109+ RemoteMetric . new (
110+ type : type ,
111+ name : name ,
112+ help : help ,
113+ client : self ,
114+ opts : opts
115+ )
104116 @metrics << metric
105117 metric
106118 end
@@ -161,9 +173,7 @@ def stop(wait_timeout_seconds: 0)
161173 @mutex . synchronize do
162174 wait_for_empty_queue_with_timeout ( wait_timeout_seconds )
163175 @worker_thread &.kill
164- while @worker_thread &.alive?
165- sleep 0.001
166- end
176+ sleep 0.001 while @worker_thread &.alive?
167177 @worker_thread = nil
168178 close_socket!
169179 end
@@ -183,12 +193,13 @@ def ensure_worker_thread!
183193 @mutex . synchronize do
184194 return if @worker_thread &.alive?
185195
186- @worker_thread = Thread . new do
187- while true
188- worker_loop
189- sleep @thread_sleep
196+ @worker_thread =
197+ Thread . new do
198+ while true
199+ worker_loop
200+ sleep @thread_sleep
201+ end
190202 end
191- end
192203 end
193204 end
194205 rescue ThreadError => e
@@ -212,7 +223,8 @@ def close_socket!
212223 end
213224
214225 def close_socket_if_old!
215- if @socket_pid == Process . pid && @socket && @socket_started && ( ( @socket_started + MAX_SOCKET_AGE ) < Time . now . to_f )
226+ if @socket_pid == Process . pid && @socket && @socket_started &&
227+ ( ( @socket_started + MAX_SOCKET_AGE ) < Time . now . to_f )
216228 close_socket!
217229 end
218230 end
@@ -240,7 +252,7 @@ def ensure_socket!
240252 end
241253
242254 nil
243- rescue
255+ rescue StandardError
244256 @socket = nil
245257 @socket_started = nil
246258 @socket_pid = nil
@@ -250,7 +262,10 @@ def ensure_socket!
250262 def wait_for_empty_queue_with_timeout ( timeout_seconds )
251263 start_time = ::Process . clock_gettime ( ::Process ::CLOCK_MONOTONIC )
252264 while @queue . length > 0
253- break if start_time + timeout_seconds < ::Process . clock_gettime ( ::Process ::CLOCK_MONOTONIC )
265+ if start_time + timeout_seconds <
266+ ::Process . clock_gettime ( ::Process ::CLOCK_MONOTONIC )
267+ break
268+ end
254269 sleep ( 0.05 )
255270 end
256271 end
0 commit comments