Skip to content
Open
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
25 changes: 25 additions & 0 deletions packages/traceloop-sdk/tests/test_fetcher_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import logging
from unittest.mock import patch

from traceloop.sdk.fetcher import Fetcher


def test_api_post_logs_error_instead_of_printing(caplog, capsys):
fetcher = Fetcher(base_url="http://example.invalid", api_key="test-key")

with patch(
"traceloop.sdk.fetcher.post_url",
side_effect=RuntimeError("boom"),
):
with caplog.at_level(logging.ERROR):
fetcher.api_post("some-endpoint", {"payload": "x"})

error_records = [r for r in caplog.records if r.levelno == logging.ERROR]
assert error_records, "expected an ERROR log record from Fetcher.api_post"

message = error_records[-1].getMessage()
assert "some-endpoint" in message
assert "boom" in message

captured = capsys.readouterr()
assert "boom" not in captured.out
2 changes: 1 addition & 1 deletion packages/traceloop-sdk/traceloop/sdk/fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def api_post(self, api: str, body: Dict[str, typing.Any]) -> None:
try:
post_url(f"{self._base_url}/v2/{api}", self._api_key, body)
except Exception as e:
print(e)
logging.error("Failed to post to %s: %s", api, e)


class RetryIfServerError(retry_if_exception):
Expand Down