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.
Step 1) Extract Incoming Tracing Information
Incoming
\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 projectRepresents your service in Sentry and allows you to scope events to a distinct application..
- 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.
Step 2) Inject Tracing Information to Outgoing Requests
For distributed
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.Inject Tracing Information Into HTTP Requests
You can generate this
\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
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.
Inject Tracing Information Into Rendered HTML
To propagate
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>
Verification
If you make outgoing requests from your
sentry-trace
and baggage
are present in the request. If so, distributed Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) to suggesting an update ("yeah, this would be better").
- Package:
- composer:sentry/sentry-laravel
- Version:
- 3.8.2
- Repository:
- https://github.com/getsentry/sentry-laravel