diff --git a/newrelic/api/web_transaction.py b/newrelic/api/web_transaction.py index 9749e26194..aff9fc01f4 100644 --- a/newrelic/api/web_transaction.py +++ b/newrelic/api/web_transaction.py @@ -16,6 +16,7 @@ import time import logging import warnings +import re try: import urlparse @@ -139,6 +140,23 @@ class WebTransaction(Transaction): unicode_error_reported = False QUEUE_TIME_HEADERS = ('x-request-start', 'x-queue-start') + @property + def _request_uri(self): + return self.request_uri + + + @_request_uri.setter + def _request_uri(self, raw_uri): + if self._settings and raw_uri: + if self._settings.url_regex and self._settings.url_regex_rep: + obfuscated_uri = re.sub(str(self._settings.url_regex),str(self._settings.url_regex_rep), str(raw_uri)) + self.request_uri = obfuscated_uri + elif self._settings.url_regex: + obfuscated_uri = re.sub(str(self._settings.url_regex),"*****", str(raw_uri)) + self.request_uri = obfuscated_uri + else: + self.request_uri = raw_uri + def __init__(self, application, name, group=None, scheme=None, host=None, port=None, request_method=None, request_path=None, query_string=None, headers=None, diff --git a/newrelic/config.py b/newrelic/config.py index 456cd722dc..eca6eb6ea1 100644 --- a/newrelic/config.py +++ b/newrelic/config.py @@ -314,6 +314,8 @@ def _process_setting(section, option, getter, mapper): def _process_configuration(section): _process_setting(section, "feature_flag", "get", _map_feature_flag) _process_setting(section, "app_name", "get", None) + _process_setting(section, "url_regex", "get", None) + _process_setting(section, "url_regex_rep", "get", None) _process_setting(section, "labels", "get", _map_labels) _process_setting(section, "license_key", "get", _map_default_host_value) _process_setting(section, "api_key", "get", None) diff --git a/newrelic/core/config.py b/newrelic/core/config.py index 60520c1134..da06932208 100644 --- a/newrelic/core/config.py +++ b/newrelic/core/config.py @@ -570,6 +570,9 @@ def default_host(license_key): _settings.ca_bundle_path = os.environ.get("NEW_RELIC_CA_BUNDLE_PATH", None) _settings.app_name = os.environ.get("NEW_RELIC_APP_NAME", "Python Application") +_settings.url_regex = os.environ.get('NEW_RELIC_URL_REGEX', None ) +_settings.url_regex_rep = os.environ.get('NEW_RELIC_URL_REGEX_REP', None) + _settings.linked_applications = [] _settings.process_host.display_name = os.environ.get("NEW_RELIC_PROCESS_HOST_DISPLAY_NAME", None) diff --git a/newrelic/newrelic.ini b/newrelic/newrelic.ini index d06d8a2926..995e0b4f16 100644 --- a/newrelic/newrelic.ini +++ b/newrelic/newrelic.ini @@ -42,6 +42,14 @@ license_key = *** REPLACE ME *** # https://docs.newrelic.com/docs/apm/agents/manage-apm-agents/app-naming/use-multiple-names-app/ app_name = Python Application +# If sensitive data needs obfuscating in the URL, set this to regex pattern to search in URL +# Disabled by default +url_regex = + +# String to replace with if regex pattern returns match +# Default ***** +url_regex_rep = + # When "true", the agent collects performance data about your # application and reports this data to the New Relic UI at # newrelic.com. This global switch is normally overridden for