Repository: meltano/sdk
Pull request #1574 (2023-04-02 04:09:11)

fix: Conform metric field `type` to Singer spec
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`.


<!-- readthedocs-preview meltano-sdk start -->
----
:books: Documentation preview :books:: https://meltano-sdk--1574.org.readthedocs.build/en/1574/

<!-- readthedocs-preview meltano-sdk end -->

codecov[bot]: ## [Codecov](https://codecov.io/gh/meltano/sdk/pull/1574?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=meltano) Report
> Merging [#1574](https://codecov.io/gh/meltano/sdk/pull/1574?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=meltano) (bd442b6) into [main](https://codecov.io/gh/meltano/sdk/commit/304d1232f286a316567786846c0fa673e27d4323?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=meltano) (304d123) will **not change** coverage.
> The diff coverage is `100.00%`.

```diff
@@           Coverage Diff           @@
##             main    #1574   +/-   ##
=======================================
  Coverage   85.59%   85.59%           
=======================================
  Files          57       57           
  Lines        4726     4726           
  Branches      807      807           
=======================================
  Hits         4045     4045           
  Misses        488      488           
  Partials      193      193           
```


| [Impacted Files](https://codecov.io/gh/meltano/sdk/pull/1574?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=meltano) | Coverage Δ | |
|---|---|---|
| [singer\_sdk/metrics.py](https://codecov.io/gh/meltano/sdk/pull/1574?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=meltano#diff-c2luZ2VyX3Nkay9tZXRyaWNzLnB5) | `96.00% <100.00%> (ø)` | |

:mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=meltano)


Patch:
diff --git a/docs/implementation/logging.md b/docs/implementation/logging.md
index ea9e186b..da20a578 100644
--- a/docs/implementation/logging.md
+++ b/docs/implementation/logging.md
@@ -51,7 +51,7 @@ version: 1
 disable_existing_loggers: false
 formatters:
   metrics:
-    format: "{asctime} {message}"
+    format: "{asctime} {levelname} {message}"
     style: "{"
 handlers:
   metrics:
diff --git a/singer_sdk/metrics.py b/singer_sdk/metrics.py
index 1c69a368..f61d3e1e 100644
--- a/singer_sdk/metrics.py
+++ b/singer_sdk/metrics.py
@@ -8,7 +8,7 @@ import json
 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
@@ -79,7 +79,15 @@ class Point(Generic[_TVal]):
         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:
@@ -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):


Test patch:
diff --git a/tests/core/test_metrics.py b/tests/core/test_metrics.py
index 5eb8fa50..a9d6b4d2 100644
--- a/tests/core/test_metrics.py
+++ b/tests/core/test_metrics.py
@@ -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]
@@ -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"