The official Opbeat client
Important: If you've been using version 0.3.x or earlier, please read our upgrade guide.
Compatibility: Make sure you read our Compatibility Guide if you use New Relic, longjohn or other modules that also captures uncaught exceptions or modifies the stacktraces.
npm install opbeat
To get started just require and initialize the Opbeat module in the top of your app's main module. Out of the box this will catch unhandled exceptions automatically.
var opbeat = require'opbeat'appId: '...'organizationId: '...'secretToken: '...';
If you want to manually send an error to Opbeat, use the
opbeatcaptureError'Ups, something broke';
When you've required the Opbeat module you can supply an optional options object to configure the client.
require'opbeat'appId: '...'organizationId: '...'secretToken: '...';
The available options are listed below, but can alternatively be set via the listed environment variables.
Your Opbeat app id. Required unless set via the
Your Opbeat orgainization id. Required unless set via the
OPBEAT_ORGANIZATION_ID environment variable.
Your secret Opbeat token. Required unless set via the
OPBEAT_SECRET_TOKEN environment variable.
A boolean specifying if errors should be collected by the Opbeat client
or not. Normally you would not want to capture errors in your
development or testing environments. If you are using the
envrionment variable, you can use this to determine the state:
var options =active: processenvNODE_ENV === 'production';
The OS hostname is automatically logged along with all errors (you can see it under the "Environment" tab on each error. If you want to overwrite this, use this option.
Set the verbosity level the Opbeat client. Note that this does not have any influence on what types of errors that are sent to Opbeat. This only controls how chatty the Opbeat client are in your logs.
Possible levels are:
Set a custom logger, e.g. bunyan:
require'opbeat'logger: require'bunyan' level: 'info';
If no custom logger is provided, Opbeat will use its built-in logger which will log to STDOUT and STDERR depending on the log level.
The logger should expose the following functions:
Note that if a custom logger is provided, the
will be ignored.
Whether or not the Opbeat client should monitor for uncaught exceptions and sent them to Opbeat automatically.
Setting it to
0 will disable stacktrace collection. Any finite integer
value will be used as the maximum number of frames to collect. Setting
Infinity means that all frames will be collected.
If you supply a filter function it will be called just before an error
is being sent to Opbeat. This will allow you to manipulate the data
being sent, for instance to always supply certain information in the
extra field. The function is synchronous and should return the
manipulated data object.
The function will be called with two arguments:
The client emits two events:
opbeaton'logged'console.log'Yay, it worked! View online at: ' + url;;opbeaton'error'console.log'Something went wrong. The error was not logged!';;opbeatcaptureError'Boom';
The client captures uncaught exceptions automatically and reports them
to Opbeat. To disable this, set the configuration option
false when initializing the Opbeat client.
You can enable capturing of uncaught exceptions later by calling the
handleUncaughtExceptions() function. This also gives you the option to
add a callback which will be called once an uncaught exception have been
sent to Opbeat.
If you don't specify a callback, the node process is terminated automatically when an uncaught exception have been captured and sent to Opbeat.
that you don't leave the process running after receiving an
uncaughtException, so if you are using the optional callback, remember
to terminate the node process:
var opbeat = require'opbeat';opbeathandleUncaughtExceptions// Do your own stuff... and then exit:processexit1;;
The callback is called after the event has been sent to the Opbeat server with the following arguments:
err- the captured exception
url- the URL of where you can find the sent error on Opbeat
You can specify an optional options argument as the 2nd argument to
.captureError(). Besides the options described in the the metedata
section, you can use the options to associate the error with
an HTTP request:
opbeatcaptureErrorerrrequest: req // an instance of http.IncomingMessage;
This will log the URL that was requested, the HTTP headers, cookies and other useful details to help you debug the error.
captureError() function can also be given an optional callback
which will be called once the error have been sent to Opbeat:
The callback is called with two arguments:
opbeatErr- set if something went wrong while trying to log the error
url- the URL of where you can find the sent error on Opbeat
Instead of an
Error object, you can log a plain text error-message:
This will also be logged as an error in Opbeat, but will not be associated with an exception.
If the message string contains state or time-specific data, Opbeat will not always recognize multiple errors as belonging to the same group, since the message text differs. To group these kind of messages, send the message as a parameterized message:
opbeatcaptureErrormessage: 'Timeout exeeded by %d seconds'params: seconds;
To ease debugging it's possible to send some extra data with each
error you send to Opbeat. The Opbeat API supports a lot of different
metadata fields, most of which are automatlically managed by the
opbeat node client. But if you wish you can supply some extra details
query. If you want to
know more about all the fields, you should take a look at the full
Opbeat API docs.
To supply any of these extra fields, use the optional options argument
Here are some examples:
// Sending some extra details about the useropbeatcaptureErrorerroruser:is_authenticated: trueid: 'unique_id'username: 'foo'email: 'email@example.com';// Sending some abitrary extra details using the `extra` fieldopbeatcaptureErrorerrorextra:some_important_metric: 'foobar';
Don't waste time initializing the Opbeat client more than once. If you need access the client in multiple files, just create an opbeat.js file somewhere in your project, initialize Opbeat in there and export it:
// opbeat.jsmoduleexports = require'opbeat'appId: '...'organizationId: '...'secretToken: '...';
The Opbeat middleware can be used as-is with either Connect or Express in the same way. Take note that in your middlewares, Opbeat must appear after your main handler to pick up any errors that may result from handling a request.
var opbeat = require'opbeat';var connect = require'connect';var app = connect;// your regular middleware:// app.use(...)// app.use(...)// your main HTTP handlerappusethrow 'Broke!';;// add Opbeat in the bottom of the middleware stackappuseopbeatmiddlewareconnect;applisten3000;
var opbeat = require'opbeat';var app = require'express'createServer;appuseopbeatmiddlewareexpress;appget'/'throw 'Broke!';;applisten3000;
must be added to the middleware stack before any other error
handling middlewares or there's a chance that the error will never get
Though Opbeat provides other means of handling release tracking, you can also use this client to do the same.
trackRelease() function with the optional options and
cwd- An optional path on the filesystem where the git repo can be found (defaults to the current working directory)
rev- An optional full git revision (will try to guess the
revbased on the
machine-completedis specified, the
machineattribute must be present
branch- Optional git branch (will try to guess the
revbased on the
machine- Optional hostname of the server that was updated. Required if
Will be called when the release has been tracked. Note that the
callback will not be called upon errors. Listen instead for the
The module is tested against Node.js v0.10 and above. Previous versions of Node.js is not supported.
All credit for the original work go out to the original contributors and the main author Matt Robenolt.