Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

opbeat-ionic

opbeat36BSD1.1.3

The unofficial Opbeat client for Ionic

opbeat, log, logging, error, errors, exceptions, monitor, monitoring, alerts, performance, ops, devops, deployment, deploying, deploy, stacktrace

readme

Opbeat

Build Status

Log errors and stacktraces in Opbeat from within your Node.js applications. Includes middleware support for Connect and Express.

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.

Installation

npm install opbeat

Basic Usage

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 captureError() function:

opbeat.captureError(new Error('Ups, something broke'));

Configuration

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.

appId

  • Type: String
  • Env: OPBEAT_APP_ID

Your Opbeat app id. Required unless set via the OPBEAT_APP_ID environment variable.

organizationId

  • Type: String
  • Env: OPBEAT_ORGANIZATION_ID

Your Opbeat orgainization id. Required unless set via the OPBEAT_ORGANIZATION_ID environment variable.

secretToken

  • Type: String
  • Env: OPBEAT_SECRET_TOKEN

Your secret Opbeat token. Required unless set via the OPBEAT_SECRET_TOKEN environment variable.

active

  • Type: Boolean
  • Default: true
  • Env: OPBEAT_ACTIVE

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 NODE_ENV envrionment variable, you can use this to determine the state:

var options = {
  active: process.env.NODE_ENV === 'production'
};

hostname

  • Type: String
  • Default: OS hostname
  • Env: OPBEAT_HOSTNAME

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.

clientLogLevel

  • Type: String
  • Default: 'info'
  • Env: OPBEAT_CLIENT_LOG_LEVEL

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: debug, info, warn, error and fatal.

logger

  • Type: object

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: debug, info, warn, error and fatal.

Note that if a custom logger is provided, the clientLogLevel option will be ignored.

captureExceptions

  • Type: Boolean
  • Default: true
  • Env: OPBEAT_CAPTURE_EXCEPTIONS

Whether or not the Opbeat client should monitor for uncaught exceptions and sent them to Opbeat automatically.

stackTraceLimit

  • Type: Number
  • Default: Infinity
  • Env: OPBEAT_STACK_TRACE_LIMIT

Setting it to 0 will disable stacktrace collection. Any finite integer value will be used as the maximum number of frames to collect. Setting it to Infinity means that all frames will be collected.

Events

The client emits two events: logged and error.

opbeat.on('logged', function (url) {
  console.log('Yay, it worked! View online at: ' + url);
});

opbeat.on('error', function (err) {
  console.log('Something went wrong. The error was not logged!');
});

opbeat.captureError('Boom');

Uncaught exceptions

The client captures uncaught exceptions automatically and reports them to Opbeat. To disable this, set the configuration option captureExceptions to 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.

opbeat.handleUncaughtExceptions([callback]);

If you don't specify a callback, the node process is terminated automatically when an uncaught exception have been captured and sent to Opbeat.

It is recommended 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')();

opbeat.handleUncaughtExceptions(function (err, url) {
  // Do your own stuff... and then exit:
  process.exit(1);
});

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

Advanced usage

HTTP requests

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:

