Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"suggest": {
"corley/influxdb-sdk": "For InfluxDB integration",
"okitsu/zabbix-sender": "For zabbix integration",
"kriswallsmith/buzz": "For Librato integration",
"symfony/http-client": "For Librato integration",
"jimdo/prometheus_client_php": "For Prometheus integration"
},
"require": {
Expand All @@ -28,10 +28,11 @@
"corley/influxdb-sdk": "^0.5.1",
"doctrine/dbal": "~2.0",
"jimdo/prometheus_client_php": "^0.5",
"kriswallsmith/buzz": "*",
"kriswallsmith/buzz": "^0.16 || ^0.17",
"okitsu/zabbix-sender": "*@dev",
"symfony/config": "~3.3 || ~4.0",
"symfony/dependency-injection": "~3.3 || ~4.0",
"symfony/http-client": "^4.0 || ^5.0",
"symfony/http-kernel": "~3.3 || ~4.0"
},
"autoload": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Beberlei\Metrics\Collector\Collector;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand Down Expand Up @@ -73,6 +74,10 @@ private function createCollector($type, $config)

return $definition;
case 'librato':
if (!class_exists(HttpClient::class)) {
$definition->replaceArgument(0, new Reference('beberlei_metrics.util.buzz.browser'));
}

$definition->replaceArgument(1, $config['source']);
$definition->replaceArgument(2, $config['username']);
$definition->replaceArgument(3, $config['password']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
<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" />
</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 +30,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
39 changes: 27 additions & 12 deletions src/Beberlei/Metrics/Collector/Librato.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
namespace Beberlei\Metrics\Collector;

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

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 +39,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,10 +114,18 @@ 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) {
}
Expand Down
10 changes: 8 additions & 2 deletions src/Beberlei/Metrics/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
use Net\Zabbix\Agent\Config;
use Buzz\Browser;
use Buzz\Client\Curl;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;

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

Expand Down Expand Up @@ -192,7 +194,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