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

@backtrace-labs/nestjs

Package Overview
Dependencies
Maintainers
7
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@backtrace-labs/nestjs

Backtrace-JavaScript NestJS integration

  • 0.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
7
Created
Source

Backtrace NestJS SDK

Backtrace captures and reports handled and unhandled exceptions in your production software so you can manage application quality through the complete product lifecycle.

The @backtrace-labs/node SDK connects your JavaScript application to Backtrace. The basic integration is quick and easy, after which you can explore the rich set of Backtrace features.

Table of Contents

  1. Basic Integration - Reporting your first errors
  2. Error Reporting Features
  3. Advanced SDK Features

Basic Integration

Install the package

$ npm install @backtrace-labs/nestjs

Integrate the SDK

Add the following code to your application before all other scripts to report NestJS errors to Backtrace.

// Import the BacktraceClient from @backtrace-labs/nestjs
import { BacktraceClient, BacktraceConfiguration, BacktraceModule } from '@backtrace-labs/nestjs';

// Configure client options
const options: BacktraceConfiguration = {
    // Submission url
    // <universe> is the subdomain of your Backtrace instance (<universe>.backtrace.io)
    // <token> can be found in Project Settings/Submission tokens
    url: 'https://submit.backtrace.io/<universe>/<token>/json',
};

// Initialize the client with the options
BacktraceClient.initialize(options);

// By default, Backtrace will send an error for Uncaught Exceptions and Unhandled Promise Rejections
// For capturing NestJS errors, see "Add a Backtrace error interceptor" in this README

// Register BacktraceModule in your base module
@Module({
    imports: [BacktraceModule],
    controllers: [AppController],
})
class AppModule {}

@Controller()
class AppController {
    // Inject BacktraceClient into your services or controllers
    constructor(private readonly _client: BacktraceClient) {}

    @Post()
    public endpoint() {
        // Manually send an error
        this._cclient.send(new Error('Something broke!'));
    }
}

Upload source maps

Client-side error reports are based on minified code. Upload source maps and source code to resolve minified code to your original source identifiers.

(Source Map feature documentation)

Add a Backtrace error interceptor

While processing requests, NestJS will handle all exceptions thrown by controllers using the exception filters. This means that the exceptions will not be unhandled, and thus not captured by Backtrace. To capture these errors, you can use the BacktraceInterceptor class.

To add the interceptor globally, you can register it as APP_INTERCEPTOR:

import { Module } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { BacktraceModule, BacktraceInterceptor } from '@backtrace-labs/nestjs';

@Module({
    imports: [BacktraceModule],
    providers: [
        {
            provide: APP_INTERCEPTOR,
            useValue: new BacktraceInterceptor(),
        },
    ],
    controllers: [CatsController],
})
export class AppModule {}

Or, use app.useGlobalInterceptors:

const app = await NestFactory.create(AppModule);
app.useGlobalInterceptors(new BacktraceInterceptor());

To use it on a controller, use UseInterceptors decorator:

@UseInterceptors(new BacktraceInterceptor())
export class CatsController {}

For more information, consult NestJS documentation.

Configuring the interceptor

By default, the interceptor will include:

  • all errors that are an instance of Error,

and exclude:

  • all HttpException errors that have status < 500.

To include or exclude specific error types, pass options to BacktraceInterceptor:

new BacktraceInterceptor({
    includeExceptionTypes: [Error],
    excludeExceptionTypes: (error) => error instanceof HttpException && error.getStatus() < 500,
});

As shown in the example above, includeExceptionTypes and excludeExceptionTypes accept either an array of error types, or a function that can return a boolean. The array types will match using instanceof. The function will have the thrown error passed as the first parameter.

Error Reporting Features

Attributes

Custom attributes are key-value pairs that can be added to your error reports. They are used in report aggregation, sorting and filtering, can provide better contextual data for an error, and much more. They are foundational to many of the advanced Backtrace features detailed in Error Reporting documentation. By default attributes such as application name and version are populated automatically based on your package.json information. If Backtrace cannot find them, you need to provide them manually via userAttributes attributes.

There are several places where attributes can be added, modified or deleted.

Attach attributes object to BacktraceClient

It is possible to include an attributes object during BacktraceClient initialization. This list of attributes will be included with every error report, referred to as global attributes.

