Skip to content

Commit fd71136

Browse files
committed
Fix unserializable settings. Closes #16
1 parent cf1918f commit fd71136

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

newrelic/core/agent_protocol.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ def connect(
455455
settings = finalize_application_settings(configuration, settings)
456456

457457
with cls(settings, host=redirect_host, client_cls=client_cls) as protocol:
458-
protocol.send("agent_settings", (global_settings_dump(settings),))
458+
protocol.send("agent_settings", (global_settings_dump(settings, serializable=True),))
459459

460460
if "messages" in configuration:
461461
for item in configuration["messages"]:

newrelic/core/config.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
from newrelic.core.attribute_filter import AttributeFilter
3333

34+
import newrelic.packages.six as six
35+
3436
try:
3537
import urlparse
3638
except ImportError:
@@ -802,7 +804,7 @@ def create_obfuscated_netloc(username, password, hostname, mask):
802804
return netloc
803805

804806

805-
def global_settings_dump(settings_object=None):
807+
def global_settings_dump(settings_object=None, serializable=False):
806808
"""This returns dictionary of global settings flattened into a single
807809
key namespace rather than nested hierarchy. This is used to send the
808810
global settings configuration back to core application.
@@ -814,10 +816,6 @@ def global_settings_dump(settings_object=None):
814816

815817
settings = flatten_settings(settings_object)
816818

817-
# Convert unserializable settings into serializable types
818-
if 'attribute_filter' in settings:
819-
settings['attribute_filter'] = str(settings['attribute_filter'])
820-
821819
# Strip out any sensitive settings.
822820
# The license key is being sent already, but no point sending
823821
# it again.
@@ -863,6 +861,16 @@ def global_settings_dump(settings_object=None):
863861

864862
settings['proxy_host'] = uri
865863

864+
if serializable:
865+
for key, value in list(six.iteritems(settings)):
866+
if not isinstance(key, six.string_types):
867+
del settings[key]
868+
869+
if (not isinstance(value, six.string_types) and
870+
not isinstance(value, float) and
871+
not isinstance(value, six.integer_types)):
872+
settings[key] = repr(value)
873+
866874
return settings
867875

868876

tests/agent_unittests/test_agent_protocol.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import pytest
99

10+
import newrelic.packages.six as six
1011
from newrelic.common import certs, system_info
1112
from newrelic.common.agent_http import DeveloperModeClient
1213
from newrelic.common.encoding_utils import json_decode, serverless_payload_decode
@@ -478,6 +479,13 @@ def test_connect(
478479
assert agent_settings_payload["agent_run_id"] is not None
479480
assert protocol.configuration.agent_run_id is not None
480481

482+
# Verify that agent settings sent have converted null, containers, and
483+
# unserializable types to string
484+
assert agent_settings_payload["proxy_host"] == "None"
485+
assert agent_settings_payload["attributes.include"] == "[]"
486+
assert agent_settings_payload["feature_flag"] == str(set())
487+
assert isinstance(agent_settings_payload["attribute_filter"], six.string_types)
488+
481489
# Verify that the connection is closed
482490
assert HttpClientRecorder.STATE == 0
483491

@@ -532,7 +540,7 @@ def test_serverless_protocol_finalize(capsys):
532540
assert payload[:2] == [1, "NR_LAMBDA_MONITORING"]
533541

534542
data = serverless_payload_decode(payload[2])
535-
assert data["data"] == {"metric_data": [1,2,3]}
543+
assert data["data"] == {"metric_data": [1, 2, 3]}
536544

537545
assert data["metadata"]["foo"] == "bar"
538546
assert data["metadata"]["agent_version"] != "x"
@@ -571,7 +579,9 @@ def __init__(self, *args, **kwargs):
571579

572580

573581
def test_max_payload_size_limit():
574-
settings = finalize_application_settings({"max_payload_size_in_bytes": 0, "port": -1})
582+
settings = finalize_application_settings(
583+
{"max_payload_size_in_bytes": 0, "port": -1}
584+
)
575585
protocol = AgentProtocol(settings, host="localhost")
576586
with pytest.raises(DiscardDataForRequest):
577587
protocol.send("metric_data")

tests/agent_unittests/test_connect_response_fields.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515
import pytest
1616
import functools
1717
import time
18-
from copy import deepcopy
1918

2019
from testing_support.fixtures import override_generic_settings
21-
from newrelic.core.config import global_settings, global_settings_dump
20+
from newrelic.core.config import global_settings
2221
from newrelic.core.agent_protocol import AgentProtocol
2322
from newrelic.common.agent_http import DeveloperModeClient
2423
from newrelic.common.encoding_utils import json_encode
@@ -111,7 +110,6 @@ def test_no_blob_behavior(headers_map_present):
111110
}
112111

113112

114-
115113
def test_blob():
116114
request_headers_map = {u'X-Foo': u'Bar'}
117115
connect_response_fields = {u"request_headers_map": request_headers_map}

0 commit comments

Comments
 (0)