Skip to content

Commit b10ac02

Browse files
committed
file-based import-export feature
1 parent 6d97efe commit b10ac02

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

src/Metrics/Exporter/PrometheusExporter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111
class PrometheusExporter extends Exporter
1212
{
13+
public function toFile(string $path, string $prefix = '', array $labels = []): void
14+
{
15+
file_put_contents($path, $this->toString($prefix, $labels));
16+
}
17+
1318
public function toString(string $prefix = '', array $labels = []): string
1419
{
1520
$info = [];

src/Metrics/Importer/PrometheusImporter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ class PrometheusImporter
1212
public function __construct(private Registry $registry, private Info $info)
1313
{
1414
}
15+
public function fromFile(string $path, string $prefix = ''): self
16+
{
17+
return $this->fromString(file_get_contents($path), $prefix);
18+
}
1519

1620
public function fromString(string $string, string $prefix = ''): self
1721
{

tests/Metrics/ImporterTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ public function test(): void
3333
$registry2 = new Registry();
3434
$info2 = new Info();
3535
$importer = new PrometheusImporter($registry2, $info2);
36-
$importer->fromString($exporter->toString('tester_'), 'tester_');
36+
$filename = '/tmp/' . bin2hex(random_bytes(32));
37+
38+
$exporter->toFile($filename, 'tester_');
39+
$importer->fromFile($filename, 'tester_');
40+
41+
unlink($filename);
3742

3843
$keys = [
3944
['memory_usage', []],

0 commit comments

Comments
 (0)