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: 2 additions & 3 deletions library/Graphite/Graphing/GraphingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@

namespace Icinga\Module\Graphite\Graphing;

use GuzzleHttp\Psr7\Uri;
use Icinga\Application\Config;
use Icinga\Application\Icinga;
use Icinga\Data\ConfigObject;
use Icinga\Exception\ConfigurationError;
use Icinga\Module\Graphite\Web\FakeSchemeRequest;
use Icinga\Web\Url;

trait GraphingTrait
{
Expand Down Expand Up @@ -72,7 +71,7 @@ public static function getMetricsDataSource()
}

static::$metricsDataSource = new MetricsDataSource(
(new GraphiteWebClient(Url::fromPath($graphite->url, [], new FakeSchemeRequest())))
(new GraphiteWebClient(new Uri($graphite->url)))
->setUser($graphite->user)
->setPassword($graphite->password)
->setInsecure((bool) $graphite->insecure)
Expand Down
54 changes: 29 additions & 25 deletions library/Graphite/Graphing/GraphiteWebClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Psr7\Request;
use Icinga\Web\Url;
use Psr\Http\Message\UriInterface;

/**
* HTTP interface to Graphite Web
Expand All @@ -18,9 +18,9 @@ class GraphiteWebClient
/**
* Base URL of every Graphite Web HTTP request
*
* @var Url
* @var UriInterface
*/
protected $baseUrl;
protected $baseUri;

/**
* HTTP basic auth user for every Graphite Web HTTP request
Expand Down Expand Up @@ -60,26 +60,26 @@ class GraphiteWebClient
/**
* Constructor
*
* @param Url $baseUrl Base URL of every Graphite Web HTTP request
* @param UriInterface $baseUrl Base URL of every Graphite Web HTTP request
*/
public function __construct(Url $baseUrl)
public function __construct(UriInterface $baseUrl)
{
$this->httpClient = new Client();

$this->setBaseUrl($baseUrl);
$this->setBaseUri($baseUrl);
}

/**
* Send an HTTP request to the configured Graphite Web and return the response's body
*
* @param Url $url
* @param string $method
* @param string[] $headers
* @param string $body
* @param UriInterface $uri
* @param string $method
* @param string[] $headers
* @param string $body
*
* @return string
*/
public function request(Url $url, $method = 'GET', array $headers = [], $body = null)
public function request(UriInterface $uri, $method = 'GET', array $headers = [], $body = null)
{
$headers['User-Agent'] = 'icingaweb2-module-graphite';
if ($this->user !== null) {
Expand All @@ -88,7 +88,7 @@ public function request(Url $url, $method = 'GET', array $headers = [], $body =

// TODO(ak): keep connections alive (TCP handshakes are a bit expensive and TLS handshakes are very expensive)
return (string) $this->httpClient->send(
new Request($method, $this->completeUrl($url)->getAbsoluteUrl(), $headers, $body),
new Request($method, $this->completeUri($uri), $headers, $body),
[
'curl' => [
CURLOPT_SSL_VERIFYPEER => ! $this->insecure
Expand All @@ -101,38 +101,42 @@ public function request(Url $url, $method = 'GET', array $headers = [], $body =
/**
* Complete the given relative URL according to the base URL
*
* @param Url $url
* @param UriInterface $uri
*
* @return Url
* @return UriInterface
*/
public function completeUrl(Url $url)
public function completeUri(UriInterface $uri)
{
$completeUrl = clone $this->baseUrl;
return $completeUrl
->setPath(ltrim(rtrim($completeUrl->getPath(), '/') . '/' . ltrim($url->getPath(), '/'), '/'))
->setParams($url->getParams());
parse_str($this->baseUri->getQuery(), $originalParams);
parse_str($uri->getQuery(), $newParams);

$uri = $this->baseUri
->withPath(ltrim(rtrim($this->baseUri->getPath(), '/') . '/' . ltrim($uri->getPath(), '/'), '/'))
->withQuery(http_build_query(array_merge($originalParams, $newParams)));

return $uri;
}

/**
* Get the base URL of every Graphite Web HTTP request
*
* @return Url
* @return UriInterface
*/
public function getBaseUrl()
public function getBaseUri()
{
return $this->baseUrl;
return $this->baseUri;
}

/**
* Set the base URL of every Graphite Web HTTP request
*
* @param Url $baseUrl
* @param UriInterface $baseUri
*
* @return $this
*/
public function setBaseUrl(Url $baseUrl)
public function setBaseUri(UriInterface $baseUri)
{
$this->baseUrl = $baseUrl;
$this->baseUri = $baseUri;

return $this;
}
Expand Down
12 changes: 5 additions & 7 deletions library/Graphite/Graphing/MetricsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Icinga\Module\Graphite\Graphing;

use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\Psr7\Uri;
use Icinga\Data\Fetchable;
use Icinga\Data\Filter\Filter;
use Icinga\Data\Filterable;
Expand All @@ -20,7 +21,6 @@
use Icinga\Module\Monitoring\Object\Macro;
use Icinga\Module\Monitoring\Object\MonitoredObject;
use Icinga\Util\Json;
use Icinga\Web\Url;
use InvalidArgumentException;
use ipl\Orm\Model;
use ipl\Stdlib\Filter as IplFilter;
Expand Down Expand Up @@ -173,19 +173,17 @@ public function fetchColumn()
}

$client = $this->dataSource->getClient();
$url = Url::fromPath('metrics/expand', [
'query' => $this->base->resolve($filter, '*')
]);
$uri = new Uri('metrics/expand?' . http_build_query(['query' => $this->base->resolve($filter, '*')]));
try {
$res = Json::decode($client->request($url));
$res = Json::decode($client->request($uri));
} catch (ServerException $e) {
IPT::recordf('Fetched 0 metric(s) from %s: %s', (string) $client->completeUrl($url), $e->getMessage());
IPT::recordf('Fetched 0 metric(s) from %s: %s', (string) $client->completeUri($uri), $e->getMessage());

return [];
}
natsort($res->results);

IPT::recordf('Fetched %s metric(s) from %s', count($res->results), (string) $client->completeUrl($url));
IPT::recordf('Fetched %s metric(s) from %s', count($res->results), (string) $client->completeUri($uri));

return array_values($res->results);
}
Expand Down
21 changes: 0 additions & 21 deletions library/Graphite/Web/FakeSchemeRequest.php

This file was deleted.

13 changes: 7 additions & 6 deletions library/Graphite/Web/Widget/GraphImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

namespace Icinga\Module\Graphite\Web\Widget;

use GuzzleHttp\Psr7\Uri;
use Icinga\Module\Graphite\Graphing\Chart;
use Icinga\Web\Url;
use Icinga\Web\UrlParams;
use Icinga\Web\Widget\AbstractWidget;
use RuntimeException;
Expand Down Expand Up @@ -109,27 +109,28 @@ public function render()
$params->add('target', $template->getCurves()[$curveName][1]->resolve($allVars));
}

$url = Url::fromPath('/render')->setParams($params);
$uri = new Uri('/render?' . $params->toString());
$headers = [
'Accept-language' => 'en',
'Content-type' => 'application/x-www-form-urlencoded'
];

for (;;) {
try {
$this->rendered = $graphiteWebClient->request($url, 'GET', $headers);
$this->rendered = $graphiteWebClient->request($uri, 'GET', $headers);
} catch (RuntimeException $e) {
if (preg_match('/\b500\b/', $e->getMessage())) {
// A 500 Internal Server Error, probably because of
// a division by zero because of a too low time range to render.

$until = (int) $url->getParam('until');
$diff = $until - (int) $url->getParam('from');
$until = (int) $params['until'];
$diff = $until - (int) $params['from'];

// Try to render a higher time range, but give up
// once our default (1h) has been reached (non successfully).
if ($diff < 3600) {
$url->setParam('from', sprintf('%F', ($until - $diff * 2)));
$params['from'] = sprintf('%F', ($until - $diff * 2));
$uri = $uri->withQuery(http_build_query($params));
continue;
}
}
Expand Down
Loading