-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Basis\Telemetry\Metrics\Importer; | ||
|
||
use Basis\Telemetry\Metrics\Info; | ||
use Basis\Telemetry\Metrics\Registry; | ||
|
||
class PrometheusImporter | ||
{ | ||
public function __construct(private Registry $registry, private Info $info) | ||
{ | ||
} | ||
|
||
public function fromString(string $string, string $prefix = ''): self | ||
{ | ||
$info = []; | ||
$values = []; | ||
|
||
foreach (explode(PHP_EOL, $string) as $line) { | ||
if (substr($line, 0, 1) == '#') { | ||
[$_, $type, $nick, $value] = explode(' ', $line, 4); | ||
if ($prefix && strpos($nick, $prefix) === 0) { | ||
$nick = substr($nick, strlen($prefix)); | ||
} | ||
if (!array_key_exists($nick, $info)) { | ||
$info[$nick] = compact('nick'); | ||
} | ||
$info[$nick][strtolower($type)] = $value; | ||
} else { | ||
$labels = []; | ||
[$nick, $value] = explode(' ', $line); | ||
if ($prefix && strpos($nick, $prefix) === 0) { | ||
$nick = substr($nick, strlen($prefix)); | ||
} | ||
if (strpos($nick, '{') !== false) { | ||
[$nick, $postfix] = explode('{', trim($nick, '}'), 2); | ||
foreach (explode(',', $postfix) as $chunk) { | ||
[$k, $v] = explode('=', $chunk, 2); | ||
$labels[$k] = trim($v, '"'); | ||
} | ||
} | ||
if ("" . intval($value) == $value) { | ||
$value = intval($value); | ||
} else { | ||
$value = floatval($value); | ||
} | ||
$values[] = compact('nick', 'labels', 'value'); | ||
} | ||
} | ||
|
||
foreach ($info as $row) { | ||
$this->info->set($row['nick'], $row['help'], $row['type']); | ||
} | ||
|
||
foreach ($values as $row) { | ||
$this->registry->set($row['nick'], $row['value'], $row['labels']); | ||
} | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Metrics; | ||
|
||
use Basis\Telemetry\Metrics\Exporter; | ||
use Basis\Telemetry\Metrics\Exporter\PrometheusExporter; | ||
use Basis\Telemetry\Metrics\Importer\PrometheusImporter; | ||
use Basis\Telemetry\Metrics\Info; | ||
use Basis\Telemetry\Metrics\Registry; | ||
use Basis\Telemetry\Metrics\Type; | ||
use DomainException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ImporterTest extends TestCase | ||
{ | ||
public function test(): void | ||
{ | ||
$registry = new Registry(); | ||
$registry->set('memory_usage', memory_get_usage(true)); | ||
$registry->set('request_counter', 1, ['user' => '1']); | ||
$registry->set('request_counter', 2, ['user' => '2']); | ||
$registry->set('uptime', 30); | ||
|
||
$info = new Info(); | ||
$info->set('memory_usage', 'Memory usage'); | ||
$info->set('request_counter', 'Request Counter', Type::COUNTER); | ||
$info->set('uptime', 'Uptime', Type::COUNTER); | ||
|
||
$exporter = new PrometheusExporter($registry, $info); | ||
|
||
$registry2 = new Registry(); | ||
$info2 = new Info(); | ||
$importer = new PrometheusImporter($registry2, $info2); | ||
$importer->fromString($exporter->toString('tester_'), 'tester_'); | ||
|
||
$keys = [ | ||
['memory_usage', []], | ||
['request_counter', ['user' => '1']], | ||
['request_counter', ['user' => '2']], | ||
['uptime'], | ||
]; | ||
|
||
foreach ($keys as $key) { | ||
$this->assertSame($registry->get(...$key), $registry2->get(...$key)); | ||
$this->assertSame($info->get($key[0]), $info2->get($key[0])); | ||
} | ||
} | ||
} |