Skip to content

Commit 513a9db

Browse files
committed
Add support for symfony/http-client
1 parent 7399179 commit 513a9db

File tree

5 files changed

+48
-17
lines changed

5 files changed

+48
-17
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"suggest": {
1919
"corley/influxdb-sdk": "For InfluxDB integration",
2020
"okitsu/zabbix-sender": "For zabbix integration",
21-
"kriswallsmith/buzz": "For Librato integration",
21+
"symfony/http-client": "For Librato integration",
2222
"jimdo/prometheus_client_php": "For Prometheus integration"
2323
},
2424
"require": {
@@ -28,10 +28,11 @@
2828
"corley/influxdb-sdk": "^0.5.1",
2929
"doctrine/dbal": "~2.0",
3030
"jimdo/prometheus_client_php": "^0.5",
31-
"kriswallsmith/buzz": "*",
31+
"kriswallsmith/buzz": "^0.16||^0.17",
3232
"okitsu/zabbix-sender": "*@dev",
3333
"symfony/config": "~3.3 || ~4.0",
3434
"symfony/dependency-injection": "~3.3 || ~4.0",
35+
"symfony/http-client": "^4.0 || ^5.0",
3536
"symfony/http-kernel": "~3.3 || ~4.0"
3637
},
3738
"autoload": {

src/Beberlei/Bundle/MetricsBundle/DependencyInjection/BeberleiMetricsExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Beberlei\Metrics\Collector\Collector;
66
use Symfony\Component\Config\FileLocator;
7+
use Symfony\Component\HttpClient\HttpClient;
78
use Symfony\Component\DependencyInjection\ChildDefinition;
89
use Symfony\Component\DependencyInjection\ContainerBuilder;
910
use Symfony\Component\DependencyInjection\Definition;
@@ -73,6 +74,10 @@ private function createCollector($type, $config)
7374

7475
return $definition;
7576
case 'librato':
77+
if (!class_exists(HttpClient::class)) {
78+
$definition->replaceArgument(0, new Reference('beberlei_metrics.util.buzz.browser'));
79+
}
80+
7681
$definition->replaceArgument(1, $config['source']);
7782
$definition->replaceArgument(2, $config['username']);
7883
$definition->replaceArgument(3, $config['password']);

src/Beberlei/Bundle/MetricsBundle/Resources/config/metrics.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
<argument type="service" id="beberlei_metrics.util.buzz.curl" />
1414
</service>
1515

16+
<service id="beberlei_metrics.util.http_client" class="Symfony\Contracts\HttpClient\HttpClientInterface" public="false">
17+
<factory class="Symfony\Component\HttpClient\HttpClient" method="create" />
18+
</service>
19+
1620
<!-- Prototype / Collector -->
1721
<service id="beberlei_metrics.collector_proto.doctrine_dbal" class="Beberlei\Metrics\Collector\DoctrineDBAL" abstract="true">
1822
<argument /> <!-- Doctrine DBAL connection, set by the extension -->
@@ -26,7 +30,7 @@
2630
<argument /> <!-- InfluxDB client, set by the extension -->
2731
</service>
2832
<service id="beberlei_metrics.collector_proto.librato" class="Beberlei\Metrics\Collector\Librato" abstract="true">
29-
<argument type="service" id="beberlei_metrics.util.buzz.browser" />
33+
<argument type="service" id="beberlei_metrics.util.http_client" />
3034
<argument /> <!-- host, set by the extension -->
3135
<argument /> <!-- port, set by the extension -->
3236
<argument /> <!-- protocol, set by the extension -->

src/Beberlei/Metrics/Collector/Librato.php

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414
namespace Beberlei\Metrics\Collector;
1515

1616
use Buzz\Browser;
17+
use Symfony\Contracts\HttpClient\HttpClientInterface;
1718

1819
class Librato implements Collector
1920
{
20-
/** @var \Buzz\Browser */
21-
private $browser;
21+
const ENDPOINT = 'https://metrics-api.librato.com/v1/metrics';
22+
23+
/** @var HttpClientInterface|Browser */
24+
private $client;
2225

2326
/** @var string */
2427
private $source;
@@ -36,14 +39,18 @@ class Librato implements Collector
3639
);
3740

3841
/**
39-
* @param \Buzz\Browser $browser
40-
* @param string $source
41-
* @param string $username
42-
* @param string $password
42+
* @param HttpClientInterface|Browser $client
43+
* @param string $source
44+
* @param string $username
45+
* @param string $password
4346
*/
44-
public function __construct(Browser $browser, $source, $username, $password)
47+
public function __construct($client, $source, $username, $password)
4548
{
46-
$this->browser = $browser;
49+
if (!$client instanceof Browser && !$client instanceof HttpClientInterface) {
50+
throw new \TypeError(sprintf('Argument 1 passed to %s::%s() must be an instance of %s or %s, %s given', __CLASS__, __METHOD__, HttpClientInterface::class, Browser::class, \is_object($client) ? \get_class($client) : \gettype($client)));
51+
}
52+
53+
$this->client = $client;
4754
$this->source = $source;
4855
$this->username = $username;
4956
$this->password = $password;
@@ -107,10 +114,18 @@ public function flush()
107114
}
108115

109116
try {
110-
$this->browser->post('https://metrics-api.librato.com/v1/metrics', array(
111-
'Authorization: Basic '.base64_encode($this->username.':'.$this->password),
112-
'Content-Type: application/json',
113-
), json_encode($this->data));
117+
if ($this->client instanceof Browser) {
118+
$this->client->post(self::ENDPOINT, array(
119+
'Authorization: Basic '.base64_encode($this->username.':'.$this->password),
120+
'Content-Type: application/json',
121+
), json_encode($this->data));
122+
} else {
123+
$this->client->request('POST', self::ENDPOINT, array(
124+
'json' => $this->data,
125+
'auth_basic' => array($this->username, $this->password)
126+
));
127+
}
128+
114129
$this->data = array('gauges' => array(), 'counters' => array());
115130
} catch (\Exception $e) {
116131
}

src/Beberlei/Metrics/Factory.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717
use Net\Zabbix\Agent\Config;
1818
use Buzz\Browser;
1919
use Buzz\Client\Curl;
20+
use Symfony\Component\HttpClient\HttpClient;
21+
use Symfony\Contracts\HttpClient\HttpClientInterface;
2022

2123
/**
2224
* Static factory for Metrics Collectors.
2325
*/
2426
abstract class Factory
2527
{
2628
/**
27-
* @var Buzz\Browser
29+
* @var HttpClientInterface|Browser
2830
*/
2931
private static $httpClient;
3032

@@ -192,7 +194,11 @@ public static function create($type, array $options = array())
192194
private static function getHttpClient()
193195
{
194196
if (self::$httpClient === null) {
195-
self::$httpClient = new Browser(new Curl());
197+
if (class_exists(HttpClient::class)) {
198+
self::$httpClient = HttpClient::create();
199+
} else {
200+
self::$httpClient = new Browser(new Curl());
201+
}
196202
}
197203

198204
return self::$httpClient;

0 commit comments

Comments
 (0)