Skip to content

Commit 5c9de2a

Browse files
authored
Add Log Handler Filter To workload tests to reduce logging noise (Azure#40700)
* Update workload_utils.py Added filters for the worklaod test logger handler. It will only log when database account read happens, when latency is above 1000ms, and if there is an error. It won't log certain errors such as 404/0, 409/0, and 412/0. Given the test uses randint for upserting and reading documents a 404/0 and 409/0 is bound to occur and would just be considered noise for the workload tests. * Update workload_utils.py * Update workload_utils.py
1 parent e60744e commit 5c9de2a

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

sdk/cosmos/azure-cosmos/tests/workloads/workload_utils.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
from logging.handlers import RotatingFileHandler
99
from workload_configs import NUMBER_OF_LOGICAL_PARTITIONS, PARTITION_KEY
1010

11+
_NOISY_ERRORS = set([404, 409, 412])
12+
_NOISY_SUB_STATUS_CODES = set([0, None])
13+
_REQUIRED_ATTRIBUTES = ["resource_type", "verb", "operation_type", "status_code", "sub_status_code", "duration"]
1114

1215
def get_random_item():
1316
random_int = random.randint(0, NUMBER_OF_LOGICAL_PARTITIONS)
@@ -49,9 +52,30 @@ def create_logger(file_name):
4952
# Create a rotating file handler
5053
handler = RotatingFileHandler(
5154
"log-" + prefix + "-" + datetime.now().strftime("%Y%m%d-%H%M%S") + '.log',
52-
maxBytes=1024 * 1024 * 10, # 10 mb
55+
maxBytes=1024 * 1024 * 10, # 10 mb
5356
backupCount=3
5457
)
5558
logger.setLevel(logging.DEBUG)
59+
# create filters for the logger handler to reduce the noise
60+
workload_logger_filter = WorkloadLoggerFilter()
61+
handler.addFilter(workload_logger_filter)
5662
logger.addHandler(handler)
57-
return prefix, logger
63+
return prefix, logger
64+
65+
66+
class WorkloadLoggerFilter(logging.Filter):
67+
def filter(self, record):
68+
# Check if the required attributes exist in the log record
69+
if all(hasattr(record, attr) for attr in _REQUIRED_ATTRIBUTES):
70+
# Check the conditions
71+
# Check database account reads
72+
if record.resource_type == "databaseaccount" and record.verb == "GET" and record.operation_type == "Read":
73+
return True
74+
# Check if there is an error and omit noisy errors
75+
if record.status_code >= 400 and not (
76+
record.status_code in _NOISY_ERRORS and record.sub_status_code in _NOISY_SUB_STATUS_CODES):
77+
return True
78+
# Check if the latency (duration) was above 1000 ms
79+
if record.duration >= 1000:
80+
return True
81+
return False

0 commit comments

Comments
 (0)