From d92165f343fea2b18784d40d9665f3b42cd8f362 Mon Sep 17 00:00:00 2001 From: Bar Nehemia Date: Mon, 6 Nov 2023 15:02:09 +0200 Subject: [PATCH] LP_Reporter: Add suffix for metrics path. --- pyformance/reporters/influx.py | 1 + pyformance/reporters/line_protocol_reporter.py | 15 +++++++++++++-- tests/test_line_protocol_reporter.py | 18 +++++++++++++++++- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/pyformance/reporters/influx.py b/pyformance/reporters/influx.py index 450ffed..9614b14 100644 --- a/pyformance/reporters/influx.py +++ b/pyformance/reporters/influx.py @@ -170,6 +170,7 @@ def report_from_files(self, files_path: Path) -> None: for file in files: with open(file, "r") as metrics_file: url = self._get_url() + LOG.debug(f"Reporting file {metrics_file.name} with data: {metrics_file.read()}") if self._try_send(url, metrics_file.read()): self.reported_files.append(file) diff --git a/pyformance/reporters/line_protocol_reporter.py b/pyformance/reporters/line_protocol_reporter.py index e1c8d62..6a72eaf 100644 --- a/pyformance/reporters/line_protocol_reporter.py +++ b/pyformance/reporters/line_protocol_reporter.py @@ -26,8 +26,9 @@ def __init__( self, registry: MetricsRegistry = None, reporting_interval: int = 30, - path: str = "/tmp/metrics", prefix: str = "", + path: str | None = None, + path_suffix: str | None = None, clock: time = None, global_tags: dict = None, reporting_precision = ReportingPrecision.SECONDS, @@ -38,7 +39,7 @@ def __init__( coarse precision may result in significant improvements in compression and vice versa. """ super(LineProtocolReporter, self).__init__(registry, reporting_interval, clock) - self.path = path + self.path = self._set_path(path, path_suffix) self.prefix = prefix if not os.path.exists(self.path): @@ -131,6 +132,16 @@ def _stringify_tags(self, metric) -> str: return "" + @staticmethod + def _set_path(path: str | None, path_suffix: str | None) -> str: + if not path: + path = f"/tmp/metrics" + if path_suffix: + path += f"/{path_suffix}" + + os.environ["METRICS_REPORTER_FOLDER_PATH"] = path + return path + def _format_field_value(value) -> str: if isinstance(value, MarkInt): return f"{value.value}i" diff --git a/tests/test_line_protocol_reporter.py b/tests/test_line_protocol_reporter.py index 96962e7..368c801 100644 --- a/tests/test_line_protocol_reporter.py +++ b/tests/test_line_protocol_reporter.py @@ -34,7 +34,8 @@ def test_report_now_writes_to_file(self) -> None: files = [f for f in Path(reporter.path).glob("*.txt")] self.assertEqual(1, len(files)) expected_file_path = f"{reporter.path}/{files[0].name}" - expected_lines = open(expected_file_path).read() + with open(expected_file_path) as f: + expected_lines = f.read() self.assertEqual(expected_lines, "test-counter count=1 1234567890") def test_get_table_name_returns_metric_key_when_prefix_empty(self) -> None: @@ -47,6 +48,21 @@ def test_get_table_name_returns_prefixed_metric_key_when_prefix_not_empty(self) table_name = reporter._get_table_name("metric_key") self.assertEqual(table_name, "prefix.metric_key") + def test_path_suffix(self) -> None: + reporter = LineProtocolReporter(registry=self.registry, clock=self.clock, path=self.path, path_suffix="suffix") + timestamp = 1234567890 + counter = reporter.registry.counter("test-counter") + counter.inc() + reporter.report_now(timestamp=timestamp) + + self.assertEqual(reporter.path, f"{self.path}/suffix") + files = [f for f in Path(reporter.path).glob("*.txt")] + self.assertEqual(1, len(files)) + expected_file_path = f"{reporter.path}/{files[0].name}" + with open(expected_file_path) as f: + expected_lines = f.read() + self.assertEqual(expected_lines, f"test-counter count=1 {timestamp}") + def test_stringify_values_returns_correct_string(self) -> None: metric_values = {"field1": 10, "field2": "value"} expected_string = "field1=10,field2=\"value\""