// Create an attributes object that can be modified throughout runtime
const attributes: Record<string, unknown> = {
    release: 'PROD',
};

// BacktraceClientOptions
const options: BacktraceConfiguration = {
    url: 'https://submit.backtrace.io/<universe>/<token>/json',

    // Attach the attributes object
    userAttributes: attributes,
};

// Initialize the client
const client = BacktraceClient.initialize(options);

You can also include attributes that will be resolved when creating a report:

// BacktraceClientOptions
const options: BacktraceConfiguration = {
    url: 'https://submit.backtrace.io/<universe>/<token>/json',

    // Attach the attributes object
    userAttributes: () => ({
        attribute: getAttributeValue(),
    }),
};

// Initialize the client
const client = BacktraceClient.initialize(options);
Add attributes during application runtime

Global attributes can be set during the runtime once specific data has be loaded (e.g. a user has logged in).

const client = BacktraceClient.initialize(options);
...

client.addAttribute({
    "clientID": "de6faf4d-d5b5-486c-9789-318f58a14476"
})

You can also add attributes that will be resolved when creating a report:

const client = BacktraceClient.initialize(options);
...

client.addAttribute(() => ({
    "clientID": resolveCurrentClientId()
}))
Add attributes to an error report

The attributes list of a BacktraceReport object can be directly modified.

const report: BacktraceReport = new BacktraceReport('My error message', { myReportKey: 'myValue' });
report.attributes['myReportKey'] = 'New value';

File Attachments

Files can be attached to error reports. This can be done when initalizing the BacktraceClient, updating the BacktraceClient, or dynamically for specific reports. When including attachments in BacktraceClient, all files will be uploaded with each report.

// Import attachment types from @backtrace-labs/nestjs
import { BacktraceStringAttachment, BacktraceUint8ArrayAttachment  } from "@backtrace-labs/nestjs";

// BacktraceStringAttachment should be used for text object like a log file, for example
const stringAttachment = new BacktraceStringAttachment("logfile.txt", "This is the start of my log")

// Buffer attachment is an attachment type dedicated to store buffer data
const bufferAttachment = new BacktraceBufferAttachment('buffer-attachment.txt', Buffer.from('sample'));

// File attachment is an attachment type dedicated for streaming files
const fileAttachment = new BacktraceFileAttachment('/path/to/sample/file');

// BacktraceClientOptions
const options = {
    url: "https://submit.backtrace.io/<universe>/<token>/json",

    // Attach the files to all reports
    attachments: [path.join('/path/to/attachment'), stringAttachment],
}

const client = BacktraceClient.initialize(options);

// Later decide to add an attachment to all reports
client.addAttachment(bufferAttachment)

// After catching an exception and generating a report
try {
    throw new Error("Caught exception!")
} catch (error) {
    const report = const report = new BacktraceReport(error, {}, [fileAttachment])
    client.send(report);
}

Breadcrumbs

Breadcrumbs are snippets of chronological data tracing runtime events. This SDK records a number of events by default, and manual breadcrumbs can also be added.

(Breadcrumbs feature documentation)

Breadcrumbs Configuration
Option NameTypeDescriptionDefaultRequired?
enableBooleanDetermines if the breadcrumbs support is enabled. By default the value is set to true.true
  • - [ ]
logLevelBreadcrumbLogLevelSpecifies which log level severity to include. By default all logs are included.All Logs
  • - [ ]
eventTypeBreadcrumbTypeSpecifies which breadcrumb type to include. By default all types are included.All Types
  • - [ ]
maximumBreadcrumbsNumberSpecifies maximum number of breadcrumbs stored by the library. By default, only 100 breadcrumbs will be stored.100
  • - [ ]
intercept(breadcrumb: RawBreadcrumb) => RawBreadcrumb | undefined;Inspects breadcrumb and allows to modify it. If the undefined value is being returned from the method, no breadcrumb will be added to the breadcrumb storage.All Breadcrumbs
  • - [ ]
import { BacktraceClient, BacktraceConfiguration } from '@backtrace-labs/nestjs';

// BacktraceClientOptions
const options: BacktraceConfiguration = {
    // ignoring all but breadcrumbs config for simplicity
    breadcrumbs: {
        // breadcrumbs configuration
    },
};

