Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

monitor

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

monitor - npm Package Compare versions

Comparing version 0.2.11 to 0.4.0

.npmignore

50

package.json
{
"name": "monitor",
"version": "0.2.11",
"main": "./lib/node-monitor.js",
"description": "Runtime monitoring for node.js applications",
"author": "Loren West <open_source@lorenwest.com>",
"directories": {"lib": "./lib", "test": "./test"},
"modules": {
"console-logger": "./logger/console-logger",
"file-logger": "./logger/file-logger",
"os-cmd-logger": "./logger/os-cmd-logger",
"email-logger": "./logger/email-logger",
"couchdb-logger": "./logger/couchdb-logger"
},
"version": "0.4.0",
"main": "./lib/index.js",
"author": {
"name": "Loren West",
"email": "open_source@lorenwest.com",
"url": "https://github.com/lorenwest"
},
"homepage": "http://lorenwest.github.com/monitor/",
"repository": {
"type": "git",
"url": "git://github.com/lorenwest/monitor.git"
},
"bugs": {
"url": "https://github.com/loren/monitor/issues"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/lorenwest/monitor/blob/master/LICENSE"
}
],
"dependencies": {
"config": ">=0.2.8",
"sprintf": ">=0.1.1",
"vows": ">=0.5.6",
"couch-client": ">=0.0.3"
"config": "0.4.x",
"cron": "0.1.x",
"backbone": "0.9.x",
"underscore": "1.3.x",
"socket.io-client": "0.9.x",
"socket.io": "0.9.x"
},
"engines": {"node": ">=0.2.6"},
"devDependencies": {
"grunt": "~0.3.6"
},
"engines": { "node": ">= 0.6.0" },
"scripts": {
"test": "vows test/*.js --spec"
"test": "grunt test",
"start": "node server"
}
}

@@ -1,5 +0,5 @@

node-monitor
============
Monitor
=======
Runtime monitoring for node.js applications
Remote monitoring for Node.js applications

@@ -9,452 +9,73 @@ Introduction

