diff --git a/newrelic/hooks/logger_structlog.py b/newrelic/hooks/logger_structlog.py index 6ad2dea6a..8b34b285f 100644 --- a/newrelic/hooks/logger_structlog.py +++ b/newrelic/hooks/logger_structlog.py @@ -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 diff --git a/tests/logger_structlog/conftest.py b/tests/logger_structlog/conftest.py index fee12d3f1..014889704 100644 --- a/tests/logger_structlog/conftest.py +++ b/tests/logger_structlog/conftest.py @@ -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 diff --git a/tests/logger_structlog/test_attribute_forwarding.py b/tests/logger_structlog/test_attribute_forwarding.py index fec54cfc5..39baac66d 100644 --- a/tests/logger_structlog/test_attribute_forwarding.py +++ b/tests/logger_structlog/test_attribute_forwarding.py @@ -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): @@ -51,4 +50,4 @@ def test_attributes_outside_transaction(logger, structlog_caplog): def test(): exercise_logging(logger, structlog_caplog) - test() \ No newline at end of file + test() diff --git a/tests/logger_structlog/test_local_decorating.py b/tests/logger_structlog/test_local_decorating.py index 18b7721fb..2a457ec6a 100644 --- a/tests/logger_structlog/test_local_decorating.py +++ b/tests/logger_structlog/test_local_decorating.py @@ -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") @@ -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() @@ -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() diff --git a/tests/logger_structlog/test_log_forwarding.py b/tests/logger_structlog/test_log_forwarding.py index 1bf67f605..a15740211 100644 --- a/tests/logger_structlog/test_log_forwarding.py +++ b/tests/logger_structlog/test_log_forwarding.py @@ -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", @@ -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}, @@ -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() diff --git a/tests/logger_structlog/test_metrics.py b/tests/logger_structlog/test_metrics.py index 7ff33e41c..e1ded3867 100644 --- a/tests/logger_structlog/test_metrics.py +++ b/tests/logger_structlog/test_metrics.py @@ -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): @@ -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), @@ -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)