// Initialize the client
const client = BacktraceClient.initialize(options);
Default Breadcrumbs
TypeDescription
ConsoleAdds a breadcrumb every time console log is being used by the developer.
Intercepting Breadcrumbs

If PII or other information needs to be filtered from a breadcrumb, you can use the intercept function to skip or filter out the sensitive information. Any RawBreadcrumb returned will be used for the breadcrumb. If undefined is returned, no breadcrumb will be added.

Manual Breadcrumbs

In addition to all of the default breadcrumbs that are automatically collected, you can also manually add breadcrumbs of your own.

client.breadcrumbs?.info('This is a manual breadcrumb.', {
    customAttr: 'wow!',
});

Application Stability Metrics

The Backtrace NestJS SDK has the ability to send usage Metrics to be viewable in the Backtrace UI.

(Stability Metrics feature documentation)

Metrics Configuration
Option NameTypeDescriptionDefaultRequired?
metricsSubmissionUrlStringMetrics server hostname. By default the value is set to https://events.backtrace.io.https://events.backtrace.io
  • - [ ]
enableBooleanDetermines if the metrics support is enabled. By default the value is set to true.true
  • - [ ]
autoSendIntervalNumberIndicates how often crash free metrics are sent to Backtrace. The interval is a value in ms. By default, session events are sent on application startup/finish, and every 30 minutes while the application is running. If the value is set to 0. The auto send mode is disabled. In this situation the application needs to maintain send mode manually.On application startup/finish
  • - [ ]
sizeNumberIndicates how many events the metrics storage can store before auto submission.50
  • - [ ]
Metrics Usage
// metrics will be undefined if not enabled
client.metrics?.send();

Offline database support

The Backtrace NestJS SDK can cache generated reports and crashes to local disk before sending them to Backtrace. This is recommended; in certain configurations NestJS applications can crash before the SDK finishes submitting data, and under slow internet conditions your application might wait in a closing window until the HTTP submission finishes. In such an event occurs cached reports will be sent on next application launch.

With offline database support you can:

  • cache your reports when the user doesn't have Internet connection or the service is unavailable,
  • capture crashes,
  • manually decide when to send them or not.

By default the offline database support is disabled. To enable it, please add "enable: true" and the path to the directory where Backtrace can store crash data.

const client = BacktraceClient.initialize({
    // ignoring all but database config for simplicity
    database: {
        enable: true,
        path: '/path/to/the/database/directory',
        captureNativeCrashes: true,
    },
});

// manually send and keep the data on connection issue
client.database.send();
// manually send and remove all data no matter if received success or not.
client.database.flush();
Database Configuration
Option NameTypeDescriptionDefaultRequired?
enabledBooleanEnable/disable offline database support.false
  • - [x]
pathStringLocal storage path for crash data.-
  • - [x]
createDatabaseDirectoryBooleanAllow the SDK to create the offline database directory..true
autoSendBooleanSends reports to the server based on the retry settings. If the value is set to 'false', you can use the Flush or Send methods as an alternative.true
maximumNumberOfRecordsNumberThe maximum number of reports stored in the offline database. When the limit is reached, the oldest reports are removed. If the value is equal to '0', then no limit is set.8
retryIntervalNumberThe amount of time (in ms) to wait between retries if the database is unable to send a report.60 000
maximumRetriesNumberThe maximum number of retries to attempt if the database is unable to send a report.3
captureNativeCrashesBooleanCapture and symbolicate stack traces for native crashes if the runtime supports this. A crash report is generated, stored locally, and uploaded upon next start.false

Native crash support

The Backtrace NestJS SDK can capture native crashes generated by a NestJS application such as Assert/OOM crashes. In order to collect them, the SDK uses the NestJS's process.report API. After setting up the native crash support, your process.report settings may be overridden and your crash data might be created in the database directory.

Database records sent in the next session may not have some information about the crashing session such as attributes or breadcrumbs. To reduce database record size, attachment support was limited only to file attachments.

Manual database operations

Database support is available in the client options with the BacktraceDatabase object. You can use it to manually operate on database records. Options are detailed in BacktraceDatabase Methods.

Advanced SDK Features

Manually send an error

There are several ways to send an error to Backtrace. For more details on the definition of client.send() see Methods below.

// send as a string
await client.send('This is a string!');

