Skip to content

Commit

Permalink
file-based import-export feature
Browse files Browse the repository at this point in the history
  • Loading branch information
nekufa committed Aug 17, 2021
1 parent 6d97efe commit b10ac02
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Metrics/Exporter/PrometheusExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

class PrometheusExporter extends Exporter
{
public function toFile(string $path, string $prefix = '', array $labels = []): void
{
file_put_contents($path, $this->toString($prefix, $labels));
}

public function toString(string $prefix = '', array $labels = []): string
{
$info = [];
Expand Down
4 changes: 4 additions & 0 deletions src/Metrics/Importer/PrometheusImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class PrometheusImporter
public function __construct(private Registry $registry, private Info $info)
{
}
public function fromFile(string $path, string $prefix = ''): self
{
return $this->fromString(file_get_contents($path), $prefix);
}

public function fromString(string $string, string $prefix = ''): self
{
Expand Down
7 changes: 6 additions & 1 deletion tests/Metrics/ImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ public function test(): void
$registry2 = new Registry();
$info2 = new Info();
$importer = new PrometheusImporter($registry2, $info2);
$importer->fromString($exporter->toString('tester_'), 'tester_');
$filename = '/tmp/' . bin2hex(random_bytes(32));

$exporter->toFile($filename, 'tester_');
$importer->fromFile($filename, 'tester_');

unlink($filename);

$keys = [
['memory_usage', []],
Expand Down

0 comments on commit b10ac02

Please sign in to comment.