This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
forked from newrelic/newrelic-python-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Prevent agent from talking to New Relic (but preserve instrumentation) #1
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f32f673
Prevent agent from talking to New Relic (canned responses)
timmc-edx c9382e0
Ignore more agent method calls, and only warn on first attempt for each
timmc-edx 0ee104c
Add all remaining known agent request methods
timmc-edx b970bc8
Gate the no-report feature behind Django setting EDX_NEWRELIC_NO_REPORT
timmc-edx a6c47c9
Remove temporary print statements
timmc-edx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,131 @@ | |
_logger = logging.getLogger(__name__) | ||
|
||
|
||
# {return_value: ...} wrapper has been removed from each of these | ||
FAKE_AGENT_RESPONSE = { | ||
'preconnect': {"redirect_host": "collector.newrelic.com"}, | ||
'connect': json_decode(r""" | ||
{ | ||
"agent_run_id": "xxx1", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed everything that looked like an identifier or key and replaced it with |
||
"data_report_period": 60, | ||
"application_id": "xxx10", | ||
"browser_key": "xxx2", | ||
"beacon": "bam.nr-data.net", | ||
"apdex_t": 0.5, | ||
"url_rules": [ | ||
{ | ||
"ignore": false, | ||
"match_expression": ".*\\.(ace|arj|ini|txt|udl|plist|css|gif|ico|jpe?g|js|png|swf|woff|caf|aiff|m4v|mpe?g|mp3|mp4|mov)$", | ||
"replacement": "/*.\\1", | ||
"eval_order": 1000, | ||
"terminate_chain": true, | ||
"replace_all": false, | ||
"each_segment": false | ||
}, | ||
{ | ||
"ignore": false, | ||
"match_expression": "^[0-9][0-9a-f_,.-]*$", | ||
"replacement": "*", | ||
"eval_order": 1001, | ||
"terminate_chain": false, | ||
"replace_all": false, | ||
"each_segment": true | ||
}, | ||
{ | ||
"ignore": false, | ||
"match_expression": "^(.*)/[0-9][0-9a-f_,-]*\\.([0-9a-z][0-9a-z]*)$", | ||
"replacement": "\\1/.*\\2", | ||
"eval_order": 1002, | ||
"terminate_chain": false, | ||
"replace_all": false, | ||
"each_segment": false | ||
} | ||
], | ||
"data_methods": { | ||
"error_event_data": { | ||
"report_period_in_seconds": 60, | ||
"max_samples_stored": 10000 | ||
}, | ||
"span_event_data": { | ||
"report_period_in_seconds": 60, | ||
"max_samples_stored": 10000 | ||
}, | ||
"custom_event_data": { | ||
"report_period_in_seconds": 60, | ||
"max_samples_stored": 10000 | ||
}, | ||
"analytic_event_data": { | ||
"report_period_in_seconds": 60, | ||
"max_samples_stored": 10000 | ||
} | ||
}, | ||
"event_harvest_config": { | ||
"report_period_ms": 5000, | ||
"harvest_limits": { | ||
"error_event_data": 8, | ||
"log_event_data": 0, | ||
"analytic_event_data": 100, | ||
"custom_event_data": 300 | ||
} | ||
}, | ||
"span_event_harvest_config": { | ||
"report_period_ms": 60000, | ||
"harvest_limit": 2000 | ||
}, | ||
"transaction_naming_scheme": "legacy", | ||
"product_level": 0, | ||
"max_payload_size_in_bytes": 1000000, | ||
"sampling_rate": 0, | ||
"entity_guid": "xxx3", | ||
"collect_error_events": true, | ||
"collect_analytics_events": true, | ||
"collect_span_events": true, | ||
"collect_errors": true, | ||
"collect_traces": true, | ||
"sampling_target_period_in_seconds": 60, | ||
"sampling_target": 10, | ||
"primary_application_id": "xxx6", | ||
"account_id": "xxx7", | ||
"messages": [], | ||
"cross_process_id": "xxx4", | ||
"encoding_key": "xxx5", | ||
"trusted_account_ids": [], | ||
"trusted_account_key": "xxx8", | ||
"request_headers_map": {}, | ||
"browser_monitoring.distributed_tracing.enabled": true, | ||
"js_agent_file": "", | ||
"episodes_url": "https://js-agent.newrelic.com/nr-100.js", | ||
"error_beacon": "bam.nr-data.net", | ||
"browser_monitoring.loader_version": "1.260.1", | ||
"browser_monitoring.loader": "spa", | ||
"js_agent_loader_version": "bogus-loader-sorry", | ||
"feature_flags": [], | ||
"episodes_file": "js-agent.newrelic.com/nr-100.js", | ||
"browser_monitoring.privacy.cookies_enabled": true, | ||
"js_agent_loader": "xxx9" | ||
} | ||
"""), | ||
'agent_settings': None, | ||
|
||
# From data_collector.py | ||
'agent_command_results': {}, | ||
'analytic_event_data': {}, | ||
'custom_event_data': {}, | ||
'dimensional_metric_data': {}, | ||
'error_data': {}, | ||
'error_event_data': {}, | ||
'get_agent_commands': {}, | ||
'log_event_data': {}, | ||
'metric_data': {}, | ||
'ml_event_data': {}, | ||
'profile_data': {}, | ||
'shutdown': {}, | ||
'span_event_data': {}, | ||
'sql_trace_data': {}, | ||
'transaction_sample_data': {}, | ||
} | ||
|
||
|
||
class AgentProtocol(object): | ||
VERSION = 17 | ||
|
||
|
@@ -158,6 +283,12 @@ class AgentProtocol(object): | |
} | ||
|
||
def __init__(self, settings, host=None, client_cls=ApplicationModeClient): | ||
try: | ||
from django.conf import settings as django_settings | ||
self._no_report = bool(getattr(django_settings, "EDX_NEWRELIC_NO_REPORT", False)) | ||
except ImportError: | ||
self._no_report = False | ||
|
||
if settings.audit_log_file: | ||
audit_log_fp = open(settings.audit_log_file, "a") | ||
else: | ||
|
@@ -225,6 +356,14 @@ def send( | |
payload=(), | ||
path="/agent_listener/invoke_raw_method", | ||
): | ||
if self._no_report: | ||
if method in FAKE_AGENT_RESPONSE: | ||
return FAKE_AGENT_RESPONSE[method] | ||
else: | ||
_logger.warning(f"NR agent wanted to send unrecognized {method=} -- denied, and will ignore future instances.") | ||
FAKE_AGENT_RESPONSE[method] = {} | ||
return {} | ||
|
||
params, headers, payload = self._to_http(method, payload) | ||
|
||
try: | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may want to move the
connect
response to a Django setting so we can iterate on it if needed. But I suspect it's fine as is while we do the experiment.