@@ -75,8 +75,7 @@ def initialize_metrics(self):
75
75
LAST_ACTIVITY .set_function (lambda : self .app .web_app .last_activity ().timestamp ())
76
76
ACTIVE_DURATION .set_function (
77
77
lambda : (
78
- self .app .web_app .last_activity ()
79
- - self .app .web_app .settings ["started" ]
78
+ self .app .web_app .last_activity () - self .app .web_app .settings ["started" ]
80
79
).total_seconds ()
81
80
)
82
81
@@ -152,13 +151,15 @@ def start(self, port: int = 9090) -> None:
152
151
self .initialize_metrics ()
153
152
154
153
# Create Tornado application with metrics handler
155
- app = tornado .web .Application ([
156
- (r"/metrics" , PrometheusMetricsHandler ),
157
- ])
154
+ app = tornado .web .Application (
155
+ [
156
+ (r"/metrics" , PrometheusMetricsHandler ),
157
+ ]
158
+ )
158
159
159
160
# Create HTTP server
160
161
self .server = tornado .httpserver .HTTPServer (app )
161
-
162
+
162
163
# Try to bind to the specified port
163
164
try :
164
165
self .server .bind (port )
@@ -178,7 +179,7 @@ def start(self, port: int = 9090) -> None:
178
179
# Start the server in a separate thread
179
180
self .thread = threading .Thread (target = self ._start_metrics_loop , daemon = True )
180
181
self .thread .start ()
181
-
182
+
182
183
# Wait for server to be ready
183
184
self ._wait_for_server_ready ()
184
185
self ._running = True
@@ -188,23 +189,23 @@ def _start_metrics_loop(self) -> None:
188
189
try :
189
190
# Create a new IOLoop for this thread
190
191
self .ioloop = tornado .ioloop .IOLoop ()
191
-
192
+
192
193
# Set as current event loop for this thread
193
194
asyncio .set_event_loop (self .ioloop .asyncio_loop )
194
-
195
+
195
196
# Start the server
196
197
self .server .start (1 ) # Single process
197
-
198
+
198
199
# Set up periodic updates in this IOLoop
199
200
def periodic_update_wrapper ():
200
201
if hasattr (self , "_periodic_update" ):
201
202
self ._periodic_update ()
202
203
# Schedule next update in 30 seconds
203
204
self .ioloop .call_later (30 , periodic_update_wrapper )
204
-
205
+
205
206
# Start periodic updates
206
207
self .ioloop .call_later (30 , periodic_update_wrapper )
207
-
208
+
208
209
# Start the IOLoop
209
210
self .ioloop .start ()
210
211
except Exception as e :
@@ -218,9 +219,9 @@ def _wait_for_server_ready(self, timeout: float = 5.0) -> None:
218
219
try :
219
220
with socket .socket (socket .AF_INET , socket .SOCK_STREAM ) as s :
220
221
s .settimeout (0.1 )
221
- s .connect ((' localhost' , self .port ))
222
+ s .connect ((" localhost" , self .port ))
222
223
return
223
- except ( socket . error , OSError ) :
224
+ except OSError :
224
225
time .sleep (0.1 )
225
226
raise TimeoutError (f"Server not ready after { timeout } seconds" )
226
227
0 commit comments