Java Code Review Solution
This article presents the challenges, solution features, and key benefits of a code review solution tool, which is used to validate that all critical events are logged.
Join the DZone community and get the full member experience.
Join For FreeA code review solution is a tool to validate that all critical events are logged with the required information and follow best practices. This low-code utility uses user-input application code to produce exception reports.
Code Review Challenges
- Manually reviewing each logger statement is a time-consuming activity and risks human error.
- Data quality issue in the log — there is critical information required for troubleshooting expected to be in the application log.
- Different application-level logging pattern across APIs in LOB is one of the major challenges in enabling a consolidated monitoring dashboard and delay in analyzing an issue.
Solution Features
1. Logger statement with unique ID validation
def check_traceability(folder_path):
for java_file in find_java_files(folder_path):
with open(java_file, "r") as f:
print(java_file)
lines = f.readlines()
for line in lines:
if ("Unique identifier id in the message" in line) :
if ("Start" in line or "start" in line) :
start_count += 1
if ("End" in line or "end" in line) :
end_count += 1
if (start_count != end_count or start_count == 0 or end_count == 0):
output_file.write(" \n")
output_file.write("{} -is missing Unique identifier id with 'Start' or 'End' \n".format(java_file))
2. Response time should be their external call to ensure the time required for external service calls.
for line in lines:
# search for controller class
if "RestController" in line:
has_rest_controller = True
# search for keyword for CICS mainframe requests
if "CICS request execute staments" in line:
cicsrec_count += 1
# search for keyword for third part service call requests
if "HTTP response key word for service call" in line:
closeable_count += 1
# search for keyword for DB execute statements
if "DB execute stament key word" in line:
dbcall_count += 1
if ("Unique identifier id in the message" in line) :
if ("response" in line or "Response Time" in line or "response time" in line) :
response_count += 1
if (has_rest_controller and response_count == 0):
output_file.write(" \n")
output_file.write("{} -is missing Unique identifier id with Response Time' \n".format(java_file))
if ((cicsrec_count > 0) and (cicsrec_count!= response_count)):
output_file.write(" \n")
output_file.write("{} -is missing Unique identifier id with 'responseTime' for CICS call \n".format(java_file))
if ((closeable_count > 0) and (closeable_count!= response_count)):
output_file.write(" \n")
output_file.write("{} -is missing 'responseTime' for service call \n".format(java_file))
if ((dbcall_count > 0) and (dbcall_count!= response_count)):
output_file.write(" \n")
output_file.write("{} -is missing traceabilty id with 'responseTime' for DB call \n".format(java_file))
3. Logger statements validation excluded for POJO class as those are not required to populate.
def find_java_files(folder_path):
# Define the file patterns to search for
java_patterns = ['*.java']
# Define the folder names to ignore
ignore_folders = ['bo', 'model', 'config']
# Traverse the directory tree recursively
for root_folder, dirnames, filenames in os.walk(folder_path):
# Exclude the folders in ignore_folders list
dirnames[:] = [d for d in dirnames if d not in ignore_folders]
# Search for matching files in the current folder
for java_pattern in java_patterns:
for filename in fnmatch.filter(filenames, java_pattern):
yield os.path.join(root_folder, filename)
4. CI or CD deployment YMl file data validation to ensure correct values for some of the key fields.
def ci_ver(folder_path):
for root, dirs, files in os.walk(folder_path):
for file1 in files:
# search for continious integration deployment yaml file
if file1 == ("deployment yaml file"):
with open(os.path.join(root, file1), "r") as f:
contents = f.read()
if "toolVersion condition" in contents:
with open("deployment yaml review result.txt", "w") as output_file:
output_file.write(" \n")
output_file.write((os.path.join(root,file1)))
output_file.write("\n borkvresion found in deployment yaml , pls remove. \n")
else:
with open ("deployment yaml review result.txt" , "w") as output_file:
output_file.write("\n toolVersion condition not found in deployment yaml No action required \n")
Key Benefits
1. Would help in troubleshooting as a unique ID would have populated in the log for tracking
2. Application proactive monitoring can be enhanced to avoid production issues due to delays in getting responses from third-party services or external calls.
3. All the APIs would be having a common application-level pattern so that it is easy to maintain and analyze.
4. Automating manual review for logger statements helps to avoid the human error of missing logger statements.
Software Requirement and Execution Procedure
This Python code validates logger statements to ensure followed all standards with all the critical, required information for logging.
Software requirement — Python version should be more than 3.0. Python version - 3.11.1
To install raumel.yml - pip
install raumel.yaml
Execute Script — python review.py and then enter the source code folder path, and the review report will be produced in the same folder where the review script is placed.
Opinions expressed by DZone contributors are their own.
Comments