Skip to content

Commit

Permalink
Merged in develop (pull request #2)
Browse files Browse the repository at this point in the history
Bump to version 1.0.0 (API is the same, but requires php 7.1)

Approved-by: Andre Ferraz <[email protected]>
Approved-by: DuShaun Alderson-Claeys <[email protected]>
Approved-by: Robin Shimield <[email protected]>
  • Loading branch information
Daniel Noel-Davies committed Jul 23, 2019
2 parents 215c9ae + 8c7fa91 commit 8caf681
Show file tree
Hide file tree
Showing 44 changed files with 1,025 additions and 783 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Also look at the [examples](examples).

### Dependencies

* PHP 5.6
* PHP 7.1
* PHP Redis extension
* PHP APCu extension
* [Composer](https://getcomposer.org/doc/00-intro.md#installation-linux-unix-osx)
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@
}
],
"require": {
"php": ">=5.6.3",
"php": "^7.1",
"ext-json": "*",
"guzzlehttp/guzzle": "^6.2",
"symfony/polyfill-apcu": "^1.6"
},
"require-dev": {
"phpunit/phpunit": "4.1.0"
"phpunit/phpunit": "^7.5"
},
"suggest": {
"ext-redis": "Required if using Redis.",
"ext-apc": "Required if using APCu."
},
"autoload": {
"psr-0": {
"Prometheus\\": "src/"
"psr-4": {
"Prometheus\\": "src/Prometheus/"
}
},
"autoload-dev": {
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
7 changes: 4 additions & 3 deletions examples/pushgateway.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php
require __DIR__ . '/../vendor/autoload.php';

use Prometheus\PushGateway;
use Prometheus\Storage\Redis;
use Prometheus\CollectorRegistry;

$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 @@ -20,5 +21,5 @@
$counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']);
$counter->incBy(6, ['blue']);

$pushGateway = new \Prometheus\PushGateway('192.168.59.100:9091');
$pushGateway->push($registry, 'my_job', array('instance'=>'foo'));
$pushGateway = new PushGateway('192.168.59.100:9091');
$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>
48 changes: 38 additions & 10 deletions src/Prometheus/Collector.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
<?php
declare(strict_types=1);

namespace Prometheus;


use InvalidArgumentException;
use Prometheus\Storage\Adapter;

abstract class Collector
{
const RE_METRIC_LABEL_NAME = '/^[a-zA-Z_:][a-zA-Z0-9_:]*$/';

/**
* @var Adapter
*/
protected $storageAdapter;

/**
* @var string
*/
protected $name;

/**
* @var string
*/
protected $help;

/**
* @var array
*/
protected $labels;

/**
Expand All @@ -21,18 +37,18 @@ 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;
if (!preg_match(self::RE_METRIC_LABEL_NAME, $metricName)) {
throw new \InvalidArgumentException("Invalid metric name: '" . $metricName . "'");
throw new InvalidArgumentException("Invalid metric name: '" . $metricName . "'");
}
$this->name = $metricName;
$this->help = $help;
foreach ($labels as $label) {
if (!preg_match(self::RE_METRIC_LABEL_NAME, $label)) {
throw new \InvalidArgumentException("Invalid label name: '" . $label . "'");
throw new InvalidArgumentException("Invalid label name: '" . $label . "'");
}
}
$this->labels = $labels;
Expand All @@ -43,33 +59,45 @@ public function __construct(Adapter $storageAdapter, $namespace, $name, $help, $
*/
public abstract function getType();

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

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

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

public function getKey()
/**
* @return string
*/
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)));
throw new InvalidArgumentException(sprintf('Labels are not defined correctly: ', print_r($labels, true)));
}
}
}
Loading

0 comments on commit 8caf681

Please sign in to comment.