• EnglishEspañol日本語한국어Português
  • Log inStart now

Guide to using the Node.js agent API

Our Node.js agent API allows you to extend the agent's standard functionality. You can use this API to:

  • Create custom transaction parameters
  • Report custom errors and metrics

You can also use the API for custom instrumentation. For supported frameworks, the agent instruments most activity automatically. Custom instrumentation lets you extend that monitoring to frameworks without default instrumentation.

Other resources:

Requirements

To use the Node.js agent API, make sure you have the latest Node.js agent release. In addition, see:

Instrument missing sections of your code with transactions

To instrument your app, New Relic separates each path through your code into its own transaction. New Relic times (or "instruments") the parent method in these transactions to measure your app's overall performance, and collects transaction traces from long-running transactions for additional detail.

Use these methods when New Relic is not instrumenting a particular part of your code at all:

If you want to...

Do this...

Start timing a method New Relic is not instrumenting automatically

Create a new transaction. See newrelic.startWebTransaction().

Stop timing a method after its work is completed

Use either of these options:

Prevent a transaction from reporting to New Relic

Ignore the transaction using any of these options:

Time specific methods using segments

If a transaction is already visible in New Relic, but you don't have enough data about a particular method that was called during that transaction, you can create segments to time those individual methods in greater detail. For example, you might want to time a particularly critical method with complex logic.

Use this method when you want to instrument a method within an existing transaction:

If you want to...

Do this...

Time a particular method

See newrelic.startSegment().

For more information about timing, see the instrumentation tutorial on GitHub.

Enhance the metadata of a transaction

Sometimes the code you are targeting is visible in New Relic, but some details of the method are not useful. For example:

Use these methods when you want to change how New Relic instruments a transaction that's already visible in New Relic:

If you want to...

Do this...

Change the name of a transaction

See newrelic.setTransactionName and rules.name.

Add metadata (such as your customer’s account name or subscription level) to your transactions

Use custom attributes. (Custom attribute collection is enabled by default in the Node.js agent.) See newrelic.addCustomAttribute() and newrelic.addCustomAttributes().

Create a new transaction for background work

See newrelic.startBackgroundTransaction().

Create a new web transaction

Use newrelic.startWebTransaction().

Prevent a transaction from affecting your Apdex score

See Rules for naming and ignoring requests, including the ignoring rules example.

Record other performance data, such as timing or computer resource data

Use the custom metrics API.

See related logs

The agent by default sends logs directly within the context of your application's errors and traces. For more information about correlating log data with other telemetry data and supported frameworks, see our logs in context docs.

If you're using a logging mechanism that's not instrumented by New Relic, you may instead directly use this API call to record and forward your logs in context:

An older alternative method is to use your own log forwarder instead of letting the agent do the forwarding for you. In this case, you'll need to annotate your logs with the proper context before you forward them. Use these API calls:

Instrument asynchronous work

For supported frameworks and supported Node.js versions, New Relic's Node.js agent usually correctly instruments async work. However, if your app uses another framework, or if the default async instrumentation is inaccurate, you can explicitly track async work.

If you want to...

Do this...

Trace an async method that New Relic is already instrumenting

See newrelic.startSegment.

Trace an async method that New Relic is not instrumenting

See newrelic.startSegment.

Trace a transaction that got lost

See newrelic.startSegment. You can also create your own custom instrumentation for libraries that are losing your transactions.

Trace a lost transaction state

A common issue is the loss of transaction state while using uninstrumented libraries. For more information, see the transaction preservation tutorial on GitHub.

Instrument calls to external services

Once the request naming API loads, New Relic's Node.js agent can automatically identify external service calls. You can also use these methods to collect data about your app's connections to other apps or databases:

If you want to...

Do this...

Time a call to an external resource (such as an external service, database server, or message queue)

Use any of these as appropriate:

Connect activity to another app instrumented by a New Relic agent

Use cross application tracing.

Important

Cross application tracing has been deprecated in favor of Distributed tracing and will be removed in a future agent version.

See the path that a request takes as it travels through a distributed system.

Distributed tracing

Collect or ignore errors

Usually the agent detects errors automatically. However, you can manually mark an error with the agent. You can also mark errors as ignored.

If you want to...

Do this...

Report an error the agent does not report automatically

See newrelic.noticeError().

Group errors by name, using a custom filter function you define

See newrelic.setErrorGroupCallback().

Associate errors with users

See newrelic.setUserId().

Send custom event and metric data from your app

New Relic includes a number of ways to record arbitrary custom data. For an explanation of New Relic data types, see Data collection.

If you want to...

Do this...

Send data about an event so you can analyze it in New Relic

Create a custom event. See newrelic.recordCustomEvent().

Tag your events with metadata to filter and facet them

Add custom attributes if needed. (Custom attribute collection is enabled by default in the Node.js agent.) See newrelic.addCustomAttribute() and newrelic.addCustomAttributes().

Report custom performance data

Create a custom metric. See newrelic.recordMetric() and newrelic.incrementMetric(). To view the data, use metrics and events.

Control the browser agent

Usually the agent is added automatically to your pages or deployed by copy/pasting the JavaScript snippet. For more information about these recommended methods, see Add browser apps to New Relic.

You can also control the browser agent via APM agent API calls. For more information, see Browser monitoring and the Node.js agent.

Extend custom instrumentation

The newrelic.instrument() provides additional flexibility for custom instrumentation. For more information, including tutorials and examples, see the shim documentation on GitHub.

To add custom instrumentation in ES module applications, please refer to our ES module documentation or example application in GitHub.

Copyright © 2024 New Relic Inc.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.