5
5
import json
6
6
import logging
7
7
import os
8
+ import re
8
9
import signal
9
10
import time
10
11
from abc import abstractmethod
14
15
from html import escape
15
16
from itertools import chain
16
17
from types import FrameType
17
- from typing import TYPE_CHECKING , Any , Callable , NoReturn , Protocol , TypedDict , TypeVar , cast
18
+ from typing import TYPE_CHECKING , Any , Callable , NoReturn , Optional , Protocol , TypedDict , TypeVar , cast
18
19
19
20
import gevent
20
21
@@ -184,17 +185,19 @@ class RequestStats:
184
185
Class that holds the request statistics. Accessible in a User from self.environment.stats
185
186
"""
186
187
187
- def __init__ (self , use_response_times_cache = True ):
188
+ def __init__ (self , use_response_times_cache = True , environment : Optional [ Environment ] = None ):
188
189
"""
189
190
:param use_response_times_cache: The value of use_response_times_cache will be set for each StatsEntry()
190
191
when they are created. Settings it to False saves some memory and CPU
191
192
cycles which we can do on Worker nodes where the response_times_cache
192
193
is not needed.
194
+ :param environment: The environment context.
193
195
"""
194
196
self .use_response_times_cache = use_response_times_cache
195
197
self .entries : dict [tuple [str , str ], StatsEntry ] = EntriesDict (self )
196
198
self .errors : dict [str , StatsError ] = {}
197
199
self .total = StatsEntry (self , "Aggregated" , None , use_response_times_cache = self .use_response_times_cache )
200
+ self .environment = environment
198
201
self .history = []
199
202
200
203
@property
@@ -217,8 +220,17 @@ def last_request_timestamp(self):
217
220
def start_time (self ):
218
221
return self .total .start_time
219
222
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
+
220
231
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 )
222
234
self .entries [(name , method )].log (response_time , content_length )
223
235
224
236
def log_error (self , method : str , name : str , error : Exception | str | None ) -> None :
0 commit comments