Skip to content

Commit

Permalink
Support for guzzle 6 (#254)
Browse files Browse the repository at this point in the history
* [guzzle] Add support for guzzle 6

* update changelog

* [tests] Add execution of guzzle 6 tests

* [ci] Fix lintings and phpstan

* [tests] Fix url of request replayer file

* [ci] Fixes to the composer.json scenarios approach

* Apply suggestions from code review

Co-Authored-By: labbati <luca.abbati@datadoghq.com>
  • Loading branch information
labbati authored and SammyK committed Jan 23, 2019
1 parent 27b708f commit ffb1fc0
Show file tree
Hide file tree
Showing 30 changed files with 686 additions and 125 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ All notable changes to this project will be documented in this file - [read more

## [UNRELEASED]

### Added
- Support for guzzle 6 #254

### Fixed
- Composer php compatibility declaration #247
- Add missing files to PECL releases #252
Expand Down
34 changes: 29 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"DDTrace\\Tests\\": "./tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"fix-lint": "phpcbf",
Expand Down Expand Up @@ -108,7 +107,9 @@
"@composer scenario elasticsearch1",
"@composer test -- tests/Integrations/Elasticsearch",
"@composer scenario guzzle5",
"@composer test -- tests/Integrations/Guzzle",
"@composer test -- tests/Integrations/Guzzle/V5",
"@composer scenario guzzle6",
"@composer test -- tests/Integrations/Guzzle/V6",
"@composer scenario predis1",
"@composer test -- tests/Integrations/Predis",
"@composer scenario default"
Expand All @@ -123,7 +124,9 @@
"@composer scenario elasticsearch1",
"@composer test -- tests/Integrations/Elasticsearch",
"@composer scenario guzzle5",
"@composer test -- tests/Integrations/Guzzle",
"@composer test -- tests/Integrations/Guzzle/V5",
"@composer scenario guzzle6",
"@composer test -- tests/Integrations/Guzzle/V6",
"@composer scenario predis1",
"@composer test -- tests/Integrations/Predis",
"@composer scenario default"
Expand All @@ -138,7 +141,9 @@
"@composer scenario elasticsearch1",
"@composer test -- tests/Integrations/Elasticsearch",
"@composer scenario guzzle5",
"@composer test -- tests/Integrations/Guzzle",
"@composer test -- tests/Integrations/Guzzle/V5",
"@composer scenario guzzle6",
"@composer test -- tests/Integrations/Guzzle/V6",
"@composer scenario predis1",
"@composer test -- tests/Integrations/Predis",
"@composer scenario default"
Expand All @@ -153,7 +158,9 @@
"@composer scenario elasticsearch1",
"@composer test -- tests/Integrations/Elasticsearch",
"@composer scenario guzzle5",
"@composer test -- tests/Integrations/Guzzle",
"@composer test -- tests/Integrations/Guzzle/V5",
"@composer scenario guzzle6",
"@composer test -- tests/Integrations/Guzzle/V6",
"@composer scenario predis1",
"@composer test -- tests/Integrations/Predis",
"@composer scenario default"
Expand Down Expand Up @@ -210,16 +217,33 @@
"elasticsearch1": {
"require": {
"elasticsearch/elasticsearch": "1.2.*"
},
"scenario-options": {
"create-lockfile": false
}
},
"guzzle5": {
"require": {
"guzzlehttp/guzzle": "~5.0"
},
"scenario-options": {
"create-lockfile": false
}
},
"guzzle6": {
"require": {
"guzzlehttp/guzzle": "~6.0"
},
"scenario-options": {
"create-lockfile": false
}
},
"predis1": {
"require": {
"predis/predis": "^1.1"
},
"scenario-options": {
"create-lockfile": false
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,4 @@ services:
- 9000:80
environment:
DD_REQUEST_DUMPER_FILE: dump.json
command: sh -c "curl --output index.php https://raw.githubusercontent.com/DataDog/dd-trace-php/labbati/fix-54-tests/tests/request_replayer/index.php -q && php -S 0.0.0.0:80 index.php"
command: sh -c "curl --output index.php https://raw.githubusercontent.com/DataDog/dd-trace-php/master/tests/request_replayer/index.php -q && php -S 0.0.0.0:80 index.php"
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
parameters:
excludes_analyse:
- src/DDTrace/Util/CodeTracer.php
- src/DDTrace/Integrations
- src/DDTrace/OpenTracer
# Temporarily disabling as it uses \OpenTracing\GlobalTracer.php
Expand Down
3 changes: 2 additions & 1 deletion src/DDTrace/Contracts/Span.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ public function overwriteOperationName($newOperationName);
*
* @param string $key
* @param string|bool|int|float $value
* @param boolean $setIfFinished
* @return void
*/
public function setTag($key, $value);
public function setTag($key, $value, $setIfFinished = false);

/**
* @param string $key
Expand Down
4 changes: 2 additions & 2 deletions src/DDTrace/Integrations/Curl/CurlIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use DDTrace\Type;
use DDTrace\Util\ArrayKVStore;
use DDTrace\GlobalTracer;
use DDTrace\Util\Environment;
use DDTrace\Util\Versions;

/**
* Integration for curl php client.
Expand All @@ -25,7 +25,7 @@ class CurlIntegration
*/
public static function load()
{
if (!function_exists('curl_exec') || Environment::matchesPhpVersion('5.4')) {
if (!function_exists('curl_exec') || Versions::phpVersionMatches('5.4')) {
// `curl_exec` doesn't come from an autoloader, if it does not exists we can return this integration as
// not available.
return Integration::NOT_AVAILABLE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use DDTrace\Span;
use DDTrace\Tag;
use DDTrace\Type;
use DDTrace\Util\Environment;
use DDTrace\Util\Versions;

/**
* ElasticSearch driver v1 Integration
Expand All @@ -19,7 +19,7 @@ class ElasticSearchIntegration

public static function load()
{
if (!class_exists('Elasticsearch\Client') || Environment::matchesPhpVersion('5.4')) {
if (!class_exists('Elasticsearch\Client') || Versions::phpVersionMatches('5.4')) {
return Integration::NOT_LOADED;
}

Expand Down
4 changes: 2 additions & 2 deletions src/DDTrace/Integrations/Eloquent/EloquentIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use DDTrace\Integrations\Integration;
use DDTrace\Tag;
use DDTrace\Type;
use DDTrace\Util\Environment;
use DDTrace\Util\Versions;
use DDTrace\Util\TryCatchFinally;
use DDTrace\GlobalTracer;

Expand All @@ -15,7 +15,7 @@ class EloquentIntegration

public static function load()
{
if (!class_exists('Illuminate\Database\Eloquent\Builder') || Environment::matchesPhpVersion('5.4')) {
if (!class_exists('Illuminate\Database\Eloquent\Builder') || Versions::phpVersionMatches('5.4')) {
return Integration::NOT_LOADED;
}

Expand Down
106 changes: 106 additions & 0 deletions src/DDTrace/Integrations/Guzzle/AbstractGuzzleIntegrationLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace DDTrace\Integrations\Guzzle;

use DDTrace\Configuration;
use DDTrace\Contracts\Span;
use DDTrace\Format;
use DDTrace\GlobalTracer;
use DDTrace\Integrations\Integration;
use DDTrace\Tag;
use DDTrace\Type;
use DDTrace\Util\CodeTracer;

/**
* Abstract integration loader for Guzzle integrations.
*/
abstract class AbstractGuzzleIntegrationLoader
{
/**
* @var CodeTracer
*/
private $codeTracer;

/**
* @param Span $span
* @param mixed $request
*/
abstract protected function setUrlTag(Span $span, $request);

/**
* @param Span $span
* @param mixed $response
*/
abstract protected function setStatusCodeTag(Span $span, $response);

/**
* @param mixed $request
*/
abstract protected function extractRequestHeaders($request);

/**
* @param mixed $request
* @param array $headers
*/
abstract protected function addRequestHeaders($request, $headers);

/**
* @return string
*/
abstract protected function getMethodToTrace();

/**
* @param CodeTracer $codeTracer
*/
public function __construct(CodeTracer $codeTracer)
{
$this->codeTracer = $codeTracer;
}

/**
* @param string $name
* @return int
*/
public function load($name)
{
$self = $this;
$method = $this->getMethodToTrace();

$this->codeTracer->tracePublicMethod(
'GuzzleHttp\Client',
$method,
function (Span $span, array $args) use ($self, $name, $method) {
list($request) = $args;
$self->applyDistributedTracingHeaders($span, $request);
$span->setTag(Tag::SPAN_TYPE, Type::HTTP_CLIENT);
$span->setTag(Tag::SERVICE_NAME, $name);
$span->setTag(Tag::HTTP_METHOD, $request->getMethod());
$self->setUrlTag($span, $request);
$span->setTag(Tag::RESOURCE_NAME, $method);
},
function (Span $span, $response) use ($self) {
$self->setStatusCodeTag($span, $response);
}
);

return Integration::LOADED;
}

/**
* @param mixed $request
* @param Span $span
*/
public function applyDistributedTracingHeaders(Span $span, $request)
{
if (!Configuration::get()->isDistributedTracingEnabled()) {
return;
}

$headers = $this->extractRequestHeaders($request);

$context = $span->getContext();
$tracer = GlobalTracer::get();
$tracer->inject($context, Format::HTTP_HEADERS, $headers);
$this->addRequestHeaders($request, $headers);
}
}
36 changes: 36 additions & 0 deletions src/DDTrace/Integrations/Guzzle/GuzzleIntegration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php


namespace DDTrace\Integrations\Guzzle;

use DDTrace\Integrations\Integration;
use DDTrace\Util\CodeTracer;
use DDTrace\Util\Versions;
use DDTrace\Integrations\Guzzle\V5\GuzzleIntegrationLoader as V5Loader;
use DDTrace\Integrations\Guzzle\V6\GuzzleIntegrationLoader as V6Loader;

final class GuzzleIntegration
{
const NAME = 'guzzle';

public static function load()
{
if (Versions::phpVersionMatches('5.4')) {
return Integration::NOT_AVAILABLE;
}

if (!defined('GuzzleHttp\ClientInterface::VERSION')) {
return Integration::NOT_LOADED;
}

$version = \GuzzleHttp\ClientInterface::VERSION;

if (Versions::versionMatches('5', $version)) {
return (new V5Loader(CodeTracer::getInstance()))->load(self::NAME);
} elseif (Versions::versionMatches('6', $version)) {
return (new V6Loader(CodeTracer::getInstance()))->load(self::NAME);
}

return Integration::NOT_LOADED;
}
}
69 changes: 0 additions & 69 deletions src/DDTrace/Integrations/Guzzle/V5/GuzzleIntegration.php

This file was deleted.

Loading

0 comments on commit ffb1fc0

Please sign in to comment.