Skip to content

Commit 85f4035

Browse files
authored
Merge pull request #1222 from newrelic/remove-loguru-version-check
Remove versioned logic in loguru instrumentation
2 parents 4768fba + 5618f78 commit 85f4035

File tree

6 files changed

+95
-56
lines changed

6 files changed

+95
-56
lines changed

newrelic/hooks/logger_loguru.py

+9-13
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
_logger = logging.getLogger(__name__)
2727

2828
IS_PYPY = hasattr(sys, "pypy_version_info")
29-
LOGURU_VERSION = get_package_version_tuple("loguru")
3029
LOGURU_FILTERED_RECORD_ATTRS = {"extra", "message", "time", "level", "_nr_original_message", "record"}
3130
ALLOWED_LOGURU_OPTIONS_LENGTHS = frozenset((8, 9))
3231

@@ -69,7 +68,7 @@ def _nr_log_forwarder(message_instance):
6968
try:
7069
time = record.get("time", None)
7170
if time:
72-
time = int(time.timestamp()*1000)
71+
time = int(time.timestamp() * 1000)
7372
record_log_event(message, level_name, time, attributes=attrs)
7473
except Exception:
7574
pass
@@ -116,18 +115,15 @@ def _nr_log_patcher(record):
116115
record["_nr_original_message"] = message = record["message"]
117116
record["message"] = add_nr_linking_metadata(message)
118117

119-
if LOGURU_VERSION > (0, 6, 0):
120-
if original_patcher is not None:
121-
patchers = [p for p in original_patcher] # Consumer iterable into list so we can modify
122-
# Wipe out reference so patchers aren't called twice, as the framework will handle calling other patchers.
123-
original_patcher = None
124-
else:
125-
patchers = []
126-
127-
patchers.append(_nr_log_patcher)
128-
return patchers
118+
if original_patcher is not None:
119+
patchers = [p for p in original_patcher] # Consumer iterable into list so we can modify
120+
# Wipe out reference so patchers aren't called twice, as the framework will handle calling other patchers.
121+
original_patcher = None
129122
else:
130-
return _nr_log_patcher
123+
patchers = []
124+
125+
patchers.append(_nr_log_patcher)
126+
return patchers
131127

132128

133129
def wrap_Logger_init(wrapped, instance, args, kwargs):

tests/logger_loguru/test_local_decorating.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414

1515
import platform
1616

17+
from testing_support.fixtures import reset_core_stats_engine
18+
from testing_support.validators.validate_log_event_count import validate_log_event_count
19+
from testing_support.validators.validate_log_event_count_outside_transaction import (
20+
validate_log_event_count_outside_transaction,
21+
)
22+
1723
from newrelic.api.application import application_settings
1824
from newrelic.api.background_task import background_task
1925
from newrelic.api.time_trace import current_trace
2026
from newrelic.api.transaction import current_transaction
21-
from testing_support.fixtures import reset_core_stats_engine
22-
from testing_support.validators.validate_log_event_count import validate_log_event_count
23-
from testing_support.validators.validate_log_event_count_outside_transaction import validate_log_event_count_outside_transaction
2427

2528

2629
def set_trace_ids():
@@ -31,6 +34,7 @@ def set_trace_ids():
3134
if trace:
3235
trace.guid = "abcdefgh"
3336

37+
3438
def exercise_logging(logger):
3539
set_trace_ids()
3640

