icingaweb2-module-graphite/library/Graphite/Graphing/GraphingTrait.php
Adam James 5e0d57cce0 Add Graphite Web HTTP request timeout option
If the Graphite Web server is unreachable, all requests for frontend
pages containing graphs hang until the backend HTTP request times out,
resulting in a very poor UX.

The Guzzle documentation states that the default behaviour is to wait
indefinitely, however in our testing the cURL handler has an internal
default of 30 seconds:

https://docs.guzzlephp.org/en/stable/request-options.html#timeout

This commit makes the HTTP request timeout configurable and sets a
reasonable default of 10 seconds.
2024-02-20 15:43:34 +01:00

82 lines
2.2 KiB
PHP

<?php
namespace Icinga\Module\Graphite\Graphing;
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
{
/**
* All loaded templates
*
* @var Templates
*/
protected static $allTemplates;
/**
* Metrics data source
*
* @var MetricsDataSource
*/
protected static $metricsDataSource;
/**
* Load and get all templates
*
* @return Templates
*/
protected static function getAllTemplates()
{
if (static::$allTemplates === null) {
$allTemplates = (new Templates())->loadDir(
Icinga::app()
->getModuleManager()
->getModule('graphite')
->getBaseDir() . DIRECTORY_SEPARATOR . 'templates'
);
$path = Config::resolvePath('modules/graphite/templates');
if (file_exists($path)) {
$allTemplates->loadDir($path);
}
static::$allTemplates = $allTemplates;
}
return static::$allTemplates;
}
/**
* Get metrics data source
*
* @return MetricsDataSource
*
* @throws ConfigurationError
*/
public static function getMetricsDataSource()
{
if (static::$metricsDataSource === null) {
$config = Config::module('graphite');
/** @var ConfigObject<string> $graphite */
$graphite = $config->getSection('graphite');
if (! isset($graphite->url)) {
throw new ConfigurationError('Missing "graphite.url" in "%s"', $config->getConfigFile());
}
static::$metricsDataSource = new MetricsDataSource(
(new GraphiteWebClient(Url::fromPath($graphite->url, [], new FakeSchemeRequest())))
->setUser($graphite->user)
->setPassword($graphite->password)
->setInsecure((bool) $graphite->insecure)
->setTimeout(isset($graphite->timeout) ? intval($graphite->timeout) : 10)
);
}
return static::$metricsDataSource;
}
}