Skip to content

Commit

Permalink
fix: Ensure metric tags coming from stream context can be JSON-serial…
Browse files Browse the repository at this point in the history
…ized
  • Loading branch information
edgarrmondragon committed Dec 2, 2022
1 parent 18aca68 commit 5c8987d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion singer_sdk/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def to_json(self) -> str:
Returns:
A JSON object.
"""
return json.dumps(asdict(self))
return json.dumps(asdict(self), default=str)


def log(logger: logging.Logger, point: Point) -> None:
Expand Down
15 changes: 15 additions & 0 deletions tests/core/test_metrics.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import logging
import time
from textwrap import dedent

import pytest

from singer_sdk import metrics


class CustomObject:
def __init__(self, name: str, value: int):
self.name = name
self.value = value

def __str__(self) -> str:
return f"{self.name}={self.value}"


def test_meter():
class _MyMeter(metrics.Meter):
def __enter__(self):
Expand All @@ -28,10 +38,13 @@ def __exit__(self, exc_type, exc_val, exc_tb):

def test_record_counter(caplog: pytest.LogCaptureFixture):
caplog.set_level(logging.INFO, logger=metrics.METRICS_LOGGER_NAME)
custom_object = CustomObject("test", 1)

with metrics.record_counter(
"test_stream",
endpoint="test_endpoint",
custom_tag="pytest",
custom_obj=custom_object,
) as counter:
for _ in range(100):
counter.last_log_time = 0
Expand All @@ -46,6 +59,7 @@ def test_record_counter(caplog: pytest.LogCaptureFixture):
for record in caplog.records:
assert record.levelname == "INFO"
assert record.msg == "INFO METRIC: %s"
assert "test=1" in record.message

point: metrics.Point[int] = record.args[0]
assert point.metric_type == "counter"
Expand All @@ -54,6 +68,7 @@ def test_record_counter(caplog: pytest.LogCaptureFixture):
metrics.Tag.STREAM: "test_stream",
metrics.Tag.ENDPOINT: "test_endpoint",
"custom_tag": "pytest",
"custom_obj": custom_object,
}

total += point.value
Expand Down

0 comments on commit 5c8987d

Please sign in to comment.