Skip to content

Commit bcd9501

Browse files
committed
Remove other framework changes
1 parent 5974d7b commit bcd9501

File tree

11 files changed

+178
-287
lines changed

11 files changed

+178
-287
lines changed

newrelic/hooks/logger_logging.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from urllib.parse import quote
2828

2929

30-
IGNORED_LOG_RECORD_KEYS = set(["message", "msg"])
30+
DEFAULT_LOG_RECORD_KEYS = frozenset(set(vars(LogRecord("", 0, "", 0, "", (), None))) | {"message"})
3131

3232

3333
def add_nr_linking_metadata(message):
@@ -52,6 +52,15 @@ def bind_callHandlers(record):
5252
return record
5353

5454

55+
def filter_record_attributes(record):
56+
record_attrs = vars(record)
57+
custom_attr_keys = set(record_attrs.keys()) - DEFAULT_LOG_RECORD_KEYS
58+
if custom_attr_keys:
59+
return {k: record_attrs[k] for k in custom_attr_keys if k not in DEFAULT_LOG_RECORD_KEYS}
60+
else:
61+
return None
62+
63+
5564
def wrap_callHandlers(wrapped, instance, args, kwargs):
5665
transaction = current_transaction()
5766
record = bind_callHandlers(*args, **kwargs)
@@ -80,17 +89,10 @@ def wrap_callHandlers(wrapped, instance, args, kwargs):
8089

8190
if settings.application_logging.forwarding and settings.application_logging.forwarding.enabled:
8291
try:
83-
message = record.msg
84-
if not isinstance(message, dict):
85-
# Allow python to convert the message to a string and template it with args.
86-
message = record.getMessage()
87-
88-
# Grab and filter context attributes from log record
89-
record_attrs = vars(record)
90-
context_attrs = {k: record_attrs[k] for k in vars(record) if k not in IGNORED_LOG_RECORD_KEYS}
91-
92+
message = record.getMessage()
93+
attrs = filter_record_attributes(record)
9294
record_log_event(
93-
message=message, level=level_name, timestamp=int(record.created * 1000), attributes=context_attrs
95+
message=message, level=level_name, timestamp=int(record.created * 1000), attributes=attrs
9496
)
9597
except Exception:
9698
pass

newrelic/hooks/logger_structlog.py

Lines changed: 23 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import functools
16-
17-
from newrelic.api.application import application_instance
18-
from newrelic.api.transaction import current_transaction, record_log_event
1915
from newrelic.common.object_wrapper import wrap_function_wrapper
20-
from newrelic.common.signature import bind_args
16+
from newrelic.api.transaction import current_transaction, record_log_event
2117
from newrelic.core.config import global_settings
18+
from newrelic.api.application import application_instance
2219
from newrelic.hooks.logger_logging import add_nr_linking_metadata
20+
from newrelic.common.signature import bind_args
2321

2422

25-
@functools.cache
2623
def normalize_level_name(method_name):
2724
# Look up level number for method name, using result to look up level name for that level number.
2825
# Convert result to upper case, and default to UNKNOWN in case of errors or missing values.
@@ -37,7 +34,14 @@ def bind_process_event(method_name, event, event_kw):
3734
return method_name, event, event_kw
3835

3936

40-
def new_relic_event_consumer(logger, level, event):
37+
def wrap__process_event(wrapped, instance, args, kwargs):
38+
try:
39+
method_name, event, event_kw = bind_process_event(*args, **kwargs)
40+
except TypeError:
41+
return wrapped(*args, **kwargs)
42+
43+
original_message = event # Save original undecorated message
44+
4145
transaction = current_transaction()
4246

4347
if transaction:
@@ -46,28 +50,16 @@ def new_relic_event_consumer(logger, level, event):
4650
settings = global_settings()
4751

4852
# Return early if application logging not enabled
49-
if settings and settings.application_logging.enabled:
50-
if isinstance(event, (str, bytes, bytearray)):
51-
message = original_message = event
52-
event_attrs = {}
53-
elif isinstance(event, dict):
54-
message = original_message = event.get("event", "")
55-
event_attrs = {k: v for k, v in event.items() if k != "event"}
56-
else:
57-
# Unclear how to proceed, ignore log. Avoid logging an error message or we may incur an infinite loop.
58-
return event
59-
60-
if settings.application_logging.local_decorating.enabled:
61-
message = add_nr_linking_metadata(message)
62-
if isinstance(event, (str, bytes, bytearray)):
63-
event = message
64-
elif isinstance(event, dict) and "event" in event:
65-
# TODO CHECK ON THIS
66-
event["event"] = message
53+
if settings and settings.application_logging and settings.application_logging.enabled:
54+
if settings.application_logging.local_decorating and settings.application_logging.local_decorating.enabled:
55+
event = add_nr_linking_metadata(event)
6756

68-
level_name = normalize_level_name(level)
57+
# Send log to processors for filtering, allowing any DropEvent exceptions that occur to prevent instrumentation from recording the log event.
58+
result = wrapped(method_name, event, event_kw)
59+
60+
level_name = normalize_level_name(method_name)
6961

70-
if settings.application_logging.metrics.enabled:
62+
if settings.application_logging.metrics and settings.application_logging.metrics.enabled:
7163
if transaction:
7264
transaction.record_custom_metric("Logging/lines", {"count": 1})
7365
transaction.record_custom_metric("Logging/lines/%s" % level_name, {"count": 1})
@@ -77,34 +69,15 @@ def new_relic_event_consumer(logger, level, event):
7769
application.record_custom_metric("Logging/lines", {"count": 1})
7870
application.record_custom_metric("Logging/lines/%s" % level_name, {"count": 1})
7971

80-
if settings.application_logging.forwarding.enabled:
72+
if settings.application_logging.forwarding and settings.application_logging.forwarding.enabled:
8173
try:
82-
record_log_event(original_message, level_name, attributes=event_attrs)
74+
record_log_event(original_message, level_name)
8375

8476
except Exception:
8577
pass
8678

87-
return event
88-
89-
90-
def wrap__process_event(wrapped, instance, args, kwargs):
91-
transaction = current_transaction()
92-
93-
if transaction:
94-
settings = transaction.settings
95-
else:
96-
settings = global_settings()
97-
98-
# Return early if application logging not enabled
99-
if settings and settings.application_logging and settings.application_logging.enabled:
100-
processors = instance._processors
101-
if not processors:
102-
instance._processors = [new_relic_event_consumer]
103-
elif processors[-1] != new_relic_event_consumer:
104-
# Remove our processor if it exists and add it to the end
105-
if new_relic_event_consumer in processors:
106-
processors.remove(new_relic_event_consumer)
107-
processors.append(new_relic_event_consumer)
79+
# Return the result from wrapped after we've recorded the resulting log event.
80+
return result
10881

10982
return wrapped(*args, **kwargs)
11083

tests/logger_logging/conftest.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"debug.record_transaction_failure": True,
2727
"application_logging.enabled": True,
2828
"application_logging.forwarding.enabled": True,
29-
"application_logging.forwarding.context_data.enabled": True,
3029
"application_logging.metrics.enabled": True,
3130
"application_logging.local_decorating.enabled": True,
3231
"event_harvest_config.harvest_limits.log_event_data": 100000,
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
from testing_support.fixtures import (
16+
override_application_settings,
17+
reset_core_stats_engine,
18+
)
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+
)
27+
28+
from newrelic.api.background_task import background_task
29+
30+
_event_attributes = {"message": "A", "context.key": "value"}
31+
32+
33+
def exercise_logging(logger):
34+
logger.error("A", extra={"key": "value"})
35+
assert len(logger.caplog.records) == 1
36+
37+
38+
@override_application_settings(
39+
{
40+
"application_logging.forwarding.context_data.enabled": True,
41+
}
42+
)
43+
def test_attributes_inside_transaction(logger):
44+
@validate_log_events([_event_attributes])
45+
@validate_log_event_count(1)
46+
@background_task()
47+
def test():
48+
exercise_logging(logger)
49+
50+
test()
51+
52+
53+
@reset_core_stats_engine()
54+
@override_application_settings(
55+
{
56+
"application_logging.forwarding.context_data.enabled": True,
57+
}
58+
)
59+
def test_attributes_outside_transaction(logger):
60+
@validate_log_events_outside_transaction([_event_attributes])
61+
@validate_log_event_count_outside_transaction(1)
62+
def test():
63+
exercise_logging(logger)
64+
65+
test()

tests/logger_logging/test_attributes.py

Lines changed: 0 additions & 92 deletions
This file was deleted.

tests/logger_logging/test_local_decorating.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ def set_trace_ids():
3535
trace.guid = "abcdefgh"
3636

3737

38-
def exercise_logging(logger, message="C"):
38+
def exercise_logging(logger):
3939
set_trace_ids()
4040

41-
logger.warning(message)
41+
logger.warning("C")
4242

4343

4444
def get_metadata_string(log_message, is_txn):
@@ -82,14 +82,3 @@ def test():
8282
assert logger.caplog.records[0] == get_metadata_string("C", False)
8383

8484
test()
85-
86-
87-
@reset_core_stats_engine()
88-
def test_local_log_decoration_dict_message(logger):
89-
@validate_log_event_count(1)
90-
@background_task()
91-
def test():
92-
exercise_logging(logger, {"message": "dict_message"})
93-
assert logger.caplog.records[0] == get_metadata_string("{'message': 'dict_message'}", True)
94-
95-
test()

0 commit comments

Comments
 (0)