---
title: newrelic_add_custom_tracer (PHP agent API)
source: https://docs.newrelic.com/docs/apm/agents/php-agent/php-agent-api/newrelic_add_custom_tracer
---

## Syntax

```php
newrelic_add_custom_tracer(string $function_name)
```

Specify functions or methods for the agent to instrument with custom instrumentation.

## Requirements

Compatible with all agent versions.

## Description

Specify functions or methods for the agent to target for [custom instrumentation](https://docs.newrelic.com/docs/agents/php-agent/features/php-custom-instrumentation). This is the API equivalent of the `newrelic.transaction_tracer.custom` setting.

You cannot apply custom tracing to internal PHP functions.

## Parameters

| Parameter                 | Description                                                                                                                                                                                                                                                                                                                                      |
| ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `$function_name` _string_ | Required. The name can be formatted either as `function_name` for procedural functions, or as `"ClassName::method"` for methods. Both static and instance methods will be instrumented if the method syntax is used, and the class name must be fully qualified: it must include the full namespace if the class was defined within a namespace. |

## Return values

Returns `true` if the tracer was added successfully.

## Examples

### Instrument a function [#function-example]

```php
function example_function() {
    if (extension_loaded('newrelic')) { // Ensure PHP agent is available
        newrelic_add_custom_tracer("example_function");
    }
}
```

### Instrument a method within a class [#class-exmaple]

```php
class ExampleClass {
    function example_method() {
        if (extension_loaded('newrelic')) { // Ensure PHP agent is available
            newrelic_add_custom_tracer("ExampleClass::example_method");
        }
    }
}
```

### Instrument a method within a namespaced class [#class-example]

```php
namespace Foo\Bar;

class ExampleClass {
    function example_method() {
        if (extension_loaded('newrelic')) { // Ensure PHP agent is available
            newrelic_add_custom_tracer("Foo\\Bar\\ExampleClass::example_method");
        }
    }
}
```

Alternatively, on PHP 5.5 or later, the `::class` syntax can be used instead:

```php
namespace Foo\Bar {
    class ExampleClass {
        function example_method() {
            // ...
        }
    }
}

namespace {
    use Foo\Bar;

    if (extension_loaded('newrelic')) { // Ensure PHP agent is available
        newrelic_add_custom_tracer(Bar::class . "::example_method");
    }
}
```
