File size: 6,032 Bytes
e706de2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | /**
* Exercise 14: Build a Metrics Tracker with Metadata
*
* Goal: Learn to use config metadata and track metrics
*
* In this exercise, you'll:
* 1. Build a stateful callback that tracks metrics
* 2. Use config.metadata to track user information
* 3. Measure execution time for each Runnable
* 4. Generate a summary report
*
* This teaches you how to pass context through your chains!
*/
import {Runnable} from '../../../../src/index.js';
import {BaseCallback} from '../../../../src/utils/callbacks.js';
class MetricsTrackerCallback extends BaseCallback {
constructor() {
super();
// Track when each call started
this.startTimes = new Map();
// Metrics per runnable
this.metrics = {};
}
async onStart(runnable, input, config) {
const key = `${runnable.constructor.name}_${Date.now()}_${Math.random()}`;
this.startTimes.set(key, Date.now());
const userId = config?.metadata?.userId;
const runnableName = runnable.constructor.name;
if (!this.metrics[runnableName]) {
this.metrics[runnableName] = {
totalCalls: 0,
totalTime: 0,
users: new Set()
};
}
this.metrics[runnableName].totalCalls++;
if (userId) {
this.metrics[runnableName].users.add(userId);
}
// Store key for matching in onEnd
config._metricsKey = key;
}
async onEnd(runnable, output, config) {
const key = config._metricsKey;
const startTime = this.startTimes.get(key);
if (startTime) {
const duration = Date.now() - startTime;
const runnableName = runnable.constructor.name;
if (this.metrics[runnableName]) {
this.metrics[runnableName].totalTime += duration;
}
this.startTimes.delete(key);
}
}
async onError(runnable, error, config) {
const key = config._metricsKey;
if (key) {
this.startTimes.delete(key);
}
}
getReport() {
const report = {};
for (const [name, data] of Object.entries(this.metrics)) {
report[name] = {
calls: data.totalCalls,
totalTime: data.totalTime,
avgTime: data.totalCalls > 0 ? Math.round(data.totalTime / data.totalCalls) : 0,
users: Array.from(data.users)
};
}
return report;
}
printReport() {
console.log('\nπ Metrics Report:');
console.log('β'.repeat(60));
const report = this.getReport();
for (const [name, data] of Object.entries(report)) {
console.log(`${name}:`);
console.log(` Calls: ${data.calls}`);
console.log(` Total Time: ${data.totalTime}ms`);
console.log(` Avg Time: ${data.avgTime}ms`);
console.log(` Users: ${data.users.join(', ')}`);
console.log('');
}
}
}
// Test Runnables with different speeds
class FastRunnable extends Runnable {
async _call(input, config) {
await new Promise(resolve => setTimeout(resolve, 100));
return `Fast: ${input}`;
}
}
class SlowRunnable extends Runnable {
async _call(input, config) {
await new Promise(resolve => setTimeout(resolve, 500));
return `Slow: ${input}`;
}
}
class MediumRunnable extends Runnable {
async _call(input, config) {
await new Promise(resolve => setTimeout(resolve, 250));
return `Medium: ${input}`;
}
}
async function exercise() {
console.log('=== Exercise 14: Metrics Tracker with Metadata ===\n');
const metrics = new MetricsTrackerCallback();
const fast = new FastRunnable();
const medium = new MediumRunnable();
const slow = new SlowRunnable();
// Test 1: User 1 makes some calls
console.log('--- User 1 making calls ---');
await fast.invoke('test1', { callbacks: [metrics], metadata: { userId: "user_123" } });
await medium.invoke('test2', { callbacks: [metrics], metadata: { userId: "user_123" } });
await fast.invoke('test3', { callbacks: [metrics], metadata: { userId: "user_123" } });
// Test 2: User 2 makes different calls
console.log('--- User 2 making calls ---');
await slow.invoke('test4', { callbacks: [metrics], metadata: { userId: "user_456" } });
await fast.invoke('test5', { callbacks: [metrics], metadata: { userId: "user_456" } });
// Test 3: User 3 makes calls
console.log('--- User 3 making calls ---');
await medium.invoke('test6', { callbacks: [metrics], metadata: { userId: "user_789" } });
await slow.invoke('test7', { callbacks: [metrics], metadata: { userId: "user_789" } });
await medium.invoke('test8', { callbacks: [metrics], metadata: { userId: "user_789" } });
metrics.printReport();
console.log('\nβ Exercise 2 complete!');
}
// Run the exercise
exercise().catch(console.error);
/**
* Expected Output:
*
* π Metrics Report:
* ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* FastRunnable:
* Calls: 3
* Total Time: 305ms
* Avg Time: 102ms
* Users: user_123, user_456
*
* SlowRunnable:
* Calls: 2
* Total Time: 1008ms
* Avg Time: 504ms
* Users: user_456, user_789
*
* MediumRunnable:
* Calls: 3
* Total Time: 756ms
* Avg Time: 252ms
* Users: user_123, user_789
*
* Learning Points:
* 1. Stateful callbacks can accumulate data
* 2. config.metadata passes arbitrary context
* 3. Useful for tracking per-user metrics
* 4. Map.set() and Map.get() for tracking start times
* 5. Report generation for observability
*/ |