Skip to content

Commit

Permalink
fix: Conform metric field type to Singer spec (#1574)
Browse files Browse the repository at this point in the history
According to the [Singer
Spec](https://github.com/singer-io/getting-started/blob/master/docs/SYNC_MODE.md#metric-messages),
the correct field name is `type` and not `metric_type`.
  • Loading branch information
edgarrmondragon authored Apr 3, 2023
1 parent 304d123 commit d809c6e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/implementation/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ version: 1
disable_existing_loggers: false
formatters:
metrics:
format: "{asctime} {message}"
format: "{asctime} {levelname} {message}"
style: "{"
handlers:
metrics:
Expand Down
14 changes: 11 additions & 3 deletions singer_sdk/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import logging
import logging.config
import os
from dataclasses import asdict, dataclass, field
from dataclasses import dataclass, field
from pathlib import Path
from time import time
from typing import TYPE_CHECKING, Any, Generic, Mapping, TypeVar
Expand Down Expand Up @@ -79,7 +79,15 @@ def to_json(self) -> str:
Returns:
A JSON object.
"""
return json.dumps(asdict(self), default=str)
return json.dumps(
{
"type": self.metric_type,
"metric": self.metric.value,
"value": self.value,
"tags": self.tags,
},
default=str,
)


def log(logger: logging.Logger, point: Point) -> None:
Expand All @@ -89,7 +97,7 @@ def log(logger: logging.Logger, point: Point) -> None:
logger: An logger instance.
point: A measurement.
"""
logger.info("INFO METRIC: %s", point)
logger.info("METRIC: %s", point)


class Meter(metaclass=abc.ABCMeta):
Expand Down
4 changes: 2 additions & 2 deletions tests/core/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +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 record.msg == "METRIC: %s"
assert "test=1" in record.message

point: metrics.Point[int] = record.args[0]
Expand Down Expand Up @@ -89,7 +89,7 @@ def test_sync_timer(caplog: pytest.LogCaptureFixture):

record = caplog.records[0]
assert record.levelname == "INFO"
assert record.msg == "INFO METRIC: %s"
assert record.msg == "METRIC: %s"

point: metrics.Point[float] = record.args[0]
assert point.metric_type == "timer"
Expand Down

0 comments on commit d809c6e

Please sign in to comment.