Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,30 @@ $ getAbbreviatedResponse({
...
});
$ endTime('abbr', "getAbbreviatedResponse");
```
```

# Usage - trackTime (async helper)

`trackTime` wraps any sync or async function, automatically recording its duration as a Server-Timing metric. It returns the function's result directly.

```typescript
import { trackTime } from 'lambda-server-timing';

// Time an async database query
const user = await trackTime('db-query', async () => {
return await dynamodb.get({ TableName: 'users', Key: { id } }).promise();
}, 'Fetch user from DynamoDB');

// Time an external API call
const data = await trackTime('external-api', async () => {
const res = await fetch('https://api.example.com/data');
return res.json();
});

// Also works with sync functions
const result = trackTime('compute', () => {
return heavyComputation(input);
}, 'Heavy computation');
```

If the wrapped function throws an error, `trackTime` still records the timing before re-throwing, so you get visibility into failed operations too.
9 changes: 9 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ export declare const startTime: (name: string, description?: string) => void;
* @returns TimeObject
* Records the duration of a metric and sets a metric timing
*/
/**
* @param name - Unique identifier for the metric
* @param fn - Async function to time
* @param description - Optional human-readable description
*
* @returns The return value of the provided function
* Wraps an async function, automatically recording its duration as a metric
*/
export declare const trackTime: <T>(name: string, fn: () => T | Promise<T>, description?: string) => Promise<T>;
export declare const endTime: (name: string, description?: string) => void | Record<string, unknown>;
interface TimeObject {
name: string;
Expand Down
30 changes: 29 additions & 1 deletion index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading