|
| 1 | +# Copyright 2010 New Relic, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import logging |
| 16 | +import pytest |
| 17 | +from structlog import DropEvent, PrintLogger |
| 18 | +from newrelic.api.time_trace import current_trace |
| 19 | +from newrelic.api.transaction import current_transaction |
| 20 | +from testing_support.fixtures import ( |
| 21 | + collector_agent_registration_fixture, |
| 22 | + collector_available_fixture, |
| 23 | +) |
| 24 | + |
| 25 | +_default_settings = { |
| 26 | + "transaction_tracer.explain_threshold": 0.0, |
| 27 | + "transaction_tracer.transaction_threshold": 0.0, |
| 28 | + "transaction_tracer.stack_trace_threshold": 0.0, |
| 29 | + "debug.log_data_collector_payloads": True, |
| 30 | + "debug.record_transaction_failure": True, |
| 31 | + "application_logging.enabled": True, |
| 32 | + "application_logging.forwarding.enabled": True, |
| 33 | + "application_logging.metrics.enabled": True, |
| 34 | + "application_logging.local_decorating.enabled": True, |
| 35 | + "event_harvest_config.harvest_limits.log_event_data": 100000, |
| 36 | +} |
| 37 | + |
| 38 | +collector_agent_registration = collector_agent_registration_fixture( |
| 39 | + app_name="Python Agent Test (logger_structlog)", |
| 40 | + default_settings=_default_settings, |
| 41 | +) |
| 42 | + |
| 43 | + |
| 44 | +class StructLogCapLog(PrintLogger): |
| 45 | + def __init__(self, caplog): |
| 46 | + self.caplog = caplog if caplog is not None else [] |
| 47 | + |
| 48 | + def msg(self, event, **kwargs): |
| 49 | + self.caplog.append(event) |
| 50 | + return |
| 51 | + |
| 52 | + log = debug = info = warn = warning = msg |
| 53 | + fatal = failure = err = error = critical = exception = msg |
| 54 | + |
| 55 | + def __repr__(self): |
| 56 | + return "<StructLogCapLog %s>" % str(id(self)) |
| 57 | + |
| 58 | + __str__ = __repr__ |
| 59 | + |
| 60 | + |
| 61 | +@pytest.fixture |
| 62 | +def set_trace_ids(): |
| 63 | + def _set(): |
| 64 | + txn = current_transaction() |
| 65 | + if txn: |
| 66 | + txn._trace_id = "abcdefgh12345678" |
| 67 | + trace = current_trace() |
| 68 | + if trace: |
| 69 | + trace.guid = "abcdefgh" |
| 70 | + return _set |
| 71 | + |
| 72 | +def drop_event_processor(logger, method_name, event_dict): |
| 73 | + if method_name == "info": |
| 74 | + raise DropEvent |
| 75 | + else: |
| 76 | + return event_dict |
| 77 | + |
| 78 | + |
| 79 | +@pytest.fixture(scope="function") |
| 80 | +def structlog_caplog(): |
| 81 | + return list() |
| 82 | + |
| 83 | + |
| 84 | +@pytest.fixture(scope="function") |
| 85 | +def logger(structlog_caplog): |
| 86 | + import structlog |
| 87 | + structlog.configure(processors=[], logger_factory=lambda *args, **kwargs: StructLogCapLog(structlog_caplog)) |
| 88 | + _logger = structlog.get_logger() |
| 89 | + return _logger |
| 90 | + |
| 91 | + |
| 92 | +@pytest.fixture(scope="function") |
| 93 | +def filtering_logger(structlog_caplog): |
| 94 | + import structlog |
| 95 | + structlog.configure(processors=[drop_event_processor], logger_factory=lambda *args, **kwargs: StructLogCapLog(structlog_caplog)) |
| 96 | + _filtering_logger = structlog.get_logger() |
| 97 | + return _filtering_logger |
| 98 | + |
| 99 | + |
| 100 | +@pytest.fixture |
| 101 | +def exercise_logging_multiple_lines(set_trace_ids, logger, structlog_caplog): |
| 102 | + def _exercise(): |
| 103 | + set_trace_ids() |
| 104 | + |
| 105 | + logger.msg("Cat", a=42) |
| 106 | + logger.error("Dog") |
| 107 | + logger.critical("Elephant") |
| 108 | + |
| 109 | + assert len(structlog_caplog) == 3 |
| 110 | + |
| 111 | + assert "Cat" in structlog_caplog[0] |
| 112 | + assert "Dog" in structlog_caplog[1] |
| 113 | + assert "Elephant" in structlog_caplog[2] |
| 114 | + |
| 115 | + return _exercise |
| 116 | + |
| 117 | + |
| 118 | +@pytest.fixture |
| 119 | +def exercise_filtering_logging_multiple_lines(set_trace_ids, filtering_logger, structlog_caplog): |
| 120 | + def _exercise(): |
| 121 | + set_trace_ids() |
| 122 | + |
| 123 | + filtering_logger.msg("Cat", a=42) |
| 124 | + filtering_logger.error("Dog") |
| 125 | + filtering_logger.critical("Elephant") |
| 126 | + |
| 127 | + assert len(structlog_caplog) == 2 |
| 128 | + |
| 129 | + assert "Cat" not in structlog_caplog[0] |
| 130 | + assert "Dog" in structlog_caplog[0] |
| 131 | + assert "Elephant" in structlog_caplog[1] |
| 132 | + |
| 133 | + return _exercise |
| 134 | + |
| 135 | + |
| 136 | +@pytest.fixture |
| 137 | +def exercise_logging_single_line(set_trace_ids, logger, structlog_caplog): |
| 138 | + def _exercise(): |
| 139 | + set_trace_ids() |
| 140 | + logger.error("A", key="value") |
| 141 | + assert len(structlog_caplog) == 1 |
| 142 | + |
| 143 | + return _exercise |
0 commit comments