At the beginning of every APM trace a transaction must be started. There are three types of supported transactions:
request
for tracing HTTP requestsjob
for tracing background jobscustom
for all other tracesApm::startRequest();
You may optionally pass $method
and $url
to the startRequest
method. By default these are gathered from $_SERVER
(see HTTP metadata below).
Apm::startRequest('POST', '/posts/:id');
For best practice the URL should be parameterized to not include dynamic parts of a path that change often but represent the same requests. For example, instad of /posts/24
set the URL to be /posts/:id
For background jobs, cron jobs, CLI commands, etc. you can use the job
transaction type
Apm::startJob('job-name');
For all other transaction types you can use the custom
type
Apm::startCustom('name');
Only one transaction may be traced at a time. If you call startRequest
and then later call startJob
, the current transaction will be changed to the type job
.
The start time of a transaction is set to when the transaction is created by default. Similarly the end time is set to the time a transaction is finished and sent.
// Start time set at the time startRequest is called
Apm::startRequest('POST', '/url');
// End time set at the time finish is called
Apm::finish();
To set a custom start/end time
// Time should be a unix timestamp in milliseconds
$time = microtime(true) * 1000;
Apm::setStartTime($time);
Apm::setEndTime($time);
finish()
will not overwrite the end time if it's been manually set
The environment can be optionally passed as a third argument to init()
Apm::init('...token', 'service-name', 'production');
or it can be set through the service
metadata
Apm::service()->setEnvironment('production');
HTTP metadata is collected by default from the $_SERVER
superglobal, but can be customized as needed
Apm::http()
->setMethod('PUT')
->setUrl('/some/path/:id')
->setUrlParams(['id' => 22])
->setHeaders(['x-forwarded-for' => 'example.com']);
To customize the hostname for a transaction
Apm::system()->setHostname('host-123');
User information can be attached to a transaction when known
Apm::user()->setId(24);
Apm::user()->setEmail('user@example.com');
Any key/value pair can be set to pass custom metadata for a transaction
Apm::tags()
->set('version', '2.4')
->set('variation', 'b');