Skip to content

Commit

Permalink
Merged in feature/upgrade_to_php_7 (pull request #1)
Browse files Browse the repository at this point in the history
Upgrade Prometheus client to PHP 7

Approved-by: Andre Ferraz <[email protected]>
Approved-by: DuShaun Alderson-Claeys <[email protected]>
  • Loading branch information
Daniel Noel-Davies committed Jul 22, 2019
2 parents c7cf01c + 6a644c3 commit 72ca2e7
Show file tree
Hide file tree
Showing 34 changed files with 783 additions and 714 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
],
"require": {
"php": ">=5.6.3",
"php": "^7.1",
"ext-json": "*",
"guzzlehttp/guzzle": "^6.2",
"symfony/polyfill-apcu": "^1.6"
Expand Down
4 changes: 2 additions & 2 deletions examples/flush_adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
$adapter = $_GET['adapter'];

if ($adapter === 'redis') {
define('REDIS_HOST', isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1');
define('REDIS_HOST', $_SERVER['REDIS_HOST'] ?? '127.0.0.1');

$redisAdapter = new Prometheus\Storage\Redis(array('host' => REDIS_HOST));
$redisAdapter = new Prometheus\Storage\Redis(['host' => REDIS_HOST]);
$redisAdapter->flushRedis();
} elseif ($adapter === 'apc') {
$apcAdapter = new Prometheus\Storage\APC();
Expand Down
2 changes: 1 addition & 1 deletion examples/metrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
$adapter = $_GET['adapter'];

if ($adapter === 'redis') {
Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
Redis::setDefaultOptions(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
$adapter = new Prometheus\Storage\Redis();
} elseif ($adapter === 'apc') {
$adapter = new Prometheus\Storage\APC();
Expand Down
4 changes: 2 additions & 2 deletions examples/pushgateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$adapter = $_GET['adapter'];

if ($adapter === 'redis') {
Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
Redis::setDefaultOptions(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
$adapter = new Prometheus\Storage\Redis();
} elseif ($adapter === 'apc') {
$adapter = new Prometheus\Storage\APC();
Expand All @@ -22,4 +22,4 @@
$counter->incBy(6, ['blue']);

$pushGateway = new PushGateway('192.168.59.100:9091');
$pushGateway->push($registry, 'my_job', array('instance'=>'foo'));
$pushGateway->push($registry, 'my_job', ['instance'=>'foo']);
2 changes: 1 addition & 1 deletion examples/some_counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$adapter = $_GET['adapter'];

if ($adapter === 'redis') {
Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
Redis::setDefaultOptions(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
$adapter = new Prometheus\Storage\Redis();
} elseif ($adapter === 'apc') {
$adapter = new Prometheus\Storage\APC();
Expand Down
2 changes: 1 addition & 1 deletion examples/some_gauge.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
$adapter = $_GET['adapter'];

if ($adapter === 'redis') {
Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
Redis::setDefaultOptions(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
$adapter = new Prometheus\Storage\Redis();
} elseif ($adapter === 'apc') {
$adapter = new Prometheus\Storage\APC();
Expand Down
2 changes: 1 addition & 1 deletion examples/some_histogram.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
$adapter = $_GET['adapter'];

if ($adapter === 'redis') {
Redis::setDefaultOptions(array('host' => isset($_SERVER['REDIS_HOST']) ? $_SERVER['REDIS_HOST'] : '127.0.0.1'));
Redis::setDefaultOptions(['host' => $_SERVER['REDIS_HOST'] ?? '127.0.0.1']);
$adapter = new Prometheus\Storage\Redis();
} elseif ($adapter === 'apc') {
$adapter = new Prometheus\Storage\APC();
Expand Down
26 changes: 20 additions & 6 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
<phpunit colors="true" bootstrap="./tests/bootstrap.php">
<testsuites>
<testsuite name="Test Suite">
<directory>./tests/Test/Prometheus</directory>
</testsuite>
</testsuites>
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Unit">
<directory>./tests/Test/Prometheus</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src/Prometheus</directory>
</whitelist>
</filter>
</phpunit>
14 changes: 8 additions & 6 deletions src/Prometheus/Collector.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

namespace Prometheus;

use InvalidArgumentException;
Expand Down Expand Up @@ -35,7 +37,7 @@ abstract class Collector
* @param string $help
* @param array $labels
*/
public function __construct(Adapter $storageAdapter, $namespace, $name, $help, $labels = array())
public function __construct(Adapter $storageAdapter, $namespace, $name, $help, $labels = [])
{
$this->storageAdapter = $storageAdapter;
$metricName = ($namespace ? $namespace . '_' : '') . $name;
Expand All @@ -60,39 +62,39 @@ public abstract function getType();
/**
* @return string
*/
public function getName()
public function getName(): string
{
return $this->name;
}

/**
* @return array
*/
public function getLabelNames()
public function getLabelNames(): array
{
return $this->labels;
}

/**
* @return string
*/
public function getHelp()
public function getHelp(): string
{
return $this->help;
}

/**
* @return string
*/
public function getKey()
public function getKey(): string
{
return sha1($this->getName() . serialize($this->getLabelNames()));
}

/**
* @param $labels
*/
protected function assertLabelsAreDefinedCorrectly($labels)
protected function assertLabelsAreDefinedCorrectly($labels): void
{
if (count($labels) != count($this->labels)) {
throw new InvalidArgumentException(sprintf('Labels are not defined correctly: ', print_r($labels, true)));
Expand Down
32 changes: 17 additions & 15 deletions src/Prometheus/CollectorRegistry.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

namespace Prometheus;

use Prometheus\Exception\MetricNotFoundException;
Expand All @@ -21,17 +23,17 @@ class CollectorRegistry
/**
* @var Gauge[]
*/
private $gauges = array();
private $gauges = [];

/**
* @var Counter[]
*/
private $counters = array();
private $counters = [];

/**
* @var Histogram[]
*/
private $histograms = array();
private $histograms = [];

/**
* CollectorRegistry constructor.
Expand All @@ -45,7 +47,7 @@ public function __construct(Adapter $redisAdapter)
/**
* @return CollectorRegistry
*/
public static function getDefault()
public static function getDefault(): CollectorRegistry
{
if (!self::$defaultRegistry) {
return self::$defaultRegistry = new static(new Redis());
Expand All @@ -56,7 +58,7 @@ public static function getDefault()
/**
* @return MetricFamilySamples[]
*/
public function getMetricFamilySamples()
public function getMetricFamilySamples(): array
{
return $this->storageAdapter->collect();
}
Expand All @@ -69,7 +71,7 @@ public function getMetricFamilySamples()
* @return Gauge
* @throws MetricsRegistrationException
*/
public function registerGauge($namespace, $name, $help, $labels = array())
public function registerGauge($namespace, $name, $help, $labels = []): Gauge
{
$metricIdentifier = self::metricIdentifier($namespace, $name);
if (isset($this->gauges[$metricIdentifier])) {
Expand All @@ -91,7 +93,7 @@ public function registerGauge($namespace, $name, $help, $labels = array())
* @return Gauge
* @throws MetricNotFoundException
*/
public function getGauge($namespace, $name)
public function getGauge($namespace, $name): Gauge
{
$metricIdentifier = self::metricIdentifier($namespace, $name);
if (!isset($this->gauges[$metricIdentifier])) {
Expand All @@ -108,7 +110,7 @@ public function getGauge($namespace, $name)
* @return Gauge
* @throws MetricsRegistrationException
*/
public function getOrRegisterGauge($namespace, $name, $help, $labels = array())
public function getOrRegisterGauge($namespace, $name, $help, $labels = []): Gauge
{
try {
$gauge = $this->getGauge($namespace, $name);
Expand All @@ -126,7 +128,7 @@ public function getOrRegisterGauge($namespace, $name, $help, $labels = array())
* @return Counter
* @throws MetricsRegistrationException
*/
public function registerCounter($namespace, $name, $help, $labels = array())
public function registerCounter($namespace, $name, $help, $labels = []): Counter
{
$metricIdentifier = self::metricIdentifier($namespace, $name);
if (isset($this->counters[$metricIdentifier])) {
Expand All @@ -148,7 +150,7 @@ public function registerCounter($namespace, $name, $help, $labels = array())
* @return Counter
* @throws MetricNotFoundException
*/
public function getCounter($namespace, $name)
public function getCounter($namespace, $name): Counter
{
$metricIdentifier = self::metricIdentifier($namespace, $name);
if (!isset($this->counters[$metricIdentifier])) {
Expand All @@ -165,7 +167,7 @@ public function getCounter($namespace, $name)
* @return Counter
* @throws MetricsRegistrationException
*/
public function getOrRegisterCounter($namespace, $name, $help, $labels = array())
public function getOrRegisterCounter($namespace, $name, $help, $labels = []): Counter
{
try {
$counter = $this->getCounter($namespace, $name);
Expand All @@ -184,7 +186,7 @@ public function getOrRegisterCounter($namespace, $name, $help, $labels = array()
* @return Histogram
* @throws MetricsRegistrationException
*/
public function registerHistogram($namespace, $name, $help, $labels = array(), $buckets = null)
public function registerHistogram($namespace, $name, $help, $labels = [], $buckets = null): Histogram
{
$metricIdentifier = self::metricIdentifier($namespace, $name);
if (isset($this->histograms[$metricIdentifier])) {
Expand All @@ -207,7 +209,7 @@ public function registerHistogram($namespace, $name, $help, $labels = array(), $
* @return Histogram
* @throws MetricNotFoundException
*/
public function getHistogram($namespace, $name)
public function getHistogram($namespace, $name): Histogram
{
$metricIdentifier = self::metricIdentifier($namespace, $name);
if (!isset($this->histograms[$metricIdentifier])) {
Expand All @@ -225,7 +227,7 @@ public function getHistogram($namespace, $name)
* @return Histogram
* @throws MetricsRegistrationException
*/
public function getOrRegisterHistogram($namespace, $name, $help, $labels = array(), $buckets = null)
public function getOrRegisterHistogram($namespace, $name, $help, $labels = [], $buckets = null): Histogram
{
try {
$histogram = $this->getHistogram($namespace, $name);
Expand All @@ -240,7 +242,7 @@ public function getOrRegisterHistogram($namespace, $name, $help, $labels = array
* @param $name
* @return string
*/
private static function metricIdentifier($namespace, $name)
private static function metricIdentifier($namespace, $name): string
{
return $namespace . ":" . $name;
}
Expand Down
14 changes: 8 additions & 6 deletions src/Prometheus/Counter.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types = 1);

namespace Prometheus;

use Prometheus\Storage\Adapter;
Expand All @@ -10,15 +12,15 @@ class Counter extends Collector
/**
* @return string
*/
public function getType()
public function getType(): string
{
return self::TYPE;
}

/**
* @param array $labels e.g. ['status', 'opcode']
*/
public function inc(array $labels = array())
public function inc(array $labels = []): void
{
$this->incBy(1, $labels);
}
Expand All @@ -27,20 +29,20 @@ public function inc(array $labels = array())
* @param int $count e.g. 2
* @param array $labels e.g. ['status', 'opcode']
*/
public function incBy($count, array $labels = array())
public function incBy($count, array $labels = []): void
{
$this->assertLabelsAreDefinedCorrectly($labels);

$this->storageAdapter->updateCounter(
array(
[
'name' => $this->getName(),
'help' => $this->getHelp(),
'type' => $this->getType(),
'labelNames' => $this->getLabelNames(),
'labelValues' => $labels,
'value' => $count,
'command' => Adapter::COMMAND_INCREMENT_INTEGER
)
'command' => Adapter::COMMAND_INCREMENT_INTEGER,
]
);
}
}
1 change: 1 addition & 0 deletions src/Prometheus/Exception/MetricNotFoundException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Prometheus\Exception;

use Exception;
Expand Down
1 change: 1 addition & 0 deletions src/Prometheus/Exception/MetricsRegistrationException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Prometheus\Exception;

use Exception;
Expand Down
1 change: 1 addition & 0 deletions src/Prometheus/Exception/StorageException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Prometheus\Exception;

use Exception;
Expand Down
Loading

0 comments on commit 72ca2e7

Please sign in to comment.