forked from DataDog/dd-trace-php
/
Curl.php
50 lines (43 loc) · 1.43 KB
/
Curl.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
<?php
namespace DDTrace\Integrations;
use DDTrace\Tags;
use DDTrace\Types;
use OpenTracing\GlobalTracer;
/**
* Tracing of the Curl library.
*
* Currently only wraps curl_exec and adds info to the trace from curl_getinfo
*/
class Curl
{
public static function load()
{
if (!extension_loaded('ddtrace')) {
trigger_error('The ddtrace extension is required to instrument Memcached', E_USER_WARNING);
return;
}
if (!function_exists('curl_exec')) {
trigger_error('Curl is not loaded and connot be instrumented', E_USER_WARNING);
}
dd_trace('curl_exec', function ($ch) {
$scope = GlobalTracer::get()->startActiveSpan("curl");
$span = $scope->getSpan();
$span->setTag(Tags\SPAN_TYPE, Types\CURL);
$span->setTag(Tags\SERVICE_NAME, 'curl');
try {
$result = curl_exec($ch);
$info = curl_getinfo($ch);
$span->setResource($info['url']);
$span->setTag('curl.http_code', $info['http_code']);
$span->setTag('curl.header_size', $info['header_size']);
$span->setTag('curl.request_size', $info['request_size']);
return $result;
} catch (\Exception $e) {
$span->setError($e);
throw $e;
} finally {
$scope->close();
}
});
}
}