or run

tessl search
Log in

Version

Files

tile.json

task.mdevals/scenario-8/

Custom Router Instrumentation Library

Build a lightweight instrumentation library for web routing frameworks that patches router methods to add automatic timing and logging capabilities.

Problem Description

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.

Requirements

Router Method Patching

Your instrumentation should patch router methods to:

  • Track when each method is called and with what arguments
  • Measure execution time for each method call
  • Preserve the original return values
  • Maintain all original method properties and behavior

Duplicate Prevention

The system must prevent the same method from being patched multiple times:

  • Use a flag or marker to track whether a method has already been patched
  • Check for this marker before applying patches
  • Ensure that multiple calls to the instrumentation setup don't create nested wrappers

Property Preservation

When patching methods, preserve all original properties:

  • Copy function name, length, and other built-in properties
  • Maintain custom properties that may have been added to the function
  • Ensure that code checking for these properties continues to work correctly

Cleanup and Unwrapping

Support clean teardown of instrumentation:

  • Provide a way to restore original methods
  • Remove any tracking flags or markers
  • Ensure that after cleanup, the router behaves exactly as before instrumentation

Test Requirements

  • Patching a router method should wrap it while maintaining its original functionality @test
  • Calling a patched method multiple times should not result in multiple layers of wrapping @test
  • Custom properties on the original method should be accessible on the patched version @test
  • After cleanup, the router method should be fully restored to its original state @test

Implementation

@generates

API

/**
 * 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
};

Dependencies { .dependencies }

@opentelemetry/instrumentation-express { .dependency }

Provides patterns and utilities for instrumenting Express-like frameworks through method patching.

@satisfied-by