diff --git a/newrelic/common/encoding_utils.py b/newrelic/common/encoding_utils.py index fd62ebcad..3620c7913 100644 --- a/newrelic/common/encoding_utils.py +++ b/newrelic/common/encoding_utils.py @@ -28,6 +28,7 @@ import types import zlib from collections import OrderedDict +from pathlib import Path HEXDIGLC_RE = re.compile("^[0-9a-f]+$") DELIMITER_FORMAT_RE = re.compile("[ \t]*,[ \t]*") @@ -66,6 +67,8 @@ def json_encode(obj, **kwargs): def _encode(o): if isinstance(o, bytes): return o.decode("latin-1") + elif isinstance(o, Path): + return str(o) elif isinstance(o, types.GeneratorType): return list(o) elif hasattr(o, "__iter__"): diff --git a/tests/agent_unittests/test_encoding_utils.py b/tests/agent_unittests/test_encoding_utils.py index 59cf53fd1..949498019 100644 --- a/tests/agent_unittests/test_encoding_utils.py +++ b/tests/agent_unittests/test_encoding_utils.py @@ -12,9 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +from pathlib import Path + import pytest -from newrelic.common.encoding_utils import camel_case, snake_case +from newrelic.common.encoding_utils import camel_case, json_encode, snake_case @pytest.mark.parametrize( @@ -55,3 +57,27 @@ def test_camel_case(input_, expected, upper): def test_snake_case(input_, expected): output = snake_case(input_) assert output == expected + + +def _generator(): + # range() itself is not a generator + yield from range(1, 4) + + +@pytest.mark.parametrize( + "input_,expected", + [ + (10, "10"), + (10.0, "10.0"), + ("my_string", '"my_string"'), + (b"my_bytes", '"my_bytes"'), + ({"id": 1, "name": "test", "NoneType": None}, '{"id":1,"name":"test","NoneType":null}'), + (_generator(), "[1,2,3]"), + (tuple(range(4, 7)), "[4,5,6]"), + (Path("test/path/file.txt"), '"test/path/file.txt"'), + ], + ids=["int", "float", "str", "bytes", "dict", "generator", "iterable", "Path"], +) +def test_json_encode(input_, expected): + output = json_encode(input_) + assert output == expected