Skip to content

Commit

Permalink
Add support for symfony/http-client
Browse files Browse the repository at this point in the history
  • Loading branch information
azjezz committed Jan 7, 2020
1 parent 7399179 commit 3adfee7
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 22 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"suggest": {
"corley/influxdb-sdk": "For InfluxDB integration",
"okitsu/zabbix-sender": "For zabbix integration",
"symfony/http-client": "For Librato integration",
"kriswallsmith/buzz": "For Librato integration",
"jimdo/prometheus_client_php": "For Prometheus integration"
},
Expand All @@ -28,11 +29,12 @@
"corley/influxdb-sdk": "^0.5.1",
"doctrine/dbal": "~2.0",
"jimdo/prometheus_client_php": "^0.5",
"kriswallsmith/buzz": "*",
"okitsu/zabbix-sender": "*@dev",
"symfony/config": "~3.3 || ~4.0",
"symfony/dependency-injection": "~3.3 || ~4.0",
"symfony/http-kernel": "~3.3 || ~4.0"
"symfony/http-kernel": "~3.3 || ~4.0",
"symfony/http-client": "^4.0 || ^5.0",
"kriswallsmith/buzz": "^0.16"
},
"autoload": {
"psr-4": {
Expand Down
12 changes: 9 additions & 3 deletions src/Beberlei/Bundle/MetricsBundle/Resources/config/metrics.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@

<services>
<!-- Util -->
<service id="beberlei_metrics.util.buzz.curl" class="Buzz\Client\Curl" public="false">
</service>
<service id="beberlei_metrics.util.buzz.curl" class="Buzz\Client\Curl" public="false" />

<service id="beberlei_metrics.util.buzz.browser" class="Buzz\Browser" public="false">
<argument type="service" id="beberlei_metrics.util.buzz.curl" />
</service>

<service id="beberlei_metrics.util.http_client" class="Symfony\Contracts\HttpClient\HttpClientInterface" public="false">
<factory class="Symfony\Component\HttpClient\HttpClient" method="create" />
<call method="setLogger">
<argument type="service" id="logger" />
</call>
</service>

<!-- Prototype / Collector -->
<service id="beberlei_metrics.collector_proto.doctrine_dbal" class="Beberlei\Metrics\Collector\DoctrineDBAL" abstract="true">
<argument /> <!-- Doctrine DBAL connection, set by the extension -->
Expand All @@ -26,7 +32,7 @@
<argument /> <!-- InfluxDB client, set by the extension -->
</service>
<service id="beberlei_metrics.collector_proto.librato" class="Beberlei\Metrics\Collector\Librato" abstract="true">
<argument type="service" id="beberlei_metrics.util.buzz.browser" />
<argument type="service" id="beberlei_metrics.util.http_client" />
<argument /> <!-- host, set by the extension -->
<argument /> <!-- port, set by the extension -->
<argument /> <!-- protocol, set by the extension -->
Expand Down
42 changes: 29 additions & 13 deletions src/Beberlei/Metrics/Collector/Librato.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
namespace Beberlei\Metrics\Collector;

use Buzz\Browser;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\Exception\ExceptionInterface;

class Librato implements Collector
{
/** @var \Buzz\Browser */
private $browser;
const ENDPOINT = 'https://metrics-api.librato.com/v1/metrics';

/** @var HttpClientInterface|Browser */
private $client;

/** @var string */
private $source;
Expand All @@ -36,14 +40,18 @@ class Librato implements Collector
);

/**
* @param \Buzz\Browser $browser
* @param string $source
* @param string $username
* @param string $password
* @param HttpClientInterface|Browser $client
* @param string $source
* @param string $username
* @param string $password
*/
public function __construct(Browser $browser, $source, $username, $password)
public function __construct($client, $source, $username, $password)
{
$this->browser = $browser;
if (!$client instanceof Browser && !$client instanceof HttpClientInterface) {
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)));
}

$this->client = $client;
$this->source = $source;
$this->username = $username;
$this->password = $password;
Expand Down Expand Up @@ -107,12 +115,20 @@ public function flush()
}

try {
$this->browser->post('https://metrics-api.librato.com/v1/metrics', array(
'Authorization: Basic '.base64_encode($this->username.':'.$this->password),
'Content-Type: application/json',
), json_encode($this->data));
if ($this->client instanceof Browser) {
$this->client->post(self::ENDPOINT, array(
'Authorization: Basic '.base64_encode($this->username.':'.$this->password),
'Content-Type: application/json',
), json_encode($this->data));
} else {
$this->client->request('POST', self::ENDPOINT, array(
'json' => $this->data,
'auth_basic' => array($this->username, $this->password)
));
}

$this->data = array('gauges' => array(), 'counters' => array());
} catch (\Exception $e) {
} catch (ExceptionInterface $e) {
}
}
}
13 changes: 9 additions & 4 deletions src/Beberlei/Metrics/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@

namespace Beberlei\Metrics;

use Net\Zabbix\Sender;
use Net\Zabbix\Agent\Config;
use Buzz\Browser;
use Buzz\Client\Curl;
use Net\Zabbix\Sender;
use Net\Zabbix\Agent\Config;
use Symfony\Component\HttpClient\HttpClient;

/**
* Static factory for Metrics Collectors.
*/
abstract class Factory
{
/**
* @var Buzz\Browser
* @var \Symfony\Contracts\HttpClient\HttpClientInterface
*/
private static $httpClient;

Expand Down Expand Up @@ -192,7 +193,11 @@ public static function create($type, array $options = array())
private static function getHttpClient()
{
if (self::$httpClient === null) {
self::$httpClient = new Browser(new Curl());
if (class_exists(HttpClient::class)) {
self::$httpClient = HttpClient::create();
} else {
self::$httpClient = new Browser(new Curl());
}
}

return self::$httpClient;
Expand Down

0 comments on commit 3adfee7

Please sign in to comment.