Skip to content
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

Replace slow json filename resolver #306

Merged
merged 3 commits into from
Jul 28, 2021
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
6 changes: 2 additions & 4 deletions aws_xray_sdk/core/sampling/local/sampler.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import json
import pkgutil
from random import Random

from pkg_resources import resource_filename
from .sampling_rule import SamplingRule
from ...exceptions.exceptions import InvalidSamplingManifestError


with open(resource_filename(__name__, 'sampling_rule.json')) as f:
local_sampling_rule = json.load(f)
local_sampling_rule = json.loads(pkgutil.get_data(__name__, 'sampling_rule.json'))

SUPPORTED_RULE_VERSION = (1, 2)

Expand Down
5 changes: 2 additions & 3 deletions aws_xray_sdk/ext/boto_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import absolute_import
# Need absolute import as botocore is also in the current folder for py27
import json
import pkgutil

from pkg_resources import resource_filename
from botocore.exceptions import ClientError

from aws_xray_sdk.core import xray_recorder
Expand All @@ -12,8 +12,7 @@
from aws_xray_sdk.ext.util import inject_trace_header, to_snake_case


with open(resource_filename(__name__, 'resources/aws_para_whitelist.json'), 'r') as data_file:
whitelist = json.load(data_file)
whitelist = json.loads(pkgutil.get_data(__name__, 'resources/aws_para_whitelist.json'))


def inject_header(wrapped, instance, args, kwargs):
Expand Down
9 changes: 9 additions & 0 deletions tests/mock_sampling_rule.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"version": 2,
"default": {
"fixed_target": 1,
"rate": 0.05
},
"rules": [
]
}
16 changes: 16 additions & 0 deletions tests/test_local_sampling_benchmark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import json
import pkgutil
from pkg_resources import resource_filename

# Faster
def test_pkgutil_static_read(benchmark):
def get_sampling_rule():
return json.loads(pkgutil.get_data(__name__, 'mock_sampling_rule.json'))
benchmark(get_sampling_rule)

# Slower
def test_pkg_resources_static_read(benchmark):
def get_sampling_rule():
with open(resource_filename(__name__, 'mock_sampling_rule.json')) as f:
return json.load(f)
benchmark(get_sampling_rule)
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ skip_missing_interpreters = True
passenv = TOXENV CI TRAVIS TRAVIS_* CODECOV_*
deps =
pytest > 3.0.0
pytest-benchmark
coverage==4.5.4
codecov
requests
Expand Down