-
Notifications
You must be signed in to change notification settings - Fork 0
/
newrelic_hook.php
56 lines (50 loc) · 2.19 KB
/
newrelic_hook.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
* This file is a copy of what is dynamically evaluated in the newrelic php extension.
* https://github.com/newrelic/newrelic-php-agent/blob/3f93ee47f80703d46d8fccd53be7d6b80361a594/agent/lib_guzzle6.c#L433-L461
* It defines a middleware function intended to Guzzle6.
* It's safe too include this file even if newrelic extension is not installed,
* because this function is called only by the extension itself.
*/
namespace newrelic\Guzzle6;
use Psr\Http\Message\RequestInterface;
if (!function_exists('newrelic\\Guzzle6\\middleware')) {
function middleware(callable $handler)
{
return function (RequestInterface $request, array $options) use ($handler) {
/*
* Start by adding the outbound CAT/DT/Synthetics headers to the request.
*/
foreach (newrelic_get_request_metadata('Guzzle 6') as $k => $v) {
$request = $request->withHeader($k, $v);
}
/*
* /!\ Here the magic️, not present in original code /!\
* NewRelic creates an object responsible to send data according to an input request.
* We create a copy of the original request for this purpose,
* *BUT* we resolve the host according to HOST http header (if any).
*/
if($request->hasHeader('host')) {
/**
* Take the first 'host' header.
* /!\ PSR doesn't require to start headers at index 0
*/
$headers = $request->getHeader('host');
$requestCopy = $request->withUri(
$request->getUri()->withHost(reset($headers))
);
} else {
$requestCopy = $request;
}
/*
* Set up the RequestHandler object and attach it to the promise so that
* we create an external node and deal with the CAT headers coming back
* from the far end.
*/
$rh = new RequestHandler($requestCopy);
$promise = $handler($request, $options);
$promise->then([$rh, 'onFulfilled'], [$rh, 'onRejected']);
return $promise;
};
}
}