|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DDTrace\Integrations\Guzzle\V6; |
| 4 | + |
| 5 | +use DDTrace\Configuration; |
| 6 | +use DDTrace\Formats; |
| 7 | +use DDTrace\Tags; |
| 8 | +use DDTrace\Span; |
| 9 | +use DDTrace\Types; |
| 10 | +use OpenTracing\GlobalTracer; |
| 11 | +use DDTrace\Http\Urls; |
| 12 | +use DDTrace\Integrations\Integration; |
| 13 | +use GuzzleHttp\Message\ResponseInterface; |
| 14 | + |
| 15 | +class GuzzleIntegration extends Integration |
| 16 | +{ |
| 17 | + const NAME = 'guzzle'; |
| 18 | + const CLASS_NAME = 'GuzzleHttp\Client'; |
| 19 | + |
| 20 | + protected static function loadIntegration() |
| 21 | + { |
| 22 | + self::traceMethod('send', function (Span $span, array $args) { |
| 23 | + list($request) = $args; |
| 24 | + GuzzleIntegration::injectDistributedTracingHeaders($request, $span); |
| 25 | + $span->setTag('http.method', $request->getMethod()); |
| 26 | + $span->setTag('http.url', Urls::sanitize($request->getUri())); |
| 27 | + }, function (Span $span, $response) { |
| 28 | + if (!$response instanceof ResponseInterface) { |
| 29 | + return; |
| 30 | + } |
| 31 | + $span->setTag('http.status_code', $response->getStatusCode()); |
| 32 | + }); |
| 33 | + } |
| 34 | + |
| 35 | + public static function setDefaultTags(Span $span, $method) |
| 36 | + { |
| 37 | + parent::setDefaultTags($span, $method); |
| 38 | + $span->setTag(Tags\SPAN_TYPE, Types\HTTP_CLIENT); |
| 39 | + $span->setTag(Tags\SERVICE_NAME, 'guzzle'); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @param \GuzzleHttp\Message\MessageInterface $request |
| 44 | + * @param Span $span |
| 45 | + */ |
| 46 | + public static function injectDistributedTracingHeaders($request, $span) |
| 47 | + { |
| 48 | + if (!Configuration::get()->isDistributedTracingEnabled()) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + if (!is_subclass_of($request, '\GuzzleHttp\Message\MessageInterface')) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + // Associative array of header names to values |
| 57 | + $headers = $request->getHeaders(); |
| 58 | + |
| 59 | + $context = $span->getContext(); |
| 60 | + $tracer = GlobalTracer::get(); |
| 61 | + $tracer->inject($context, Formats\HTTP_HEADERS, $headers); |
| 62 | + $request->setHeaders($headers); |
| 63 | + } |
| 64 | +} |
0 commit comments