-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: rearrange some code to prepare for more span pointers (#3207)
Co-authored-by: Christophe Papazian <114495376+christophe-papazian@users.noreply.github.com>
- Loading branch information
1 parent
be6bac6
commit 2a6b751
Showing
21 changed files
with
160 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
48 changes: 48 additions & 0 deletions
48
tests/serverless/span_pointers/aws/test_s3_span_pointers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import json | ||
|
||
from utils import weblog, interfaces, rfc, features | ||
from utils.tools import logger | ||
from tests.serverless.span_pointers.utils import ( | ||
POINTER_DIRECTION_DOWNSTREAM, | ||
make_single_span_link_validator, | ||
standard_hashing_function, | ||
) | ||
|
||
|
||
def _validate_s3_object_pointer(r): | ||
assert r.status_code == 200 | ||
|
||
response_content = json.loads(r.text) | ||
bucket = r.request.params["bucket"].encode("ascii") | ||
key = r.request.params["key"].encode("utf-8") | ||
etag = response_content["object"]["e_tag"].encode("ascii") | ||
|
||
assert b'"' not in etag, "boto3 sometimes includes double-quotes in etags" | ||
|
||
logger.info(f"bucket: {bucket}, key: {key}, etag: {etag}") | ||
|
||
interfaces.library.validate_spans( | ||
r, | ||
validator=make_single_span_link_validator( | ||
pointer_kind="aws.s3.object", | ||
pointer_direction=POINTER_DIRECTION_DOWNSTREAM, | ||
pointer_hash=standard_hashing_function([bucket, key, etag]), | ||
), | ||
full_trace=True, | ||
) | ||
|
||
|
||
@rfc("https://github.com/DataDog/dd-span-pointer-rules") | ||
@features.serverless_span_pointers | ||
class Test_PutObject: | ||
def setup_main(self): | ||
self.r = weblog.get("/mock_s3/put_object", params={"bucket": "mybucket", "key": "my-key"}) | ||
|
||
def test_main(self): | ||
_validate_s3_object_pointer(self.r) | ||
|
||
def setup_non_ascii(self): | ||
self.r_non_ascii = weblog.get("/mock_s3/put_object", params={"bucket": "mybucket", "key": "some-key.你好"}) | ||
|
||
def test_non_ascii(self): | ||
_validate_s3_object_pointer(self.r_non_ascii) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from hashlib import sha256 | ||
from typing import NewType | ||
|
||
|
||
PointerHash = NewType("PointerHash", str) | ||
|
||
POINTER_DIRECTION_DOWNSTREAM = "d" | ||
POINTER_DIRECTION_UPSTREAM = "d" | ||
|
||
|
||
# copyied from https://github.com/DataDog/dd-span-pointer-rules/blob/main/test_py/test_rules.py | ||
def standard_hashing_function(elements: list[bytes]) -> PointerHash: | ||
assert isinstance(elements, list) | ||
assert elements | ||
assert all(isinstance(element, bytes) for element in elements) | ||
|
||
separator = b"|" | ||
bits_per_hex_digit = 4 | ||
desired_bits = 128 | ||
hex_digits = desired_bits // bits_per_hex_digit | ||
|
||
hex_digest = sha256(separator.join(elements)).hexdigest() | ||
assert len(hex_digest) >= hex_digits | ||
|
||
return PointerHash(hex_digest[:hex_digits]) | ||
|
||
|
||
def make_single_span_link_validator( | ||
pointer_kind: str, pointer_direction: str, pointer_hash: PointerHash, | ||
): | ||
""" | ||
Make a validator function for use with interfaces.library.validate_spans. | ||
The validator checks that there is one and only one span pointer for the | ||
pointer_kind and pointer_direction and that its hash matches the | ||
pointer_hash. | ||
""" | ||
|
||
def validator(span): | ||
if "span_links" not in span: | ||
return | ||
|
||
found_matching = False | ||
|
||
for span_link in span["span_links"]: | ||
attributes = span_link.get("attributes", {}) | ||
if not attributes: | ||
continue | ||
|
||
if attributes.get("ptr.kind") != pointer_kind: | ||
continue | ||
|
||
if attributes.get("ptr.dir") != pointer_direction: | ||
continue | ||
|
||
assert not found_matching | ||
|
||
assert attributes.get("ptr.hash") == pointer_hash | ||
found_matching = True | ||
# we loop oonwards in caes there's unexpectedly more than one | ||
|
||
return found_matching | ||
|
||
return validator |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM datadog/system-tests:django-poc.base-v3 | ||
FROM datadog/system-tests:django-poc.base-v4 | ||
|
||
WORKDIR /app | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM datadog/system-tests:fastapi.base-v3 | ||
FROM datadog/system-tests:fastapi.base-v4 | ||
|
||
WORKDIR /app | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.