Skip to content

Commit

Permalink
metrics importer implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
nekufa committed Aug 17, 2021
1 parent 75a976e commit 6d97efe
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/Metrics/Importer/PrometheusImporter.php
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;
}
}
50 changes: 50 additions & 0 deletions tests/Metrics/ImporterTest.php
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]));
}
}
}

0 comments on commit 6d97efe

Please sign in to comment.