From b10ac0223be7162f293b453bb9938fcfd51723fa Mon Sep 17 00:00:00 2001 From: dmitry krokhin Date: Tue, 17 Aug 2021 20:18:52 +0300 Subject: [PATCH] file-based import-export feature --- src/Metrics/Exporter/PrometheusExporter.php | 5 +++++ src/Metrics/Importer/PrometheusImporter.php | 4 ++++ tests/Metrics/ImporterTest.php | 7 ++++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Metrics/Exporter/PrometheusExporter.php b/src/Metrics/Exporter/PrometheusExporter.php index 4bdc437..df4c1fa 100644 --- a/src/Metrics/Exporter/PrometheusExporter.php +++ b/src/Metrics/Exporter/PrometheusExporter.php @@ -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 = []; diff --git a/src/Metrics/Importer/PrometheusImporter.php b/src/Metrics/Importer/PrometheusImporter.php index 0c1fdfa..8a5a52e 100644 --- a/src/Metrics/Importer/PrometheusImporter.php +++ b/src/Metrics/Importer/PrometheusImporter.php @@ -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 { diff --git a/tests/Metrics/ImporterTest.php b/tests/Metrics/ImporterTest.php index 53af9f3..e48d3d2 100644 --- a/tests/Metrics/ImporterTest.php +++ b/tests/Metrics/ImporterTest.php @@ -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', []],