|
| 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 newrelic.api.application import application_instance |
| 16 | +from newrelic.api.time_trace import get_linking_metadata |
| 17 | +from newrelic.api.transaction import current_transaction, record_log_event |
| 18 | +from newrelic.common.object_wrapper import wrap_function_wrapper, function_wrapper |
| 19 | +from newrelic.core.config import global_settings |
| 20 | + |
| 21 | + |
| 22 | +try: |
| 23 | + from urllib import quote |
| 24 | +except ImportError: |
| 25 | + from urllib.parse import quote |
| 26 | + |
| 27 | + |
| 28 | +def add_nr_linking_metadata(message): |
| 29 | + available_metadata = get_linking_metadata() |
| 30 | + entity_name = quote(available_metadata.get("entity.name", "")) |
| 31 | + entity_guid = available_metadata.get("entity.guid", "") |
| 32 | + span_id = available_metadata.get("span.id", "") |
| 33 | + trace_id = available_metadata.get("trace.id", "") |
| 34 | + hostname = available_metadata.get("hostname", "") |
| 35 | + |
| 36 | + nr_linking_str = "|".join(("NR-LINKING", entity_guid, hostname, trace_id, span_id, entity_name)) |
| 37 | + return "%s %s|" % (message, nr_linking_str) |
| 38 | + |
| 39 | + |
| 40 | +@function_wrapper |
| 41 | +def wrap_getMessage(wrapped, instance, args, kwargs): |
| 42 | + message = wrapped(*args, **kwargs) |
| 43 | + return add_nr_linking_metadata(message) |
| 44 | + |
| 45 | + |
| 46 | +def bind_callHandlers(record): |
| 47 | + return record |
| 48 | + |
| 49 | + |
| 50 | +def wrap_callHandlers(wrapped, instance, args, kwargs): |
| 51 | + transaction = current_transaction() |
| 52 | + record = bind_callHandlers(*args, **kwargs) |
| 53 | + |
| 54 | + logger_name = getattr(instance, "name", None) |
| 55 | + if logger_name and logger_name.split(".")[0] == "newrelic": |
| 56 | + return wrapped(*args, **kwargs) |
| 57 | + |
| 58 | + if transaction: |
| 59 | + settings = transaction.settings |
| 60 | + else: |
| 61 | + settings = global_settings() |
| 62 | + |
| 63 | + # Return early if application logging not enabled |
| 64 | + if settings and settings.application_logging and settings.application_logging.enabled: |
| 65 | + level_name = str(getattr(record, "levelname", "UNKNOWN")) |
| 66 | + if settings.application_logging.metrics and settings.application_logging.metrics.enabled: |
| 67 | + if transaction: |
| 68 | + transaction.record_custom_metric("Logging/lines", {"count": 1}) |
| 69 | + transaction.record_custom_metric("Logging/lines/%s" % level_name, {"count": 1}) |
| 70 | + else: |
| 71 | + application = application_instance(activate=False) |
| 72 | + if application and application.enabled: |
| 73 | + application.record_custom_metric("Logging/lines", {"count": 1}) |
| 74 | + application.record_custom_metric("Logging/lines/%s" % level_name, {"count": 1}) |
| 75 | + |
| 76 | + if settings.application_logging.forwarding and settings.application_logging.forwarding.enabled: |
| 77 | + try: |
| 78 | + message = record.getMessage() |
| 79 | + record_log_event(message, level_name, int(record.created * 1000)) |
| 80 | + except Exception: |
| 81 | + pass |
| 82 | + |
| 83 | + if settings.application_logging.local_decorating and settings.application_logging.local_decorating.enabled: |
| 84 | + record._nr_original_message = record.getMessage |
| 85 | + record.getMessage = wrap_getMessage(record.getMessage) |
| 86 | + |
| 87 | + return wrapped(*args, **kwargs) |
| 88 | + |
| 89 | + |
| 90 | +def instrument_logging(module): |
| 91 | + if hasattr(module, "Logger"): |
| 92 | + if hasattr(module.Logger, "callHandlers"): |
| 93 | + wrap_function_wrapper(module, "Logger.callHandlers", wrap_callHandlers) |
0 commit comments