Skip to content

Commit 7515447

Browse files
chore(dep): remove jsonschema dependency from package [backport 1.16] (#6295)
Backport 578e85b from #6292 to 1.16. We have had issues recently with newer versions of `jsonschema`. Most importantly, it caused issues with our build/release process (mainly `rpds-py` which is a dependency of `jsonschema`s): https://github.com/DataDog/dd-trace-py/actions/runs/5476876584/jobs/9975064818 Looking over the usage of `jsonschema`, there isn't much it is really doing for us. This PR attempts to replace the usage with other/similar manual checks. If the existing test cases/edge cases continue to pass, then we have successfully replaced `jsonschema`. The risk with this change is a user ends up with an opaque error message. Instead of getting a message saying `max_per_second` must be an integer, they might instead get an operation error when trying to multiply `None` (or some non-integer value) against another number. We can consider adding more/additional checks. ## Checklist - [x] Change(s) are motivated and described in the PR description. - [x] Testing strategy is described if automated tests are not included in the PR. - [x] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) are followed. If no release note is required, add label `changelog/no-changelog`. - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). - [x] Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Title is accurate. - [x] No unnecessary changes are introduced. - [x] Description motivates each change. - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Testing strategy adequately addresses listed risk(s). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] Release note makes sense to a user of the library. - [x] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment. - [x] Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) Co-authored-by: Brett Langdon <[email protected]>
1 parent 5e6437d commit 7515447

File tree

3 files changed

+5
-19
lines changed

3 files changed

+5
-19
lines changed

ddtrace/internal/sampling.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,6 @@ class SamplingMechanism(object):
7272
total=False,
7373
)
7474

75-
SPAN_SAMPLING_JSON_SCHEMA = {
76-
"type": "array",
77-
"items": {
78-
"type": "object",
79-
"anyOf": [
80-
{"properties": {"service": {"type": "string"}}, "required": ["service"]},
81-
{"properties": {"name": {"type": "string"}}, "required": ["name"]},
82-
],
83-
"properties": {"max_per_second": {"type": "integer"}, "sample_rate": {"type": "number"}},
84-
},
85-
}
86-
8775

8876
def _set_trace_tag(
8977
context, # type: Context
@@ -223,16 +211,16 @@ def apply_span_sampling_tags(self, span):
223211
def get_span_sampling_rules():
224212
# type: () -> List[SpanSamplingRule]
225213
json_rules = _get_span_sampling_json()
226-
if json_rules:
227-
from jsonschema import validate
228-
229-
validate(json_rules, SPAN_SAMPLING_JSON_SCHEMA)
230214
sampling_rules = []
231215
for rule in json_rules:
232216
# If sample_rate not specified default to 100%
233217
sample_rate = rule.get("sample_rate", 1.0)
234218
service = rule.get("service")
235219
name = rule.get("name")
220+
221+
if not service and not name:
222+
raise ValueError("Sampling rules must supply at least 'service' or 'name', got {}".format(json.dumps(rule)))
223+
236224
# If max_per_second not specified default to no limit
237225
max_per_second = rule.get("max_per_second", _SINGLE_SPAN_SAMPLING_MAX_PER_SEC_NO_LIMIT)
238226
if service:

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,6 @@ def get_ddup_ext():
481481
"typing_extensions",
482482
"importlib_metadata; python_version<'3.8'",
483483
"pathlib2; python_version<'3.5'",
484-
"jsonschema",
485484
"xmltodict>=0.12",
486485
"ipaddress; python_version<'3.7'",
487486
"envier",

tests/tracer/test_single_span_sampling_rules.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import sys
22

3-
from jsonschema import ValidationError
43
import pytest
54

65
from ddtrace import Tracer
@@ -65,7 +64,7 @@ def test_sampling_rule_init_via_env():
6564

6665
# Testing error thrown when neither name nor service is set
6766
with override_env(dict(DD_SPAN_SAMPLING_RULES='[{"sample_rate":1.0}]')):
68-
with pytest.raises(ValidationError):
67+
with pytest.raises(ValueError):
6968
sampling_rules = get_span_sampling_rules()
7069

7170
# Testing exception thrown when service pattern contains unsupported char

0 commit comments

Comments
 (0)