An open API service indexing awesome lists of open source software.

https://github.com/nodesource/ah-collector

Super light wrapper around async-hooks to collect information about resources created during async operations.
https://github.com/nodesource/ah-collector

Last synced: 14 days ago
JSON representation

Super light wrapper around async-hooks to collect information about resources created during async operations.

Awesome Lists containing this project

README

          

# ah-collector

Super light wrapper around async-hooks to collect information about resources created during async operations.

```js
const ActivityCollector = require('ah-collector')

function captureStack(hook, { uid, type, triggerId }, resource) {
// Predicate to decide if a stack should be captured or not.
// Capture only inits of the TIMERWRAP but everything of the Timeout.
return type === 'Timeout' || (type === 'TIMERWRAP' && hook === 'init')
}

const collector = new ActivityCollector({
start: process.hrtime()
, captureStack
}).enable()

setTimeout(ontimeout, 100)

function ontimeout() {
collector
.processStacks()
.dump()
}
```

## Requirements

Needs async hooks feature, therefore build from [this PR](https://github.com/nodejs/node/pull/8531) for now.

[EPS document](https://github.com/nodejs/node-eps/pull/18)

[Documentation in
progress](https://github.com/nodesource/node/blob/trevnorris-async-wrap-eps-impl%2Bdocs/doc/api/async_hooks.md)

## Installation

npm install ah-collector

## [API](https://nodesource.github.io/ah-collector)

### ActivityCollector

Creates an instance of an ActivityCollector

**Parameters**

- `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `$0.start` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>** start time obtained via `process.hrtime()`
- `$0.stackCapturer` **StackCapturer?** which is used to decide if a stack
should be captured as well as to capture and process it @see
[thlorenz/ah-stack-capturer](https://github.com/nodesource/ah-stack-capturer)
The default capturer used doesn't ever capture a stack so this feature is
turned off by default. (optional, default `defaultStackCapturer`)
- `$0.requireInit` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** when `true` any activities whose `init` hook we missed
are ignored even if we see `before`, `after` and/or `destroy` hooks. (optional, default `false`)

Returns **[ActivityCollector](#activitycollector)** instance of ActivityCollector

### activityCollector.enable

Enables the collection of async hooks.
**Needs to be called** as otherwise nothing will be collected.

Returns **[ActivityCollector](#activitycollector)** activityCollector

### activityCollector.disable

Disables the collection of async hooks.
Nothing will be collected until `activityCollector.enable()` is called.

Returns **[ActivityCollector](#activitycollector)** activityCollector

### activityCollector.clear

Clears all currently collected activity.

Returns **[ActivityCollector](#activitycollector)** activityCollector

### activityCollector.activitiesOfTypes

Returns an Array of all activities collected so far that are of the specified type(s).

**Parameters**

- `type` **([Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> | [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String))** the type to match

Returns **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)** activities matching the specified type(s)

### activityCollector.activities

A `getter` that returns a map of all activities collected so far.

Returns **[Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)** activities

### activityCollector.activitiesArray

A `getter` that returns an Array of all activities collected so far.

Returns **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)** activities

### activityCollector.processStacks

Processes all stacks that were captured for specific activities
This is done in line, i.e. the actual stacks of the activity objects
are modified.

Returns **[ActivityCollector](#activitycollector)** activityCollector

### activityCollector.dump

Dumps all so far collected activities to the console.
This is useful for diagnostic purposes.

If no arguments are provided, all activities are dumped.

**Parameters**

- `opts` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** allow tweaking which activities are dumped and how
- `opts.types` **([Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> | [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String))** type(s) to dump
- `opts.stage` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** the stage to print as the title of the dump
- `opts.depth` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** the depth with which the dumped object is dumped

Returns **[ActivityCollector](#activitycollector)** activityCollector

### activityCollector.dumpTypes

Dumps all types in the order they were collected including
id and trigger id.

Example: `FSREQWRAP:id:triggerId`

### ActivityCollector#UNKNOWN_TYPE

Static getter that denotes the type given to activities whose type is unkown since
we missed their `init` event.

## License

MIT