New Relic APM's PHP agent provides an API for customizing agent behavior. The PHP agent automatically instruments many popular PHP frameworks, but you can use the API to instrument activity that the agent doesn't instrument by default. Other common tasks include:
- Rename the application reporting the data.
- Name a transaction.
- Capture additional performance or business data for deeper analysis.
- Add the New Relic Browser header/footer snippet manually.
- Capture specific errors.
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.
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, New Relic APM's Applications index page, the Application Overview page, the Transactions page, etc.).
- Query your data in New Relic Insights.
- Create New Relic Alerts 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 |
Enhance the metadata of a transaction
Sometimes the code you are targeting is visible in the New Relic UI, but some details of the method are not useful. For example, the default name might not be helpful (perhaps it is causing a metric grouping issue), or you want to add custom attributes to your transactions so you can filter them in Insights.
Use these methods when you want to change how New Relic instruments a transaction that's already visible in the New Relic UI:
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 |
|
Mark a transaction as a background job |
|
Prevent a transaction from affecting your Apdex score |
Instrument asynchronous work
The only async work the PHP agent instruments is Guzzle, which is instrumented by default.
Collect 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 |
Send custom event and metric data from your app
New Relic APM 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 Insights |
Create a custom event. See Insert custom events via APM agent (PHP). |
Tag your events with metadata to filter and facet them in Insights or error analytics |
|
Report custom performance data once a minute |
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 |
Review these API calls:
For examples, see the documentation to manually instrument distributed tracing. |
Control the New Relic Browser agent
Usually the Browser agent is added automatically to your pages or deployed by copy/pasting the JavaScript snippet. For more information about these recommended methods, see Add apps to New Relic Browser.
However, you can also control the New Relic Browser agent by using APM agent API calls. For more information, see New Relic Browser and the PHP agent.