opbeat.captureError(err, {
  request: 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.

Callback

The captureError() function can also be given an optional callback which will be called once the error have been sent to Opbeat:

opbeat.captureError(error, function (opbeatErr, url) {
  // ...
});

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

Non-exceptions

Instead of an Error object, you can log a plain text error-message:

opbeat.captureError('Something happened!');

This will also be logged as an error in Opbeat, but will not be associated with an exception.

Parameterized messages

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:

opbeat.captureError({
  message: 'Timeout exeeded by %d seconds',
  params: [seconds]
});

Metadata

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 using client_supplied_id, extra, user or 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 when calling opbeat.captureError().

Here are some examples:

// Sending some extra details about the user
opbeat.captureError(error, {
  user: {
    is_authenticated: true,
    id: 'unique_id',
    username: 'foo',
    email: 'foo@example.com'
  }
});

// Sending some abitrary extra details using the `extra` field
opbeat.captureError(error, {
  extra: {
    some_important_metric: 'foobar'
  }
});

Singleton access

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.js
module.exports = require('opbeat')({
  appId: '...',
  organizationId: '...',
  secretToken: '...'
});

Integrations

Connect/Express middleware

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.

Connect

var opbeat = require('opbeat')();
var connect = require('connect');

var app = connect();

// your regular middleware:
// app.use(...)
// app.use(...)

// your main HTTP handler
app.use(function (req, res, next) {
  throw new Error('Broke!');
});

// add Opbeat in the bottom of the middleware stack
app.use(opbeat.middleware.connect());

app.listen(3000);

Express

var opbeat = require('opbeat')();
var app = require('express').createServer();

app.use(opbeat.middleware.express());
app.get('/', function mainHandler(req, res) {
  throw new Error('Broke!');
});
app.listen(3000);

Note: opbeat.middleware.express or opbeat.middleware.connect must be added to the middleware stack before any other error handling middlewares or there's a chance that the error will never get to Opbeat.

Release tracking

Though Opbeat provides other means of handling release tracking, you can also use this client to do the same.

Use the trackDeployment() function with the optional options and callback arguments:

opbeat.trackDeployment(options, callback);

Options:

  • path - 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 rev based on the path)
  • status - completed (default) or machine-completed. If machine-completed is specified, the hostname attribute must be present
  • branch - Optional git branch (will try to guess the rev based on the path)
  • hostname - Optional hostname of the server that was updated. Required if status=machine-completed

Callback:

Will be called when the release has been tracked. Note that the callback will not be called upon errors. Listen instead for the error events.

Compatibility

The module is tested against Node.js v0.10 and above. Previous versions of Node.js is not supported.

Credit

All credit for the original work go out to the original contributors and the main author Matt Robenolt.

License

BSD

changelog

1.1.1 - 2015/01/14

  • Fix: Ensure invalid objects are logged in a more human readable way

1.1.0 - 2015/01/08

  • New: Log if an error is uncaught under the "Extra" tab
  • New: Support custom loggers using the new options.logger option
  • Internal improvements

1.0.8 - 2015/01/02

  • Bug fix: Fix connect/express middleware

1.0.7 - 2014/12/11

  • Bug fix: Exit process even if Opbeat cannot be reached
  • Improve tests

1.0.6 - 2014/12/4

  • Fix issue with logging json HTTP requests if using req.json
  • Rename internal options.apiHost to options._apiHost (hopefully you did not use this)

1.0.5 - 2014/11/8

  • Log missing line numbers as line zero

1.0.4 - 2014/11/8

  • Bug fix: Ensure the agent doesn't fail on circular dependencies

1.0.3 - 2014/11/8

  • API update: The new version of the Opbeat API expects the stack frames in reverse order

1.0.2 - 2014/10/30

  • Big fix: Ensure emitted errors after an uncaught exception doesn't throw

1.0.1 - 2014/10/23

  • Minor bugfixes and improvements

1.0.0 - 2014/9/25

  • Remove createClient() function
  • Replace options.env with options.active
  • Rename options.handleExceptions to options.captureExceptions
  • Rename options.app_id to options.appId
  • Rename options.organization_id to options.organizationId
  • Rename options.secret_token to options.secretToken
  • Add deployment tracking support
  • Merge captureMessage, captureError and captureRequestError into one function
  • Remove support for overriding the Opbeat http API port number
  • Automatically log custom properties on the Error object
  • Log HTTP User-Agent header if present
  • Log name of Node.js module where error occured
  • Log request.json if present
  • Log whether http requests are secure or not
  • Log remote IP
  • Allow options.stackTraceLimit to be falsy
  • Remove client.version property
  • Remove event connectionError (use error event instead)
  • Control log level of client with options.clientLogLevel (replaces options.silent)
  • Allow handleUncaughtExceptions to be called multiple times
  • Allow the severity level of exceptions to be set in options
  • Allow all options to be set via environment variables
  • Parse the Opbeat URL to the captureUncaughtExceptions callback
  • Don't log stack-frame path as absolute
  • Only log cookies if they are present
  • Security fix: Don't shamelessly track all environment variables
  • Bug fix: Support the new Opbeat param_message API format
  • Improve HTTP message parsing
  • A lot of code cleanup

0.3.1 - 2014/4/28

  • Allow you to call client functions without having to worry about binding

0.3.0 - 2014/4/9

  • Removed support for Node.js versions below v0.10

0.2.9 - 2014/4/9

  • Internal improvements

0.2.8 - 2013/8/25

  • Bug fix: Set culprit correctly on errors sent to Opbeat

0.2.7 - 2013/6/10

  • Bug fix: The express/connect middleware now correctly uses or creates an instance of the Opbeat client

0.2.6 - 2013/6/10

  • Never published

0.2.5 - 2013/6/10

  • Some exceptions where logged twice in the local log
  • Improved opbeat error logging: If opbeat returns an error, the entire error is now written to the log
  • Bug fix: Thrown exceptions was not logged to Opbeat

0.2.4 - 2013/5/7

  • Bug fix: Request errors was printet twice in the server-logs

0.2.3 - 2013/5/2

  • Create an opbeat client once and reuse it. Now you can create a client using opbeat.createClient() and reuse it using opbeat.client
  • Added silent option. Opbeat will not output anything to STDOUT or STDERR, except configuration errors
  • Added exceptionsAreCritical option, which is on my default. This means that uncaught exceptions are logged as critical as opposed to the error level
  • Allow better grouping of messages: 21384d7c0df1ffec5b985d918cab3a91208e75e3
  • Added default event listeners, so you do not have to: 9b83e18835c2b7e24dd211b51fb38f9d820a9956
  • Bug fixing

0.2.2 - 2013/4/27

  • All output is now directed to STDERR
  • Always output result of sending something to Opbeat, even if you have disabled automatic exception handling and are providing a custom callback to handleUncaughtExceptions

0.2.1 - 2013/4/8

  • Bug fixing

0.2.0 - 2013/4/8

  • Brand new API - Not backward compatible

0.1.0 - 2013/4/2

  • Initial release. Forked raven-node and converted to Opbeat API