@@ -138,43 +138,44 @@ def _get_client_ip(self, request: Request) -> str:
138138# Metrics tracking
139139class ServiceMetrics :
140140 """In-memory metrics tracking for observability"""
141+
141142 def __init__ (self ):
142143 self .start_time = time .time ()
143144 self .webhooks_received = 0
144145 self .webhooks_accepted = 0
145146 self .webhooks_rejected = 0
146147 self .kafka_publish_errors = 0
147148 self .processing_times_ms = []
148-
149+
149150 def record_webhook_received (self ):
150151 self .webhooks_received += 1
151-
152+
152153 def record_webhook_accepted (self ):
153154 self .webhooks_accepted += 1
154-
155+
155156 def record_webhook_rejected (self ):
156157 self .webhooks_rejected += 1
157-
158+
158159 def record_kafka_error (self ):
159160 self .kafka_publish_errors += 1
160-
161+
161162 def record_processing_time (self , duration_ms : float ):
162163 self .processing_times_ms .append (duration_ms )
163164 # Keep only last 1000 measurements to prevent memory issues
164165 if len (self .processing_times_ms ) > 1000 :
165166 self .processing_times_ms = self .processing_times_ms [- 1000 :]
166-
167+
167168 def get_percentile (self , percentile : int ) -> float :
168169 """Calculate percentile from processing times"""
169170 if not self .processing_times_ms :
170171 return 0.0
171172 sorted_times = sorted (self .processing_times_ms )
172173 index = int (len (sorted_times ) * percentile / 100 )
173174 return round (sorted_times [min (index , len (sorted_times ) - 1 )], 2 )
174-
175+
175176 def get_uptime_seconds (self ) -> int :
176177 return int (time .time () - self .start_time )
177-
178+
178179 def to_dict (self ) -> Dict [str , Any ]:
179180 return {
180181 "service" : "ingress" ,
@@ -190,6 +191,7 @@ def to_dict(self) -> Dict[str, Any]:
190191 "uptime_seconds" : self .get_uptime_seconds (),
191192 }
192193
194+
193195metrics = ServiceMetrics ()
194196
195197
@@ -360,7 +362,9 @@ def verify_github_signature(payload_body: bytes, signature_header: str) -> bool:
360362 return False
361363
362364
363- async def publish_to_kafka (topic : str , key : str , message : dict , correlation_id : Optional [str ] = None ):
365+ async def publish_to_kafka (
366+ topic : str , key : str , message : dict , correlation_id : Optional [str ] = None
367+ ):
364368 """
365369 Publish message to Kafka/Redpanda topic with optional correlation ID in headers
366370 """
@@ -369,7 +373,7 @@ async def publish_to_kafka(topic: str, key: str, message: dict, correlation_id:
369373 headers = []
370374 if correlation_id :
371375 headers .append (("correlation_id" , correlation_id .encode ("utf-8" )))
372-
376+
373377 future = producer .send (topic , key = key , value = message , headers = headers )
374378 record_metadata = future .get (timeout = 10 )
375379
@@ -383,7 +387,13 @@ async def publish_to_kafka(topic: str, key: str, message: dict, correlation_id:
383387 )
384388
385389 except Exception as e :
386- logger .error ("Failed to publish to Kafka" , topic = topic , key = key , correlation_id = correlation_id , error = str (e ))
390+ logger .error (
391+ "Failed to publish to Kafka" ,
392+ topic = topic ,
393+ key = key ,
394+ correlation_id = correlation_id ,
395+ error = str (e ),
396+ )
387397 metrics .record_kafka_error ()
388398 raise
389399
@@ -412,10 +422,10 @@ async def github_webhook(
412422 """
413423 start_time = time .time ()
414424 metrics .record_webhook_received ()
415-
425+
416426 try :
417427 correlation_id = str (uuid .uuid4 ())
418-
428+
419429 # Get raw body for signature verification
420430 body = await request .body ()
421431
@@ -428,7 +438,9 @@ async def github_webhook(
428438 try :
429439 payload = json .loads (body .decode ("utf-8" ))
430440 except json .JSONDecodeError as e :
431- logger .error ("Invalid JSON payload" , error = str (e ), correlation_id = correlation_id )
441+ logger .error (
442+ "Invalid JSON payload" , error = str (e ), correlation_id = correlation_id
443+ )
432444 raise HTTPException (status_code = 400 , detail = "Invalid JSON payload" )
433445
434446 # Get event type and validate
@@ -561,7 +573,12 @@ async def github_webhook(
561573 message_key = f"{ event_data ['repository' ]['full_name' ]} :{ event_data ['event_type' ]} :{ event_data .get ('issue' , event_data .get ('pull_request' , {})).get ('number' , 'unknown' )} "
562574 print (f"Publishing to Kafka: { message_key } " )
563575
564- await publish_to_kafka (KAFKA_TOPIC_RAW_ISSUES , message_key , event_data , correlation_id = correlation_id )
576+ await publish_to_kafka (
577+ KAFKA_TOPIC_RAW_ISSUES ,
578+ message_key ,
579+ event_data ,
580+ correlation_id = correlation_id ,
581+ )
565582
566583 logger .info (
567584 "Webhook processed successfully" ,
@@ -571,7 +588,7 @@ async def github_webhook(
571588 message_key = message_key ,
572589 correlation_id = correlation_id ,
573590 )
574-
591+
575592 # Record successful processing
576593 metrics .record_webhook_accepted ()
577594 processing_time_ms = (time .time () - start_time ) * 1000
0 commit comments