Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(taps): Ensure metric tags coming from stream context can be JSON-serialized #1238

Merged
merged 2 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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