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
1915from 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
2117from newrelic .core .config import global_settings
18+ from newrelic .api .application import application_instance
2219from newrelic .hooks .logger_logging import add_nr_linking_metadata
20+ from newrelic .common .signature import bind_args
2321
2422
25- @functools .cache
2623def 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
0 commit comments