// send as an Error
await client.send(new Error('This is an Error!'));

// as a BacktraceReport (string)
await client.send(new BacktraceReport('This is a report with a string!'));

// as a BacktraceReport (Error)
await client.send(new BacktraceReport(new Error('This is a report with a string!')));

BacktraceClient

BacktraceClient is the main SDK class. Error monitoring starts when this object is instantiated, and it will compose and send reports for unhandled errors and unhandled promise rejections. It can also be used to manually send reports from exceptions and rejection handlers.

BacktraceClientOptions

The following options are available for the BacktraceClientOptions passed when initializing the BacktraceClient.

Option NameTypeDescriptionDefaultRequired?
urlStringSubmission URL to send errors to
  • - [x]
tokenStringThe submission token for error injestion. This is required only if submitting directly to a Backtrace URL. (uncommon)
  • - [ ]
userAttributesDictionaryAdditional attributes that can be filtered and aggregated against in the Backtrace UI.
  • - [ ]
attachmentsBacktraceAttachment[]Additional files to be sent with error reports. See File Attachments
  • - [ ]
beforeSend(data: BacktraceData) => BacktraceData | undefinedTriggers an event every time an exception in the managed environment occurs, which allows you to skip the report (by returning a null value) or to modify data that library collected before sending the report. You can use the BeforeSend event to extend attributes or JSON object data based on data the application has at the time of exception. See BeforeSend
  • - [ ]
skipReport(report: BacktraceReport) => booleanIf you want to ignore specific types of error reports, we recommend that you use the skipReport callback. By using it, based on the data generated in the report, you can decide to filter the report, or send it to Backtrace.
  • - [ ]
captureUnhandledErrorsBooleanEnable unhandled errorstrue
  • - [ ]
captureUnhandledPromiseRejectionsBooleanEnable unhandled promise rejectiontrue
  • - [ ]
timeoutIntegerHow long to wait in ms before timing out the connection15000
  • - [ ]
ignoreSslCertificateBooleanIgnore SSL Certificate errorsfalse
  • - [ ]
rateLimitIntegerLimits the number of reports the client will send per minute. If set to '0', there is no limit. If set to a value greater than '0' and the value is reached, the client will not send any reports until the next minute.0
  • - [ ]
metricsBacktraceMetricsOptionsSee Backtrace Stability Metrics
  • - [ ]
breadcrumbsBacktraceBreadcrumbsSettingsSee Backtrace Breadcrumbs
  • - [ ]
databaseBacktraceDatabaseSettingsSee Backtrace Database
  • - [ ]
BacktraceClient Methods
NameReturn TypeDescription
addAttribute(attributes: Record<string, unknown>)voidAdd attributes to the BacktraceClient reports
addAttachment(attachment: BacktraceAttachment)voidAdd an attachment to the BacktraceClient reports
initialize(options: BacktraceClientOptions)BacktraceClientInitializes a new BacktraceClient (returns the same instance on subsequent calls)
builder(options: BacktraceClientOptions).build()BacktraceClient(Advanced) Sets up a new BacktraceClient for reporting
send(data: BacktraceReport | Error | string, reportAttributes: Record<string, unknown> = {}, reportAttachments: BacktraceAttachment[] = [])PromiseAsynchronously sends error data to Backtrace
BacktraceDatabase Methods
NameReturn TypeDescription
enabledBooleanDetermines if the database is enabled
start()BooleanStarts the database
add(backtraceData: Backtracedata, attachments: BacktraceAttachment[])BacktraceDatabaseRecord | undefinedAdds manually a data object to the database. If the database is not available or the record cannot be stored on the hard drive, the add method can return undefined.
get()BacktraceDatabaseRecord[]Returns all records stored in the database
count()NumberReturns the number of records stored in the database
dispose()voidDisables the database integration
remove(record: BacktraceDatabaseRecord)voidRemoves the record from the database
flush()PromiseSends all records to Backtrace and remove them no matter if the submission was successful or not
send()PromiseSends all records to Backtrace. If the submission process fails, the retry information is being increased by the records are not removed from the database.
disposevoidDisposes the client

BacktraceReport

A Backtrace Report is the format that ultimately gets sent to Backtrace. Its structure can be found in BacktraceReport.ts.

Keywords

FAQs

Package last updated on 16 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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