docs
evals
scenario-1
scenario-10
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
Build a lightweight instrumentation library for web routing frameworks that patches router methods to add automatic timing and logging capabilities.
You need to create an instrumentation system that wraps router methods to automatically track execution time and log method calls. The system should patch methods dynamically while preserving their original behavior and properties, preventing duplicate instrumentation, and supporting proper cleanup.
Your instrumentation should patch router methods to:
The system must prevent the same method from being patched multiple times:
When patching methods, preserve all original properties:
Support clean teardown of instrumentation:
/**
* Patches a method on a target object to add instrumentation.
* Returns the patched method. Prevents duplicate patching.
*
* @param {Object} target - The object containing the method to patch
* @param {string} methodName - The name of the method to patch
* @param {Function} wrapper - A function that wraps the original method
* @returns {Function} The patched method
*/
function patchMethod(target, methodName, wrapper) {
// Implementation here
}
/**
* Checks if a method has already been patched.
*
* @param {Function} method - The method to check
* @returns {boolean} True if the method has been patched
*/
function isPatched(method) {
// Implementation here
}
/**
* Restores a patched method to its original implementation.
*
* @param {Object} target - The object containing the patched method
* @param {string} methodName - The name of the method to restore
* @returns {boolean} True if the method was successfully restored
*/
function unpatchMethod(target, methodName) {
// Implementation here
}
module.exports = {
patchMethod,
isPatched,
unpatchMethod
};Provides patterns and utilities for instrumenting Express-like frameworks through method patching.