Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aliu39 committed Jan 8, 2025
1 parent 4ab3a74 commit c59035b
Showing 1 changed file with 76 additions and 1 deletion.
77 changes: 76 additions & 1 deletion tests/sentry/logging/test_handler.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import logging
from collections.abc import Callable
from contextlib import contextmanager
from typing import Any
from unittest import mock

import pytest

from sentry.logging.handlers import GKEStructLogHandler, JSONRenderer, StructLogHandler
from sentry.logging.handlers import (
GKEStructLogHandler,
JSONRenderer,
SamplingFilter,
StructLogHandler,
)


@pytest.fixture
Expand All @@ -26,6 +33,20 @@ def __str__(self) -> str:
return SNAFU()


@contextmanager
def filter_context(
logger: logging.Logger, filters: list[logging.Filter | Callable[[logging.LogRecord], bool]]
):
"""Manages adding and cleaning up log filters"""
for f in filters:
logger.addFilter(f)
try:
yield
finally:
for f in filters:
logger.removeFilter(f)


def make_logrecord(
*,
name: str = "name",
Expand Down Expand Up @@ -131,3 +152,57 @@ def test_gke_emit() -> None:
event="msg",
**{"logging.googleapis.com/labels": {"name": "name"}},
)


@mock.patch("random.random", lambda: 0.1)
def test_sampling_filter_pass(caplog):
logger = logging.getLogger(__name__)
with filter_context(logger, [SamplingFilter(0.2)]):
with caplog.at_level(logging.DEBUG, __name__):
logger.info("msg1")
logger.info("message.2")

captured_msgs = list(map(lambda r: r.msg, caplog.records))
assert sorted(captured_msgs) == ["message.2", "msg1"]


@mock.patch("random.random", lambda: 0.1)
def test_sampling_filter_filter(caplog):
logger = logging.getLogger(__name__)
with filter_context(logger, [SamplingFilter(0.05)]):
with caplog.at_level(logging.DEBUG, __name__):
logger.info("msg1")
logger.info("message.2")

captured_msgs = list(map(lambda r: r.msg, caplog.records))
assert captured_msgs == []


@mock.patch("random.random", lambda: 0.1)
def test_sampling_filter_level(caplog):
logger = logging.getLogger(__name__)
with filter_context(logger, [SamplingFilter(0.05, level=logging.WARNING)]):
with caplog.at_level(logging.DEBUG, __name__):
logger.debug("debug")
logger.info("info")
logger.warning("warning")
logger.error("error")
logger.critical("critical")

captured_msgs = list(map(lambda r: r.msg, caplog.records))
assert sorted(captured_msgs) == ["critical", "error"]


@mock.patch("random.random", lambda: 0.1)
def test_sampling_filter_level_default(caplog):
logger = logging.getLogger(__name__)
with filter_context(logger, [SamplingFilter(0.05)]):
with caplog.at_level(logging.DEBUG, __name__):
logger.debug("debug")
logger.info("info")
logger.warning("warning")
logger.error("error")
logger.critical("critical")

captured_msgs = list(map(lambda r: r.msg, caplog.records))
assert sorted(captured_msgs) == ["critical", "error", "warning"]

0 comments on commit c59035b

Please sign in to comment.