• /
  • EnglishEspañol日本語한국어Português
  • 로그인지금 시작하기

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:

For more information about create custom web transactions, see the startWebTransaction example on GitHub.

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.

To time a particular method, see newrelic.startSegment.

For more information about timing, see the custom instrumentation example 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(). For more information, see the example application on GitHub.

Create a new transaction for background work

See newrelic.startBackgroundTransaction() and example application on GitHub.

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 recordMetric or incrementMetric.

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.

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 distributed tracing. For more information on manually inserting and accepting distributed tracing headers, see the example application on GitHub.

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. For more information on using the API methods to log, group, and associate errors, use the example application on GitHub.

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. For more information on using the API methods below, use the example application on GitHub.

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

For more information about using the custom instrumentation APIs, see Node.js custom instrumentation.

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.