Guide to using the PHP agent API

New Relic's PHP agent provides an API for customizing agent behavior. The agent automatically instruments many popular PHP frameworks, but you can use the PHP API to instrument activity that the agent doesn't already instrument by default.

This guide describes PHP API calls for several common tasks; for example:

You can also set many of these options per directory or in newrelic.ini. If setting via API, the API takes precedence over the other methods.

Notice or ignore errors

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

If you want to... Do this...

Report an error the agent does not report automatically

See newrelic_notice_error().

Ignore an error

Edit error_collector.ignore_exceptions or error_collector.ignore_errors in thenewrelic.ini file.

Enhance the metadata of a transaction

Sometimes the code you target 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_name_transaction().

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

Use custom attributes.

For custom attributes which will be added to transactions and spans (if enabled), see newrelic_add_custom_parameter().

For custom attributes which will be added only to spans, see newrelic_add_custom_span_parameter().

Mark a transaction as a background job

See newrelic_background_job().

Prevent a transaction from affecting your Apdex score

See newrelic_ignore_apdex().

Name reporting app

The New Relic UI organizes data by how you name your application. Each name appears as a different app. You can rename the reporting app for any part of your code to better help you:

This is especially useful if you have a multi-site application or distinct sets of application code using the same PHP installation.

Alternatively, you can set the app name at a higher level instead via the newrelic.appname config value, via Apache, or via PHP-FPM.

If you want to... Do this...

Rename the reporting app for a specific part of your code

Set the app name. See newrelic_set_appname().

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. For more information about transactions, see transaction and transaction trace.

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

Start a transaction. See newrelic_start_transaction().

Stop timing a method after its work is completed

Stop a transaction. See newrelic_end_transaction().

Prevent a transaction from reporting to New Relic

Ignore the transaction. See newrelic_ignore_transaction().

Time specific methods using segments

If a transaction is already visible in the New Relic UI, 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 methods when you want to instrument a method within an existing transaction:

If you want to... Do this...

Time a particular method

See newrelic_add_custom_tracer().

Instrument asynchronous work

The only async work the PHP agent instruments is Guzzle, which is instrumented by default.

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 Insert custom events via APM agent (PHP).

Tag your events with metadata to filter and facet them

Add custom attributes. See newrelic_add_custom_parameter().

Report custom performance data

Create a custom metric. See newrelic_custom_metric().

Instrument calls to external services

Use these methods to collect data about your app's connections to other apps or datastores:

If you want to... Do this...

Time a call to a datastore not instrumented by default

See newrelic_record_datastore_segment().

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

For PHP agent versions lower than 9.8, see these API calls:

For PHP agent versions 9.8 or higher, see these API calls:

For examples, see the documentation to manually instrument distributed tracing.

Monitor specific browser pages

You can install the browser agent by automatically adding it to your pages or by deploying it on specific pages by copying and pasting our JavaScript snippet. You can also control the browser agent by using APM agent API calls. For more information, see Browser monitoring and the PHP agent.

For more help

If you need more help, check out these support and learning resources: