55import json
66import logging
77import os
8+ import re
89import signal
910import time
1011from abc import abstractmethod
@@ -184,17 +185,19 @@ class RequestStats:
184185 Class that holds the request statistics. Accessible in a User from self.environment.stats
185186 """
186187
187- def __init__ (self , use_response_times_cache = True ):
188+ def __init__ (self , use_response_times_cache = True , environment : Environment | None = None ):
188189 """
189190 :param use_response_times_cache: The value of use_response_times_cache will be set for each StatsEntry()
190191 when they are created. Settings it to False saves some memory and CPU
191192 cycles which we can do on Worker nodes where the response_times_cache
192193 is not needed.
194+ :param environment: The environment context.
193195 """
194196 self .use_response_times_cache = use_response_times_cache
195197 self .entries : dict [tuple [str , str ], StatsEntry ] = EntriesDict (self )
196198 self .errors : dict [str , StatsError ] = {}
197199 self .total = StatsEntry (self , "Aggregated" , None , use_response_times_cache = self .use_response_times_cache )
200+ self .environment = environment
198201 self .history = []
199202
200203 @property
@@ -217,8 +220,17 @@ def last_request_timestamp(self):
217220 def start_time (self ):
218221 return self .total .start_time
219222
223+ def exclude_from_total (self , method : str , name : str ):
224+ exclude_from_aggregation = getattr (getattr (self , "environment" , None ), "exclude_from_aggregation" , None )
225+ if exclude_from_aggregation :
226+ found_in_method = re .search (exclude_from_aggregation , method )
227+ found_in_name = re .search (exclude_from_aggregation , name )
228+ return found_in_method or found_in_name
229+ return False
230+
220231 def log_request (self , method : str , name : str , response_time : int , content_length : int ) -> None :
221- self .total .log (response_time , content_length )
232+ if not self .exclude_from_total (method , name ):
233+ self .total .log (response_time , content_length )
222234 self .entries [(name , method )].log (response_time , content_length )
223235
224236 def log_error (self , method : str , name : str , error : Exception | str | None ) -> None :
0 commit comments