@@ -42,7 +46,9 @@ def get_metadata_string(log_message, is_txn):
4246
assert host
4347
entity_guid = application_settings().entity_guid
4448
if is_txn:
45-
metadata_string = f"NR-LINKING|{entity_guid}|{host}|abcdefgh12345678|abcdefgh|Python%20Agent%20Test%20%28logger_loguru%29|"
49+
metadata_string = (
50+
f"NR-LINKING|{entity_guid}|{host}|abcdefgh12345678|abcdefgh|Python%20Agent%20Test%20%28logger_loguru%29|"
51+
)
4652
else:
4753
metadata_string = f"NR-LINKING|{entity_guid}|{host}|||Python%20Agent%20Test%20%28logger_loguru%29|"
4854
formatted_string = f"{log_message} {metadata_string}"
@@ -55,7 +61,7 @@ def test_local_log_decoration_inside_transaction(logger):
5561
@background_task()
5662
def test():
5763
exercise_logging(logger)
58-
assert logger.caplog.records[0] == get_metadata_string('C', True)
64+
assert logger.caplog.records[0] == get_metadata_string("C", True)
5965

6066
test()
6167

@@ -65,7 +71,7 @@ def test_local_log_decoration_outside_transaction(logger):
6571
@validate_log_event_count_outside_transaction(1)
6672
def test():
6773
exercise_logging(logger)
68-
assert logger.caplog.records[0] == get_metadata_string('C', False)
74+
assert logger.caplog.records[0] == get_metadata_string("C", False)
6975

7076
test()
7177

@@ -80,6 +86,6 @@ def patch(record):
8086
def test():
8187
patch_logger = logger.patch(patch)
8288
exercise_logging(patch_logger)
83-
assert logger.caplog.records[0] == get_metadata_string('C-PATCH', False)
89+
assert logger.caplog.records[0] == get_metadata_string("C-PATCH", False)
8490

8591
test()

tests/logger_loguru/test_log_forwarding.py

+30-11
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,21 @@
1313
# limitations under the License.
1414

1515
import logging
16+
1617
import pytest
18+
from testing_support.fixtures import reset_core_stats_engine
19+
from testing_support.validators.validate_log_event_count import validate_log_event_count
20+
from testing_support.validators.validate_log_event_count_outside_transaction import (
21+
validate_log_event_count_outside_transaction,
22+
)
23+
from testing_support.validators.validate_log_events import validate_log_events
24+
from testing_support.validators.validate_log_events_outside_transaction import (
25+
validate_log_events_outside_transaction,
26+
)
1727

1828
from newrelic.api.background_task import background_task
1929
from newrelic.api.time_trace import current_trace
2030
from newrelic.api.transaction import current_transaction
21-
from testing_support.fixtures import reset_core_stats_engine
22-
from testing_support.validators.validate_log_event_count import validate_log_event_count
23-
from testing_support.validators.validate_log_event_count_outside_transaction import validate_log_event_count_outside_transaction
24-
from testing_support.validators.validate_log_events import validate_log_events
25-
from testing_support.validators.validate_log_events_outside_transaction import validate_log_events_outside_transaction
2631

2732

2833
def set_trace_ids():
@@ -33,23 +38,33 @@ def set_trace_ids():
3338
if trace:
3439
trace.guid = "abcdefgh"
3540

41+
3642
def exercise_logging(logger):
3743
set_trace_ids()
3844

3945
logger.warning("C")
4046
logger.error("D")
4147
logger.critical("E")
42-
48+
4349
assert len(logger.caplog.records) == 3
4450

4551

46-
_common_attributes_service_linking = {"timestamp": None, "hostname": None, "entity.name": "Python Agent Test (logger_loguru)", "entity.guid": None}
47-
_common_attributes_trace_linking = {"span.id": "abcdefgh", "trace.id": "abcdefgh12345678", **_common_attributes_service_linking}
52+
_common_attributes_service_linking = {
53+
"timestamp": None,
54+
"hostname": None,
55+
"entity.name": "Python Agent Test (logger_loguru)",
56+
"entity.guid": None,
57+
}
58+
_common_attributes_trace_linking = {
59+
"span.id": "abcdefgh",
60+
"trace.id": "abcdefgh12345678",
61+
**_common_attributes_service_linking,
62+
}
4863

