Problem: Track API Usage Metrics Transparently
Problem statement
Your monitoring system needs to record how frequently different service methods are used, but the service code itself can’t be modified. Adding manual counters inside every method would make the code messy and prone to errors.
Instead, you’ll wrap the service in a Proxy that intercepts all method calls and keeps track of call counts transparently. This tracking layer should be completely invisible to the rest of the application—methods should behave the same, but usage data should accumulate behind the scenes.
Goal
Create a function createTrackedService() that returns a Proxy wrapping a given service object. The Proxy should track the number of times each method is called and expose a .getStats() method to retrieve the metrics.
Constraints
You must use a
Proxywith agettrap.Method behavior must remain unchanged.
Track counts per method name.
Expose a
.getStats()method that returns an object like{ methodName: callCount }.
Sample output
The examples below illustrate what the output should look like:
const service = createTrackedService();console.log(service.fetchData());/* Expected output:Data fetched*/console.log(service.postData());/* Expected output:Data posted*/console.log(service.fetchData());/* Expected output:Data fetched*/console.log(service.getStats()); // should show call counts per method/* Expected output:{ fetchData: 2, postData: 1 }*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.
Problem: Track API Usage Metrics Transparently
Problem statement
Your monitoring system needs to record how frequently different service methods are used, but the service code itself can’t be modified. Adding manual counters inside every method would make the code messy and prone to errors.
Instead, you’ll wrap the service in a Proxy that intercepts all method calls and keeps track of call counts transparently. This tracking layer should be completely invisible to the rest of the application—methods should behave the same, but usage data should accumulate behind the scenes.
Goal
Create a function createTrackedService() that returns a Proxy wrapping a given service object. The Proxy should track the number of times each method is called and expose a .getStats() method to retrieve the metrics.
Constraints
You must use a
Proxywith agettrap.Method behavior must remain unchanged.
Track counts per method name.
Expose a
.getStats()method that returns an object like{ methodName: callCount }.
Sample output
The examples below illustrate what the output should look like:
const service = createTrackedService();console.log(service.fetchData());/* Expected output:Data fetched*/console.log(service.postData());/* Expected output:Data posted*/console.log(service.fetchData());/* Expected output:Data fetched*/console.log(service.getStats()); // should show call counts per method/* Expected output:{ fetchData: 2, postData: 1 }*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.