Custom Instrumentation
On this page you will learn how to manually propagate trace information into and out of your PHP application.
To set it up manually, all you have to do is to make sure your application extracts incoming headers and to set those headers again when making an outgoing request within your application.
Incoming tracing information has to be extracted and stored in memory for later use. Sentry provides the \Sentry\continueTrace()
function to help you with this. Incoming tracing information can come from different places:
- In a web environment it will be sent via HTTP headers, for example, by another Sentry SDK used in your frontend project.
- You also can pick up tracing information from environment variables.
Here's an example of how to extract and store incoming tracing information using \Sentry\continueTrace()
:
$sentryTraceHeader = $request->getHeaderLine('sentry-trace');
$baggageHeader = $request->getHeaderLine('baggage');
\Sentry\continueTrace($sentryTraceHeader, $baggageHeader);
Sentry's \Sentry\continueTrace()
function will extract the given headers, try to find the sentry-trace
and baggage
headers, and store them in memory for later use.
For distributed tracing to work, the two headers sentry-trace
and baggage
, must now also be added to outgoing requests. If you pregenerate HTML on the server-side, you might want to take a look at option 2 as well, which describes how to pass on tracing information through HTML meta tags.
You can generate this tracing information with the Sentry SDK's \Sentry\getTraceparent()
and \Sentry\getBaggage()
functions. Here's an example using Guzzle:
$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'https://example.com', [
'headers' => [
'baggage' => \Sentry\getBaggage(),
'sentry-trace' => \Sentry\getTraceparent(),
]
]);
In this example, tracing information is propagated to the project running at https://example.com
. If this project uses a Sentry SDK, it will extract and save the tracing information for later use.
The two services are now connected with your custom distributed tracing implementation.
To propagate tracing information into JavaScript running in rendered HTML you have to inject HTML meta
tags for sentry-trace
and baggage
data into your rendered HTML. Here's an example:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<?= sprintf('<meta name="baggage" content="%s"/>', \Sentry\getBaggage()); ?>
<?= sprintf('<meta name="sentry-trace" content="%s"/>', \Sentry\getTraceparent()); ?>
</head>
<body>
<p>This is a website.</p>
</body>
</html>
If you make outgoing requests from your project to other services, check if the headers sentry-trace
and baggage
are present in the request. If so, distributed tracing is working.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").
- Package:
- composer:sentry/sentry-laravel
- Version:
- 4.5.1
- Repository:
- https://github.com/getsentry/sentry-laravel