4964
_test_logging_inside_transaction_events = [
5065
{"message": "C", "level": "WARNING", **_common_attributes_trace_linking},
5166
{"message": "D", "level": "ERROR", **_common_attributes_trace_linking},
52-
{"message": "E", "level": "CRITICAL", **_common_attributes_trace_linking},
67+
{"message": "E", "level": "CRITICAL", **_common_attributes_trace_linking},
5368
]
5469

5570

@@ -67,9 +82,10 @@ def test():
6782
_test_logging_outside_transaction_events = [
6883
{"message": "C", "level": "WARNING", **_common_attributes_service_linking},
6984
{"message": "D", "level": "ERROR", **_common_attributes_service_linking},
70-
{"message": "E", "level": "CRITICAL", **_common_attributes_service_linking},
85+
{"message": "E", "level": "CRITICAL", **_common_attributes_service_linking},
7186
]
7287

88+
7389
@reset_core_stats_engine()
7490
def test_logging_outside_transaction(logger):
7591
@validate_log_events_outside_transaction(_test_logging_outside_transaction_events)
@@ -96,6 +112,7 @@ def test():
96112
_test_patcher_application_captured_event = {"message": "C-PATCH", "level": "WARNING"}
97113
_test_patcher_application_captured_event.update(_common_attributes_trace_linking)
98114

115+
99116
@reset_core_stats_engine()
100117
def test_patcher_application_captured(logger):
101118
def patch(record):
@@ -112,9 +129,11 @@ def test():
112129

113130
test()
114131

132+
115133
_test_logger_catch_event = {"level": "ERROR"} # Message varies wildly, can't be included in test
116134
_test_logger_catch_event.update(_common_attributes_trace_linking)
117135

136+
118137
@reset_core_stats_engine()
119138
def test_logger_catch(logger):
120139
@validate_log_events([_test_logger_catch_event])
@@ -132,5 +151,5 @@ def throw():
132151
throw()
133152
except ValueError:
134153
pass
135-
154+
136155
test()

tests/logger_loguru/test_metrics.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,22 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from newrelic.api.background_task import background_task
1615
from testing_support.fixtures import reset_core_stats_engine
17-
from testing_support.validators.validate_custom_metrics_outside_transaction import validate_custom_metrics_outside_transaction
18-
from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics
16+
from testing_support.validators.validate_custom_metrics_outside_transaction import (
17+
validate_custom_metrics_outside_transaction,
18+
)
19+
from testing_support.validators.validate_transaction_metrics import (
20+
validate_transaction_metrics,
21+
)
22+
23+
from newrelic.api.background_task import background_task
1924

2025

2126
def exercise_logging(logger):
2227
logger.warning("C")
2328
logger.error("D")
2429
logger.critical("E")
25-
30+
2631
assert len(logger.caplog.records) == 3
2732

2833

@@ -33,6 +38,7 @@ def exercise_logging(logger):
3338
("Logging/lines/CRITICAL", 1),
3439
]
3540

