|
| 1 | +"""before_send hook and sampling (conformance C22/C23).""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from logtide_sdk import ClientOptions, LogTideClient |
| 6 | + |
| 7 | + |
| 8 | +def make_client(**kwargs): |
| 9 | + return LogTideClient( |
| 10 | + ClientOptions(api_url="http://localhost:8080", api_key="lp_k", service="svc", **kwargs) |
| 11 | + ) |
| 12 | + |
| 13 | + |
| 14 | +def test_before_send_can_mutate(mocker): |
| 15 | + def scrub(entry): |
| 16 | + entry.metadata["password"] = "[redacted]" |
| 17 | + return entry |
| 18 | + |
| 19 | + client = make_client(before_send=scrub) |
| 20 | + try: |
| 21 | + client.info("login", {"password": "hunter2"}) |
| 22 | + assert client._buffer[-1].metadata["password"] == "[redacted]" |
| 23 | + finally: |
| 24 | + client._closed = True |
| 25 | + |
| 26 | + |
| 27 | +def test_before_send_can_drop(): |
| 28 | + client = make_client(before_send=lambda entry: None) |
| 29 | + try: |
| 30 | + client.info("dropped") |
| 31 | + assert len(client._buffer) == 0 |
| 32 | + assert client.get_metrics().logs_sent == 0 |
| 33 | + finally: |
| 34 | + client._closed = True |
| 35 | + |
| 36 | + |
| 37 | +def test_before_send_exception_does_not_break_capture(): |
| 38 | + def broken(entry): |
| 39 | + raise RuntimeError("hook bug") |
| 40 | + |
| 41 | + client = make_client(before_send=broken) |
| 42 | + try: |
| 43 | + client.info("survives") |
| 44 | + # a buggy hook must not lose the entry or raise to the caller |
| 45 | + assert len(client._buffer) == 1 |
| 46 | + finally: |
| 47 | + client._closed = True |
| 48 | + |
| 49 | + |
| 50 | +def test_sample_rate_zero_sends_nothing(): |
| 51 | + client = make_client(sample_rate=0.0) |
| 52 | + try: |
| 53 | + for _ in range(20): |
| 54 | + client.info("nope") |
| 55 | + assert len(client._buffer) == 0 |
| 56 | + finally: |
| 57 | + client._closed = True |
| 58 | + |
| 59 | + |
| 60 | +def test_sample_rate_one_sends_everything(): |
| 61 | + client = make_client(sample_rate=1.0) |
| 62 | + try: |
| 63 | + for _ in range(5): |
| 64 | + client.info("yes") |
| 65 | + assert len(client._buffer) == 5 |
| 66 | + finally: |
| 67 | + client._closed = True |
| 68 | + |
| 69 | + |
| 70 | +def test_sample_rate_validation(): |
| 71 | + with pytest.raises(ValueError): |
| 72 | + ClientOptions(api_url="x://h", api_key="k", sample_rate=1.5) |
| 73 | + with pytest.raises(ValueError): |
| 74 | + ClientOptions(api_url="x://h", api_key="k", sample_rate=-0.1) |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.asyncio |
| 78 | +async def test_async_before_send_and_sampling(): |
| 79 | + from logtide_sdk.async_client import AsyncLogTideClient |
| 80 | + |
| 81 | + client = AsyncLogTideClient( |
| 82 | + ClientOptions( |
| 83 | + api_url="http://localhost:8080", |
| 84 | + api_key="lp_k", |
| 85 | + service="svc", |
| 86 | + before_send=lambda e: None, |
| 87 | + ) |
| 88 | + ) |
| 89 | + try: |
| 90 | + await client.info("dropped") |
| 91 | + assert len(client._buffer) == 0 |
| 92 | + finally: |
| 93 | + client._closed = True |
0 commit comments