Skip to content

Commit

Permalink
Add more filtering tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
umaannamalai committed Nov 17, 2022
1 parent bb2747a commit 5cee7b0
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 13 deletions.
3 changes: 1 addition & 2 deletions newrelic/hooks/logger_structlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ def wrap__process_event(wrapped, instance, args, kwargs):

if settings.application_logging.forwarding and settings.application_logging.forwarding.enabled:
try:
#record_log_event(original_message, level_name, attributes=event_kw)
record_log_event(original_message, level_name)
record_log_event(original_message, level_name, attributes=event_kw)

except Exception:
pass
Expand Down
1 change: 1 addition & 0 deletions tests/logger_structlog/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def logger(structlog_caplog):
_logger = structlog.get_logger()
yield _logger


@pytest.fixture(scope="function")
def filtering_logger(structlog_caplog):
import structlog
Expand Down
7 changes: 3 additions & 4 deletions tests/logger_structlog/test_attribute_forwarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
# limitations under the License.

from newrelic.api.background_task import background_task
from testing_support.fixtures import reset_core_stats_engine
from testing_support.fixtures import override_application_settings, reset_core_stats_engine
from testing_support.validators.validate_log_event_count import validate_log_event_count
from testing_support.validators.validate_log_event_count_outside_transaction import validate_log_event_count_outside_transaction
from testing_support.validators.validate_log_events import validate_log_events
from testing_support.validators.validate_log_events_outside_transaction import validate_log_events_outside_transaction
from testing_support.fixtures import override_application_settings


_event_attributes = {"message": "A", "context.key": "value", "context.int_attr": "1", "context.dict_attr": '{"key":"value"}'}
_event_attributes = {"message": "A", "context.key": "value"}


def exercise_logging(logger, structlog_caplog):
Expand Down Expand Up @@ -51,4 +50,4 @@ def test_attributes_outside_transaction(logger, structlog_caplog):
def test():
exercise_logging(logger, structlog_caplog)

test()
test()
6 changes: 3 additions & 3 deletions tests/logger_structlog/test_local_decorating.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def set_trace_ids():
if trace:
trace.guid = "abcdefgh"

def exercise_logging(logger, structlog_caplog):
def exercise_logging(logger):
set_trace_ids()

logger.warning("C")
Expand All @@ -54,7 +54,7 @@ def test_local_log_decoration_inside_transaction(logger, structlog_caplog):
@validate_log_event_count(1)
@background_task()
def test():
exercise_logging(logger, structlog_caplog)
exercise_logging(logger)
assert get_metadata_string('C', True) in structlog_caplog[0]

test()
Expand All @@ -64,7 +64,7 @@ def test():
def test_local_log_decoration_outside_transaction(logger, structlog_caplog):
@validate_log_event_count_outside_transaction(1)
def test():
exercise_logging(logger, structlog_caplog)
exercise_logging(logger)
assert get_metadata_string('C', False) in structlog_caplog[0]

test()
47 changes: 47 additions & 0 deletions tests/logger_structlog/test_log_forwarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ def exercise_logging(logger, structlog_caplog):
assert "Elephant" in structlog_caplog[2]


def exercise_filtering_logging(filtering_logger, structlog_caplog):
set_trace_ids()

filtering_logger.msg("Cat", a=42)
filtering_logger.error("Dog")
filtering_logger.critical("Elephant")

assert len(structlog_caplog) == 2

assert "Cat" not in structlog_caplog[0]
assert "Dog" in structlog_caplog[0]
assert "Elephant" in structlog_caplog[1]

_common_attributes_service_linking = {"timestamp": None, "hostname": None,
"entity.name": "Python Agent Test (logger_structlog)", "entity.guid": None}
_common_attributes_trace_linking = {"span.id": "abcdefgh", "trace.id": "abcdefgh12345678",
Expand All @@ -73,6 +86,24 @@ def test():
test()


_test_logging_filtering_inside_transaction_events = [
{"message": "Dog", "level": "ERROR", **_common_attributes_trace_linking},
{"message": "Elephant", "level": "CRITICAL", **_common_attributes_trace_linking},
]


@reset_core_stats_engine()
@override_application_settings({"application_logging.local_decorating.enabled": False})
def test_logging_filtering_inside_transaction(filtering_logger, structlog_caplog):
@validate_log_events(_test_logging_filtering_inside_transaction_events)
@validate_log_event_count(2)
@background_task()
def test():
exercise_filtering_logging(filtering_logger, structlog_caplog)

test()


_test_logging_outside_transaction_events = [
{"message": "Cat", "level": "INFO", **_common_attributes_service_linking},
{"message": "Dog", "level": "ERROR", **_common_attributes_service_linking},
Expand All @@ -90,3 +121,19 @@ def test():

test()


_test_logging_filtering_outside_transaction_events = [
{"message": "Dog", "level": "ERROR", **_common_attributes_service_linking},
{"message": "Elephant", "level": "CRITICAL", **_common_attributes_service_linking},
]


@reset_core_stats_engine()
@override_application_settings({"application_logging.local_decorating.enabled": False})
def test_logging_filtering_outside_transaction(filtering_logger, structlog_caplog):
@validate_log_events_outside_transaction(_test_logging_filtering_outside_transaction_events)
@validate_log_event_count_outside_transaction(2)
def test():
exercise_filtering_logging(filtering_logger, structlog_caplog)

test()
12 changes: 8 additions & 4 deletions tests/logger_structlog/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
from newrelic.packages import six
from newrelic.api.background_task import background_task
from testing_support.fixtures import reset_core_stats_engine
from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics
from testing_support.validators.validate_custom_metrics_outside_transaction import validate_custom_metrics_outside_transaction
from testing_support.fixtures import (
validate_transaction_metrics,
)



def exercise_logging(logger, structlog_caplog):
Expand All @@ -38,6 +37,12 @@ def exercise_filtering_logging(filtering_logger, structlog_caplog):
filtering_logger.error("Dog")
filtering_logger.critical("Elephant")

assert len(structlog_caplog) == 2

assert "Cat" not in structlog_caplog[0]
assert "Dog" in structlog_caplog[0]
assert "Elephant" in structlog_caplog[1]


_test_logging_unscoped_metrics = [
("Logging/lines", 3),
Expand Down Expand Up @@ -81,7 +86,6 @@ def test():
test()



@reset_core_stats_engine()
def test_logging_metrics_outside_transaction(logger, structlog_caplog):
@validate_custom_metrics_outside_transaction(_test_logging_unscoped_metrics)
Expand Down

0 comments on commit 5cee7b0

Please sign in to comment.