41+
3642
@reset_core_stats_engine()
3743
def test_logging_metrics_inside_transaction(logger):
3844
@validate_transaction_metrics(

tests/logger_loguru/test_settings.py

+33-18
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,30 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import pytest
1615
import platform
1716

17+
import pytest
18+
from testing_support.fixtures import (
19+
override_application_settings,
20+
reset_core_stats_engine,
21+
)
22+
from testing_support.validators.validate_log_event_count import validate_log_event_count
23+
from testing_support.validators.validate_transaction_metrics import (
24+
validate_transaction_metrics,
25+
)
26+
1827
from newrelic.api.application import application_settings
1928
from newrelic.api.background_task import background_task
20-
from testing_support.fixtures import reset_core_stats_engine
21-
from testing_support.validators.validate_log_event_count import validate_log_event_count
22-
from testing_support.fixtures import override_application_settings
23-
from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics
29+
2430

2531
def get_metadata_string(log_message, is_txn):
2632
host = platform.uname().node
2733
assert host
2834
entity_guid = application_settings().entity_guid
2935
if is_txn:
30-
metadata_string = f"NR-LINKING|{entity_guid}|{host}|abcdefgh12345678|abcdefgh|Python%20Agent%20Test%20%28internal_logging%29|"
36+
metadata_string = (
37+
f"NR-LINKING|{entity_guid}|{host}|abcdefgh12345678|abcdefgh|Python%20Agent%20Test%20%28internal_logging%29|"
38+
)
3139
else:
3240
metadata_string = f"NR-LINKING|{entity_guid}|{host}|||Python%20Agent%20Test%20%28internal_logging%29|"
3341
formatted_string = f"{log_message} {metadata_string}"
@@ -49,10 +57,12 @@ def basic_logging(logger):
4957
@pytest.mark.parametrize("feature_setting,subfeature_setting,expected", _settings_matrix)
5058
@reset_core_stats_engine()
5159
def test_log_forwarding_settings(logger, feature_setting, subfeature_setting, expected):
52-
@override_application_settings({
53-
"application_logging.enabled": feature_setting,
54-
"application_logging.forwarding.enabled": subfeature_setting,
55-
})
60+
@override_application_settings(
61+
{
62+
"application_logging.enabled": feature_setting,
63+
"application_logging.forwarding.enabled": subfeature_setting,
64+
}
65+
)
5666
@validate_log_event_count(1 if expected else 0)
5767
@background_task()
5868
def test():
@@ -65,10 +75,12 @@ def test():
6575
@pytest.mark.parametrize("feature_setting,subfeature_setting,expected", _settings_matrix)
6676
@reset_core_stats_engine()
6777
def test_local_decorating_settings(logger, feature_setting, subfeature_setting, expected):
68-
@override_application_settings({
69-
"application_logging.enabled": feature_setting,
70-
"application_logging.local_decorating.enabled": subfeature_setting,
71-
})
78+
@override_application_settings(
79+
{
80+
"application_logging.enabled": feature_setting,
81+
"application_logging.local_decorating.enabled": subfeature_setting,
82+
}
83+
)
7284
@background_task()
7385
def test():
7486
basic_logging(logger)
@@ -86,10 +98,13 @@ def test():
8698
@reset_core_stats_engine()
8799
def test_log_metrics_settings(logger, feature_setting, subfeature_setting, expected):
88100
metric_count = 1 if expected else None
89-
@override_application_settings({
90-
"application_logging.enabled": feature_setting,
91-
"application_logging.metrics.enabled": subfeature_setting,
92-
})
101+
102+
@override_application_settings(
103+
{
104+
"application_logging.enabled": feature_setting,
105+
"application_logging.metrics.enabled": subfeature_setting,
106+
}
107+
)
93108
@validate_transaction_metrics(
94109
"test_settings:test_log_metrics_settings.<locals>.test",
95110
custom_metrics=[

tox.ini

-3
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ envlist =
144144
python-framework_tornado-{py39,py310,py311,py312}-tornadomaster,
145145
python-logger_logging-{py37,py38,py39,py310,py311,py312,pypy310},
146146
python-logger_loguru-{py37,py38,py39,py310,py311,py312,pypy310}-logurulatest,
147-
python-logger_loguru-py39-loguru{06,05},
148147
python-logger_structlog-{py37,py38,py39,py310,py311,py312,pypy310}-structloglatest,
149148
python-mlmodel_langchain-{py39,py310,py311,py312},
150149
python-mlmodel_openai-openai0-{py37,py38,py39,py310,py311,py312},
@@ -375,8 +374,6 @@ deps =
375374
mlmodel_langchain: mock
376375
mlmodel_langchain: asyncio
377376
logger_loguru-logurulatest: loguru
378-
logger_loguru-loguru06: loguru<0.7
379-
logger_loguru-loguru05: loguru<0.6
380377
logger_structlog-structloglatest: structlog
381378
messagebroker_pika-pikalatest: pika
382379
messagebroker_pika: tornado<5

0 commit comments

Comments
 (0)