Skip to content

Commit de29808

Browse files
author
Bar Nehemia
authored
Merge pull request #14 from Lightricks/add-line-protocol-reporter
LP_Reporter: Add suffix for metrics path.
2 parents 8847a99 + d92165f commit de29808

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

pyformance/reporters/influx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ def report_from_files(self, files_path: Path) -> None:
170170
for file in files:
171171
with open(file, "r") as metrics_file:
172172
url = self._get_url()
173+
LOG.debug(f"Reporting file {metrics_file.name} with data: {metrics_file.read()}")
173174
if self._try_send(url, metrics_file.read()):
174175
self.reported_files.append(file)
175176

pyformance/reporters/line_protocol_reporter.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ def __init__(
2626
self,
2727
registry: MetricsRegistry = None,
2828
reporting_interval: int = 30,
29-
path: str = "/tmp/metrics",
3029
prefix: str = "",
30+
path: str | None = None,
31+
path_suffix: str | None = None,
3132
clock: time = None,
3233
global_tags: dict = None,
3334
reporting_precision = ReportingPrecision.SECONDS,
@@ -38,7 +39,7 @@ def __init__(
3839
coarse precision may result in significant improvements in compression and vice versa.
3940
"""
4041
super(LineProtocolReporter, self).__init__(registry, reporting_interval, clock)
41-
self.path = path
42+
self.path = self._set_path(path, path_suffix)
4243
self.prefix = prefix
4344

4445
if not os.path.exists(self.path):
@@ -131,6 +132,16 @@ def _stringify_tags(self, metric) -> str:
131132

132133
return ""
133134

135+
@staticmethod
136+
def _set_path(path: str | None, path_suffix: str | None) -> str:
137+
if not path:
138+
path = f"/tmp/metrics"
139+
if path_suffix:
140+
path += f"/{path_suffix}"
141+
142+
os.environ["METRICS_REPORTER_FOLDER_PATH"] = path
143+
return path
144+
134145
def _format_field_value(value) -> str:
135146
if isinstance(value, MarkInt):
136147
return f"{value.value}i"

tests/test_line_protocol_reporter.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ def test_report_now_writes_to_file(self) -> None:
3434
files = [f for f in Path(reporter.path).glob("*.txt")]
3535
self.assertEqual(1, len(files))
3636
expected_file_path = f"{reporter.path}/{files[0].name}"
37-
expected_lines = open(expected_file_path).read()
37+
with open(expected_file_path) as f:
38+
expected_lines = f.read()
3839
self.assertEqual(expected_lines, "test-counter count=1 1234567890")
3940

4041
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)
4748
table_name = reporter._get_table_name("metric_key")
4849
self.assertEqual(table_name, "prefix.metric_key")
4950

51+
def test_path_suffix(self) -> None:
52+
reporter = LineProtocolReporter(registry=self.registry, clock=self.clock, path=self.path, path_suffix="suffix")
53+
timestamp = 1234567890
54+
counter = reporter.registry.counter("test-counter")
55+
counter.inc()
56+
reporter.report_now(timestamp=timestamp)
57+
58+
self.assertEqual(reporter.path, f"{self.path}/suffix")
59+
files = [f for f in Path(reporter.path).glob("*.txt")]
60+
self.assertEqual(1, len(files))
61+
expected_file_path = f"{reporter.path}/{files[0].name}"
62+
with open(expected_file_path) as f:
63+
expected_lines = f.read()
64+
self.assertEqual(expected_lines, f"test-counter count=1 {timestamp}")
65+
5066
def test_stringify_values_returns_correct_string(self) -> None:
5167
metric_values = {"field1": 10, "field2": "value"}
5268
expected_string = "field1=10,field2=\"value\""

0 commit comments

Comments
 (0)