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 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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

from aws_xray_sdk.core.sampling.local.sampler import LocalSampler

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)

def test_pkgutil_static_read(benchmark):
def get_sampling_rule():
json.loads(pkgutil.get_data(__name__, 'mock_sampling_rule.json'))
benchmark(get_sampling_rule)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are really testing the performance of standard library functions (pkgutil and pkg_resources) instead of testing aws-xray-sdk-python functions.

For that reason I think we should not include them here? The PR comment can serve as proof that this change is worth it.

Otherwise, we can leave the tests here so the results can be reproduced later.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with deleting these benchmarks (first thing that came to mind but keeping them also isn't a big deal)

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