Skip to content

Replace builtin json with faster ujson package. #469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ datadog,github.com/DataDog/datadogpy,BSD-3-Clause,"Copyright (c) 2015-Present Da
wrapt,github.com/GrahamDumpleton/wrapt,BSD-2-Clause,"Copyright (c) 2013-2019, Graham Dumpleton"
ddtrace,github.com/DataDog/dd-trace-py,BSD-3-Clause,"Copyright (c) 2016, Datadog <[email protected]>"
urllib3,github.com/urllib3/urllib3,MIT,Copyright (c) 2008-2020 Andrey Petrov and contributors.
ujson,github.com/ultrajson/ultrajson,BSD-3-Clause,"Copyright (c) 2014, Electronic Arts Inc"
importlib_metadata,github.com/python/importlib_metadata,Apache-2.0,Copyright © Jason R. Coombs
boto3,github.com/boto/boto3,Apache-2.0,"Copyright 2013-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved."
typing_extensions,github.com/python/typing_extensions,PSF-2.0,"Copyright (c) 1991 - 1995, Stichting Mathematisch Centrum Amsterdam, The Netherlands. All rights reserved"
requests,github.com/psf/requests,Apache-2.0,"Copyright 2018 Kenneth Reitz"
pytest,github.com/pytest-dev/pytest,MIT,Copyright (c) 2004 Holger Krekel and others
pytest-benchmark,github.com/ionelmc/pytest-benchmark,BSD-2-Clause,"Copyright (c) 2014-2023, Ionel Cristian Mărieș. All rights reserved."
flake8,gitlab.com/pycqa/flake8,MIT,"Copyright (C) 2011-2013 Tarek Ziade <[email protected]>. Copyright (C) 2012-2016 Ian Cordasco <[email protected]>."
5 changes: 3 additions & 2 deletions datadog_lambda/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
# Copyright 2019 Datadog, Inc.

import os
import json
import time
import logging
import ujson as json

from datadog_lambda.extension import should_use_extension
from datadog_lambda.tags import get_enhanced_metrics_tags, dd_lambda_layer_tag
Expand Down Expand Up @@ -85,7 +85,8 @@ def write_metric_point_to_stdout(metric_name, value, timestamp=None, tags=[]):
"v": value,
"e": timestamp or int(time.time()),
"t": tags,
}
},
escape_forward_slashes=False,
)
)

Expand Down
6 changes: 3 additions & 3 deletions datadog_lambda/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2019 Datadog, Inc.

import json
import os
import sys
import logging
import zlib
import ujson as json

from wrapt import wrap_function_wrapper as wrap
from wrapt.importer import when_imported
Expand Down Expand Up @@ -144,14 +144,14 @@ def _print_request_string(request):
data = zlib.decompress(data)
data_dict = json.loads(data)
data_dict.get("series", []).sort(key=lambda series: series.get("metric"))
sorted_data = json.dumps(data_dict)
sorted_data = json.dumps(data_dict, escape_forward_slashes=False)

# Sort headers to prevent any differences in ordering
headers = request.headers or {}
sorted_headers = sorted(
"{}:{}".format(key, value) for key, value in headers.items()
)
sorted_header_str = json.dumps(sorted_headers)
sorted_header_str = json.dumps(sorted_headers, escape_forward_slashes=False)
print(
"HTTP {} {} Headers: {} Data: {}".format(
method, url, sorted_header_str, sorted_data
Expand Down
2 changes: 1 addition & 1 deletion datadog_lambda/tag_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
# Copyright 2021 Datadog, Inc.

from decimal import Decimal
import json
import logging
import ujson as json

redactable_keys = ["authorization", "x-authorization", "password", "token"]
max_depth = 10
Expand Down
2 changes: 1 addition & 1 deletion datadog_lambda/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import hashlib
import logging
import os
import json
import base64
import ujson as json
from datetime import datetime, timezone
from typing import Optional, Dict

Expand Down
2 changes: 1 addition & 1 deletion datadog_lambda/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import base64
import gzip
import json
import ujson as json
from io import BytesIO, BufferedReader
from enum import Enum
from typing import Any
Expand Down
6 changes: 4 additions & 2 deletions datadog_lambda/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import os
import logging
import traceback
import ujson as json
from importlib import import_module
import json
from time import time_ns

from datadog_lambda.extension import should_use_extension, flush_extension
Expand Down Expand Up @@ -258,7 +258,9 @@ def _inject_authorizer_span_headers(self, request_id):
injected_headers[Headers.Parent_Span_Finish_Time] = finish_time_ns
if request_id is not None:
injected_headers[Headers.Authorizing_Request_Id] = request_id
datadog_data = base64.b64encode(json.dumps(injected_headers).encode()).decode()
datadog_data = base64.b64encode(
json.dumps(injected_headers, escape_forward_slashes=False).encode()
).decode()
self.response.setdefault("context", {})
self.response["context"]["_datadog"] = datadog_data

Expand Down
5 changes: 3 additions & 2 deletions datadog_lambda/xray.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os
import logging
import json
import binascii
import time
import socket
import ujson as json

from datadog_lambda.constants import XrayDaemon, XraySubsegment, TraceContextSource

Expand Down Expand Up @@ -102,7 +102,8 @@ def build_segment(context, key, metadata):
key: metadata,
}
},
}
},
escape_forward_slashes=False,
)
return segment

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ python = ">=3.8.0,<4"
datadog = ">=0.41.0,<1.0.0"
wrapt = "^1.11.2"
ddtrace = ">=2.7.2"
ujson = ">=5.9.0"
urllib3 = [
{version = "<2.0.0", python = "<3.11", optional = true},
{version = "<2.1.0", python = ">=3.11", optional = true},
Expand Down
Loading