Skip to content

Commit

Permalink
Merge pull request #14 from Lightricks/add-line-protocol-reporter
Browse files Browse the repository at this point in the history
LP_Reporter: Add suffix for metrics path.
  • Loading branch information
BarNehemia authored Nov 6, 2023
2 parents 8847a99 + d92165f commit de29808
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions pyformance/reporters/influx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
15 changes: 13 additions & 2 deletions pyformance/reporters/line_protocol_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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):
Expand Down Expand Up @@ -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"
Expand Down
18 changes: 17 additions & 1 deletion tests/test_line_protocol_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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\""
Expand Down

0 comments on commit de29808

Please sign in to comment.