node-monitor records and gives you access to vital statistics about your
running node.js application.
This package provides a foundation for monitoring and controlling remote
node.js applications.
As a developer, node-monitor gives you a consistent pattern for exposing
business events, monitoring exceptions, and managing errors and callbacks
in your asynchronous code.
It introduces the concept of a [Probe](http://lorenwest.github.com/monitor/doc/classes/Probe.html) -
a small software component capable of exposing and controlling state within a
running node.js server.
During configuration and deployment, node-monitor lets you configure
standard output logging, or customized logging and alerts. Logging and
alerts can be defined application-wide, module-wide, or for a specific
event or error condition.
Probes are written as
[Backbone](http://documentcloud.github.com/backbone) models, and
remain dormant in your process until instantiated from remote monitors.
The provided loggers let you log to files, to a CouchDB database, to an email
distribution list, an SMS gateway, an internal event, or an external program.
From the monitoring process, a [Monitor](http://lorenwest.github.com/monitor/doc/classes/Monitor.html) class
is provided to connect with a remote probe.
When your application exits, node-monitor produces an activity report listing
activity for each monitor that fired throughout the lifetime of the application.
This package is used for writing and embedding probes into your app server,
and for writing custom clients for inspecting and controlling these probes.
A companion package -
[Node-Monitor](http://lorenwest.github.com/node-monitor) - provides a user interface
for building real-time monitor dashboards.
Quick Start
-----------
Installation & Testing
----------------------
**Install using npm**
Node-monitor installs with *npm* and comes with an extensive suite of tests to
make sure it performs well in your deployment environment.
To install and test node-monitor:
$ npm install monitor
$ npm test monitor
**Start the monitor service (standalone)**
Event Monitoring
----------------
Normally you'll include this package into your own application server, but you can
run as a standalone application as well.
Start by defining the monitor at the top of your module. Subsequent examples
use this *monitor* variable.
$ npm start monitor
var monitor = require('monitor')('Customers');
**Observe a probe from a remote process**
Whenever you wish to record an event, call *monitor.event()*:
In this example we're using a REPL console to connect with the
built-in [Process](http://http://lorenwest.github.com/monitor/doc/classes/Process.html) probe.
monitor.event('Customer purchase', invoice.amount);
Open a REPL console from another terminal
If you pass a Date object to the monitor, it assumes it's a start of an
interval, and records the number of milliseconds from that time. Example:
$ node
var beforeDbSave = new Date();
db.save(customer, function() {
Create a monitor for the Process probe
// Monitor the DB save time
monitor.event('Customer save time, ms.', beforeDbSave);
> var Monitor = require('monitor');
> var processMonitor = new Monitor({server:'localhost', probeClass: 'Process'});
});
Connect with the probe, and view the properties
In the above example, customer database save times are monitored. Logging
can be turned on or off at configuration time or at runtime. First, last,
maximum, minimum, average, count, and total execution times are maintained for
the *Customer save time, ms.* monitor.
> processMonitor.connect();
> processMonitor.toJSON();
Error Monitoring
----------------
The monitor is a Backbone model, so you can watch for changes
Node-monitor lets you keep track of the health of your running node.js
application by giving you a consistent pattern for trapping program errors
and exceptions.
> var showFreeMem = function(){console.log(processMonitor.get('freemem'))};
> processMonitor.on('change', showFreeMem);
Errors are conditions preventing your function from producing the intended
results. Here's an example using the try/catch pattern:
See Also
--------
try {
... do something
} catch (e) {
monitor.error('Customer insertion failure', e);
...
}
* [API-Docs](http://lorenwest.github.com/monitor/doc/index.html) Monitor internal documentation
* [Node-Monitor](http://lorenwest.github.com/node-monitor) Companion webapp for building real-time monitor dashboards
In the above example, if an exception is thrown between the try and catch
block, the *Customer insertion failure* monitor will log the error, and keep
track of failure statistics.
License
-------
Using the asynchronous callback pattern, you can monitor errors that occur
in asynchronous functions, easily trapping and forwarding these errors to the
callback function.
Released under the Apache License 2.0
Long form example:
See [LICENSE](https://github.com/lorenwest/monitor/blob/master/LICENSE) file.
function saveCustomer(customer, callback) {
...
db.save(customer.id, function(err, dbObject) {
// Forward database errors to our callback
if (err) {
monitor.error('Customer db.save error', err);
if (callback) {
callback(err);
}
return;
}
...
});
}
Same, only using the short form:
function saveCustomer(customer, callback) {
...
db.save(customer.id, function(err, dbObject) {
// Forward database errors to our callback
if (err) return monitor.error('Customer db.save error', err, callback);
...
});
}
In these examples, the *Customer db.save error* monitor will log and track the
error, and if a callback is specified, it is called with the error object as
the first (and only) argument.
Logging & Alerts
----------------
Monitor loggers are separated into two categories - eventLoggers and
errorLoggers. These loggers can be configured application-wide, module-wide,
or for individual event and error types.
The default event logger is *require('sys').log*, and the default error logger
is *require('sys').debug*. Changing these defaults and writing your own loggers
are done at configuration time (see _Configuration_ below)
External alerts are written with loggers, and can be as simple or as complex
as you desire. For example, you could write a logger which sends an email
when the error occurs.
Email logger example:
// Email logger
var fs = require('fs');
var exec = require('child_process').exec;
var mail_cmd = "mail -s 'Program Error' john.doe@example.com";
var errorLogger = function(message, value, err, monitor) {
// Write the error message to a temp file
var tmpfile = "/tmp/" + Math.random() + ".out";
fs.writeFile(tmpfile, message, function (err) {
if (err) throw err;
// Mail the message
exec(mail_cmd + " < " + tmpfile, function(err) {
if (err) throw err;
// Remove the temporary file
fs.unlink(tmpfile, function(err){
if (err) throw err;
});
});
});
}
See the _Configuration_ section below for attaching loggers to various monitors
within your application.
Configuration
-------------
node-monitor uses the config package for configuration. This allows you to
specify configurations in a file or on the command line. See the node-config
package for more information about specifying configuration options for your
different application deployments.
The following configuration parameters are recognized:
* enabled - (boolean) Should the monitor be enabled? (default: true)
* eventLogger - Logger[s] to use for events (default: sys.log)
Can be a single logger, or an array of loggers.
* errorLogger - Logger[s] to use for errors (default: sys.debug)
Can be a single logger, or an array of loggers.
* maxLogSize - Limit the log output to this size (default: 10k)
These configuration parameters can be specified globally, or as defaults
for all monitors in a particular module, or on a monitor by monitor basis
within a module.
_Global_ - specify them as defaults for the 'monitor' module. Example:
// production.js - Configurations for the production deployment
module.exports = {
monitor: {
defaults: {
eventLogger: null,
maxLogSize: 4196
}
},
...
}
The above configuration turns off logging for the event logger, and sets the
maxLogSize parameter for all monitors (unless overridden).
_Module Defaults_ - specify them as the 'defaults' monitor for your module.
Example:
// production.js - Configurations for the production deployment
module.exports = {
Customer: {
monitor: {
defaults: {
errorLogger: require('monitor/emailLogger'),
maxLogSize: 4196
}
}
}
}
// Customer.js - Customer module
var config = require('config')('Customer');
var monitor = require('monitor')('Customer', config.monitor);
The above configuration defines module-level monitor defaults for *Customer*
_Per Monitor_ - specify per-monitor configurations like per-module (above),
only use the monitor name instead of 'default'. Example:
// production.js - Configurations for the production deployment
module.exports = {
Customer: {
monitor: {
default: {
errorLogger: null
},
'Customer insertion failure': {
errorLogger: require('monitor/emailLogger')
},
'Customer save time, ms.': {
enabled: false
}
}
}
}
// Customer.js - Customer module
var config = require('config')('Customer');
var monitor = require('monitor')('Customer', config.monitor);
The above configuration defaults error logging off for monitors within the
*Customer* module. The the *Customer insertion failure* monitor overrides this
default, and the *Customer save time, ms.* monitor is disabled.
API
---
When you run require('monitor')('my-module', config), an instance of a
ModuleMonitor class is returned. Module monitors contain individual monitors
for your module, and methods for easily adding to the monitors.
node-monitor
require('monitor')(moduleName, configuration) - This returns a Module monitor
object for the specified moduleName, creating it if necessary.
moduleName - The name of your module
configuration - An object containing these elements:
enabled - (boolean) Should the monitor be enabled? (default: true)
eventLogger - Logger[s] to use for events (default: util.log)
Can be a single logger, or an array of loggers.
errorLogger - Logger[s] to use for errors (default: util.debug)
Can be a single logger, or an array of loggers.
maxLogSize - Limit individual log output to this size (default: 10k)
require('monitor').getAllMonitors() - This returns an object containing
named ModuleMonitor objects for each module.
ModuleMonitor
Instances of this class contain a dictionary of Monitor objects for each
named monitor in your module. Convenience methods exist for easily adding to
individual named monitors.
new ModuleMonitor(moduleName, configs) - Constructor
Inputs:
moduleName - Name of your module
configs - A dictionary of monitor configurations for this module
key: monitorName, or 'defaults' for the module level defaults
value: A configuration object for the monitor, possibly including:
enabled - (boolean) Should the monitor be enabled? (default: true)
eventLogger - Logger[s] to use for events (default: util.log)
Can be a single logger, or an array of loggers.
errorLogger - Logger[s] to use for errors (default: util.debug)
Can be a single logger, or an array of loggers.
maxLogSize - Limit individual log output to this size (default: 10k)
Output:
A new ModuleMonitor class instance
ModuleMonitor.get(monitorName) - Get a named Monitor object. If the Monitor
object exists it will be returned, otherwise it will be created.
Inputs:
monitorName - Name of the monitor
Output:
An instance of the Monitor class for the specified monitorName
ModuleMonitor.getMonitors() - Get the list of Monitor objects for the module.
Inputs: (none)
Output:
An object containing all monitor objects for the module, by name.
ModuleMonitor.event(name, value, data) - Monitor an event. This adds an amount
to the specified monitor, and logs the event.
Inputs:
name - The event (monitor) name.
value - A numeric value to add to the monitor. Default = 1. If this is a
Date object, the number of milliseconds between the Date object and
Date.now() is added to the monitor.
data - An optional object to pass on to the event logger. May be specified
as the second argument in place of value (in which case value will
default to 1).
Output:
The named monitor object (for chaining)
ModuleMonitor.error(name, value) - Monitor an error that shouldn't be occurring.
This monitors and logs the specified error. It can be used for exception
processing as well as asynchronous error processing.
Input
name - The error monitor name
error - An object representing the error
callback - An optional method to call (passing the error) after logging.
Output:
monitor - This monitor (for chaining)
Monitor
new Monitor(name, moduleName, config, moduleConfig) - Constructor.
Inputs:
name: This monitor name
moduleName: Name of the containing module
config: Specific configurations for this monitor:
enabled - (boolean) Should the monitor be enabled? (default: true)
eventLogger - Logger[s] to use for events (default: console.log)
Can be a single logger, or an array of loggers.
errorLogger - Logger[s] to use for errors (default: console.debug)
Can be a single logger, or an array of loggers.
maxLogSize - Limit the log output to this size (default: 10k)
moduleConfig: Default monitor configurations for the module
enabled - (boolean) Should the monitor be enabled? (default: true)
eventLogger - Logger[s] to use for events (default: console.log)
Can be a single logger, or an array of loggers.
errorLogger - Logger[s] to use for errors (default: console.debug)
Can be a single logger, or an array of loggers.
maxLogSize - Limit the log output to this size (default: 10k)
Output:
A new Monitor object
Monitor.getHits() - Return the number of hits this monitor has recorded. Hits
are the total number of logEvent() and logError() calls.
Monitor.getTotal() - Return the total values this monitor has accumulated.
Values are specified on logEvent() calls. logError() calls accumulate a value
of one (1) for each call.
Monitor.getAvg() - Return the overall average for the monitor. The average is
the total amount as reported by getTotal() divided by the number of hits as
reported by getHits().
Monitor.getMin() - Return the smallest value added via the logEvent() or
logError() methods.
Monitor.getMax() - Return the larges value added via the logEvent() or
logError() methods.
Monitor.getFirst() - Return the first value added via the logEvent() or
logError() methods.
Monitor.getLoggers() - Return an object containing loggers added using
addLogger(). The keys are the logger IDs, and values are the logger functions.
Monitor.getLast() - Return the last value added via the logEvent() or
logError() methods.
Monitor.getConfig() - Return the actual configuration used for this monitor.
This is a mixin of the program defaults, module defaults, and monitor configs
passed in to the constructor.
Monitor.getName() - Return the name of this monitor.
Monitor.getModuleName() - Return the module name this monitor was created under.
Monitor.isEnabled() - Returns true if the monitor is enabled, false if disabled.
Monitor.enable(enabled) - This enables or disables the monitor. Disabling
prevents the monitor from accumulating values and logging messages.
Input:
enabled - (boolean) Enable or disable the monitor
Monitor.reset() - Reset the monitor accumulators to their original state.
Monitor.addLogger(loggerFunction) - Attach a logger function at runtime.
Loggers added using addLogger() will be called for all error and event logging
called on this monitor.
Input:
loggerFunction(message, value, data, monitor) - A function to run when
an event or error is logged. The function accepts:
message - A formatted message for logging
value - The numeric value of the event
data - The data object associated with the event
monitor - A reference to this monitor object for accessing monitor data
Output:
loggerId - An ID associated with this logger so it can be retrieved using
getLogger(), and removed using removeLogger().
Monitor.removeLogger(loggerId) - Remove a logger that was added using
addLogger(). This removes a logger by the ID assigned using addLogger().
Input:
loggerId - The ID returned by the addLogger function when adding the logger.
Monitor.logEvent(value, data) - Log and accumulate an event.
Input:
value - An optional numeric value to add to the monitor. Default = 1.
If this is a Date object, the number of milliseconds between the
Date object and Date.now() is added to the monitor.
data - An optional object to pass on to the event logger
Output:
monitor - This monitor (for chaining)
Monitor.logError() - Monitor an error that shouldn't be occurring.
Input
error - An object representing the error
callback - An optional method to call (passing the err) after logging.
Output:
monitor - This monitor (for chaining)
License
-------
Released under the Apache License 2.0
See `LICENSE` file.
Copyright (c) 2010 Loren West
Copyright (c) 2012 Loren West

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc