Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Princemachiavelli authored Sep 15, 2024
2 parents d7e3e50 + 35d5a68 commit dd28936
Show file tree
Hide file tree
Showing 20 changed files with 914 additions and 13 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/blackbox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ jobs:
run: composer install --prefer-dist --no-interaction --no-suggest

- name: Compose Blackbox environment
run: docker-compose up -d
run: docker compose up -d
- name: Run Blackbox with APC
run: docker-compose run phpunit env ADAPTER=apc vendor/bin/phpunit tests/Test/
run: docker compose run phpunit env ADAPTER=apc vendor/bin/phpunit tests/Test/
- name: Run Blackbox with APCng
run: docker-compose run phpunit env ADAPTER=apcng vendor/bin/phpunit tests/Test/
run: docker compose run phpunit env ADAPTER=apcng vendor/bin/phpunit tests/Test/
- name: Run Blackbox with Redis
run: docker-compose run phpunit env ADAPTER=redis vendor/bin/phpunit tests/Test/
run: docker compose run phpunit env ADAPTER=redis vendor/bin/phpunit tests/Test/



1 change: 0 additions & 1 deletion .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ jobs:
fail-fast: false
matrix:
php:
- "8.0"
- "8.1"
- "8.2"
- "8.3"
Expand Down
23 changes: 21 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
fail-fast: true
matrix:
os: [ ubuntu-latest ]
php: [ 8.0, 8.1, 8.2, 8.3 ]
php: [ 8.1, 8.2, 8.3 ]
dependency-version: [ prefer-lowest, prefer-stable ]
redis-version: [ 5, 6, 7 ]

Expand All @@ -20,6 +20,11 @@ jobs:
REDIS_HOST: redis
# The default Redis port
REDIS_PORT: 6379
# MySQL
DB_DATABASE: test
DB_USER: root
DB_PASSWORD: root

steps:
- name: Checkout code
uses: actions/checkout@v2
Expand All @@ -40,9 +45,23 @@ jobs:

- name: Install dependencies
run: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest

- name: Start Redis
uses: supercharge/[email protected]
with:
redis-version: ${{ matrix.redis-version }}
- name: Execute tests

- name: Execute tests (PDO with Sqlite)
run: vendor/bin/phpunit

- name: Start MySQL
run: |
sudo /etc/init.d/mysql start
mysql -e "CREATE DATABASE IF NOT EXISTS $DB_DATABASE;" -u$DB_USER -p$DB_PASSWORD
- name: Execute PDO tests with MySQL
env:
TEST_PDO_DSN: 'mysql:host=localhost;dbname=test'
TEST_PDO_USERNAME: 'root'
TEST_PDO_PASSWORD: 'root'
run: vendor/bin/phpunit tests/Test/Prometheus/PDO
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ $registry = new CollectorRegistry(new APC());
```
(see the `README.APCng.md` file for more details)

Using the PDO storage:
```php
$registry = new CollectorRegistry(new \PDO('mysql:host=localhost;dbname=prometheus', 'username', 'password'));
or
$registry = new CollectorRegistry(new \PDO('sqlite::memory:'));
```

### Advanced Usage

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"suggest": {
"ext-redis": "Required if using Redis.",
"ext-apc": "Required if using APCu.",
"ext-pdo": "Required if using PDO.",
"promphp/prometheus_push_gateway_php": "An easy client for using Prometheus PushGateway.",
"symfony/polyfill-apcu": "Required if you use APCu on PHP8.0+"
},
Expand Down
19 changes: 17 additions & 2 deletions src/Prometheus/RenderTextFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
namespace Prometheus;

use RuntimeException;
use Throwable;

class RenderTextFormat implements RendererInterface
{
const MIME_TYPE = 'text/plain; version=0.0.4';

/**
* @param MetricFamilySamples[] $metrics
* @param bool $silent If true, render value errors as comments instead of throwing them.
* @return string
*/
public function render(array $metrics): string
public function render(array $metrics, bool $silent = false): string
{
usort($metrics, function (MetricFamilySamples $a, MetricFamilySamples $b): int {
return strcmp($a->getName(), $b->getName());
Expand All @@ -25,7 +27,20 @@ public function render(array $metrics): string
$lines[] = "# HELP " . $metric->getName() . " {$metric->getHelp()}";
$lines[] = "# TYPE " . $metric->getName() . " {$metric->getType()}";
foreach ($metric->getSamples() as $sample) {
$lines[] = $this->renderSample($metric, $sample);
try {
$lines[] = $this->renderSample($metric, $sample);
} catch (Throwable $e) {
// Redis and RedisNg allow samples with mismatching labels to be stored, which could cause ValueError
// to be thrown when rendering. If this happens, users can decide whether to ignore the error or not.
// These errors will normally disappear after the storage is flushed.
if (!$silent) {
throw $e;
}

$lines[] = "# Error: {$e->getMessage()}";
$lines[] = "# Labels: " . json_encode(array_merge($metric->getLabelNames(), $sample->getLabelNames()));
$lines[] = "# Values: " . json_encode(array_merge($sample->getLabelValues()));
}
}
}
return implode("\n", $lines) . "\n";
Expand Down
5 changes: 5 additions & 0 deletions src/Prometheus/Sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Prometheus;

use function is_infinite;

class Sample
{
/**
Expand Down Expand Up @@ -67,6 +69,9 @@ public function getLabelValues(): array
*/
public function getValue(): string
{
if (is_float($this->value) && is_infinite($this->value)) {
return $this->value > 0 ? '+Inf' : '-Inf';
}
return (string) $this->value;
}

Expand Down
15 changes: 15 additions & 0 deletions src/Prometheus/Storage/APC.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ public function updateHistogram(array $data): void
$old = apcu_fetch($sumKey);
if ($old !== false) {
$done = apcu_cas($sumKey, $old, $this->toBinaryRepresentationAsInteger($this->fromBinaryRepresentationAsInteger($old) + $data['value']));
} else {
$new = apcu_add($sumKey, $this->toBinaryRepresentationAsInteger(0));

// If sum does not exist, assume a new histogram and store the metadata
if ($new) {
apcu_store($this->metaKey($data), json_encode($this->metaData($data)));
}
}
}

Expand Down Expand Up @@ -160,6 +167,11 @@ public function updateGauge(array $data): void
$old = apcu_fetch($valueKey);
if ($old !== false) {
$done = apcu_cas($valueKey, $old, $this->toBinaryRepresentationAsInteger($this->fromBinaryRepresentationAsInteger($old) + $data['value']));
} else {
$new = apcu_add($valueKey, $this->toBinaryRepresentationAsInteger(0));
if ($new) {
apcu_store($this->metaKey($data), json_encode($this->metaData($data)));
}
}
}
}
Expand All @@ -183,6 +195,9 @@ public function updateCounter(array $data): void
$old = apcu_fetch($valueKey);
if ($old !== false) {
$done = apcu_cas($valueKey, $old, $this->toBinaryRepresentationAsInteger($this->fromBinaryRepresentationAsInteger($old) + $data['value']));
} else {
apcu_add($this->valueKey($data), 0);
apcu_store($this->metaKey($data), json_encode($this->metaData($data)));
}
}
}
Expand Down
Loading

0 comments on commit dd28936

Please sign in to comment.