2121import requests
2222import logging
2323import logging .handlers
24+ import random
2425
2526# ULS specific modules
2627import config .global_config as uls_config
@@ -93,6 +94,12 @@ def __init__(self, output_type: str,
9394
9495 # HTTP parameters
9596 elif self .output_type in ['HTTP' ] and http_url :
97+
98+ # ---- Begin change for EME-588 ----
99+ self .aggregateList = list ()
100+ self .aggregateListTick = None # Last time we added items in the list
101+ # ---- End change for EME-588 ----
102+
96103 self .http_url = http_url
97104 # apply other variables if SET
98105
@@ -375,7 +382,7 @@ def send_data(self, data):
375382 :return: True on successful send, False on error
376383 """
377384 try :
378- aka_log .log .info (f"{ self .name } Trying to send data via { self .output_type } " )
385+ aka_log .log .debug (f"{ self .name } Trying to send data via { self .output_type } " )
379386
380387 if self .output_type == "TCP" :
381388 self .clientSocket .sendall (data )
@@ -384,30 +391,41 @@ def send_data(self, data):
384391 self .clientSocket .sendto (data , (self .host , self .port ))
385392
386393 elif self .output_type == "HTTP" :
387- response = self .httpSession .post (url = self .http_url ,
388- data = self .http_out_format % (data .decode ()),
389- verify = self .http_verify_tls ,
390- timeout = self .http_timeout )
391- aka_log .log .debug (f"{ self .name } DATA Send response { response .status_code } ,"
392- f" { response .text } " )
394+ self .aggregateList .append (data )
395+ if len (self .aggregateList ) == uls_config .output_http_aggregate_count or (
396+ self .aggregateListTick is not None and
397+ self .aggregateListTick < time .time () - uls_config .output_http_aggregate_idle
398+ ):
399+ data = uls_config .output_line_breaker .join (
400+ self .http_out_format % (event .decode ()) for event in self .aggregateList )
401+ request = requests .Request ('POST' , url = self .http_url , data = data )
402+ prepped = self .httpSession .prepare_request (request )
403+ payload_length = prepped .headers ["Content-Length" ]
404+ response = self .httpSession .send (prepped , verify = self .http_verify_tls , timeout = self .http_timeout )
405+ response .close () # Free up the underlying TCP connection in the connection pool
406+ aka_log .log .info (f"{ self .name } HTTP POST of { len (self .aggregateList )} event(s) "
407+ f"completed in { (response .elapsed .total_seconds ()* 1000 ):.3f} ms, "
408+ f"payload={ payload_length } bytes, HTTP response { response .status_code } , "
409+ f"response={ response .text } " )
410+ self .aggregateList .clear ()
411+ self .aggregateListTick = time .time ()
393412
394413 elif self .output_type == "RAW" :
395414 sys .stdout .write (data .decode ())
396415 sys .stdout .flush ()
397416
398-
399417 elif self .output_type == "FILE" :
400418 self .my_file_writer .info (f"{ data .decode ().rstrip ()} " )
401419
402420 else :
403421 aka_log .log .critical (f"{ self .name } target was not defined { self .output_type } " )
404422 sys .exit (1 )
405423
406- aka_log .log .info (f"{ self .name } Data successfully sent via { self .output_type } " )
424+ aka_log .log .debug (f"{ self .name } Data successfully sent via { self .output_type } " )
407425 return True
408426
409427 except Exception as my_error :
410- aka_log .log .error (f"{ self .name } Issue sending data { my_error } " )
428+ aka_log .log .exception (f"{ self .name } Issue sending data { my_error } " )
411429 self .connected = False
412430 self .connect ()
413431 return False
0 commit comments