Syntax
newrelic_record_datastore_segment(callable $func, array $parameters)
Records a datastore segment.
Requirements
Agent version 7.5.0.199 or higher.
Description
Records a datastore segment. Datastore segments appear in the Breakdown table and Databases tab of the Transactions page in the New Relic UI.
This function allows an unsupported datastore to be instrumented in the same way as the PHP agent automatically instruments its supported datastores.
Parameters
Parameter | Description |
---|---|
callable | Required. The function that should be timed to create the datastore segment. |
array | Required. An associative array of parameters describing the datastore call. |
The supported keys in the $parameters
array are as follows:
Key | Description |
---|---|
string | Required. The name of the datastore product being used: for example, |
string | Optional. The table or collection being used or queried against. |
string | Optional. The operation being performed: for example, While operations may be specified with any case, New Relic suggests using lowercase to better line up with the operation names used by the PHP agent's automated datastore instrumentation. |
string | Optional. The datastore host name. |
string | Optional. The port or socket used to connect to the datastore. |
string | Optional. The database name or number in use. |
string | Optional. The query that was sent to the server. For security reasons, this value is only used if you set |
string | Optional. The name of the ORM in use (for example: |
string | Optional. The input query that was provided to the ORM. For security reasons, and as with the |
重要
The string arguments used in the $parameters
array should not contain the special character '/'.
Return values
The return value of $callback
is returned. If an error occurs, false
is returned, and an error at the E_WARNING
level will be triggered.
Examples
Instrumenting an unsupported NoSQL datastore
To instrument a hypothetical NoSQL datastore called Bucket
that exposes a get
method, the following code would create a datastore metric that would show up in the New Relic UI:
$bucket = new Bucket($host, $port);$id = 12345;
$value = newrelic_record_datastore_segment(function () use ($bucket, $id) { return $bucket->get($id);}, array( 'product' => 'Bucket', 'collection' => $id, 'operation' => 'get', 'host' => $host, 'portPathOrId' => $port,));
Instrumenting an unsupported SQL client library
To record a query for an unsupported MySQL client library with the query obfuscated and attached to the segment:
$sql = 'SELECT * FROM table';$stmt = $db->prepare($sql);
$result = newrelic_record_datastore_segment(function () use ($stmt) { return $stmt->execute();}, array( 'product' => 'MySQL', 'collection' => 'table', 'operation' => 'select', 'host' => $host, 'portPathOrId' => $port, 'databaseName' => 'dbname', 'query' => $query,));