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:
- Notice or ignore specific errors.
- Name a transaction.
- Rename the application reporting the data.
Sugerencia
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 | |
Ignore an error | Edit |
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:
- The default name is not helpful, or it is causing a metric grouping issue.
- You want to add custom attributes to your transactions or spans so you can filter them with NRQL queries.
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 | |
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 For custom attributes which will be added only to spans, see |
Mark a transaction as a background job | |
Prevent a transaction from affecting your Apdex score |
See related logs
To see logs directly within the context of your application's errors and traces, use these API calls to annotate your logs:
For more information about correlating log data with other telemetry data, see our logs in context documentation.
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:
- Organize and analyze your data on New Relic UI pages (for example, APM's Applications index page, the Summary page, the Transactions page, etc.).
- Query your data.
- Create alert policies.
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 |
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 |
Stop timing a method after its work is completed | Stop a transaction. See |
Prevent a transaction from reporting to New Relic | Ignore the transaction. See |
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 |
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 | |
Report custom performance data | Create a custom metric. See |
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 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:
|
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 agent by using agent API calls. For more information, see Browser monitoring and the PHP agent.