Skip to content

Commit

Permalink
Upate reference version of OTel
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanielRN committed Nov 5, 2020
1 parent 01b26e2 commit f391cd7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 41 deletions.
4 changes: 2 additions & 2 deletions sdk-extension/opentelemetry-sdk-extension-aws/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ package_dir=
=src
packages=find_namespace:
install_requires =
opentelemetry-api == 0.15.dev0
opentelemetry-api == 0.15.b0

[options.entry_points]
opentelemetry_propagator =
aws_xray = opentelemetry.sdk.extension.aws.trace.propagation.aws_xray_format:AwsXRayFormat

[options.extras_require]
test =
opentelemetry-test == 0.14.b0
opentelemetry-test == 0.15.b0

[options.packages.find]
where = src
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,14 @@
)
import opentelemetry.trace as trace_api
from opentelemetry.trace import (
DEFAULT_TRACE_OPTIONS,
DEFAULT_TRACE_STATE,
INVALID_SPAN_CONTEXT,
SpanContext,
TraceFlags,
set_span_in_context,
DEFAULT_TRACE_OPTIONS,
DEFAULT_TRACE_STATE,
INVALID_SPAN_CONTEXT,
SpanContext,
TraceFlags,
set_span_in_context,
TraceState
)
from opentelemetry.trace.propagation.textmap import (
Getter,
Setter,
TextMapPropagatorT,
)
from opentelemetry.trace.span import INVALID_TRACE_ID

TRACE_ID_BASE16 = "8a3c60f7d188f8fa79d48a391a778fa6"
Expand Down Expand Up @@ -71,17 +66,37 @@ def build_test_context(
)

def build_dict_with_xray_trace_header(
trace_id=f"{AwsXRayFormat.TRACE_ID_VERSION}{AwsXRayFormat.TRACE_ID_DELIMITER}{TRACE_ID_BASE16[:AwsXRayFormat.TRACE_ID_FIRST_PART_LENGTH]}{AwsXRayFormat.TRACE_ID_DELIMITER}{TRACE_ID_BASE16[AwsXRayFormat.TRACE_ID_FIRST_PART_LENGTH:]}",
trace_id="{}{}{}{}{}".format(
AwsXRayFormat.TRACE_ID_VERSION,
AwsXRayFormat.TRACE_ID_DELIMITER,
TRACE_ID_BASE16[:AwsXRayFormat.TRACE_ID_FIRST_PART_LENGTH],
AwsXRayFormat.TRACE_ID_DELIMITER,
TRACE_ID_BASE16[AwsXRayFormat.TRACE_ID_FIRST_PART_LENGTH:],
),
span_id=SPAN_ID_BASE16,
sampled="0",
):
carrier = CaseInsensitiveDict()

carrier[AwsXRayFormat.TRACE_HEADER_KEY] = (
f"{AwsXRayFormat.TRACE_ID_KEY}{AwsXRayFormat.KEY_AND_VALUE_DELIMITER}{trace_id}{AwsXRayFormat.KV_PAIR_DELIMITER}"
f"{AwsXRayFormat.PARENT_ID_KEY}{AwsXRayFormat.KEY_AND_VALUE_DELIMITER}{span_id}{AwsXRayFormat.KV_PAIR_DELIMITER}"
f"{AwsXRayFormat.SAMPLED_FLAG_KEY}{AwsXRayFormat.KEY_AND_VALUE_DELIMITER}{sampled}"
)

carrier[AwsXRayFormat.TRACE_HEADER_KEY] = ''.join([
"{}{}{}{}".format(
AwsXRayFormat.TRACE_ID_KEY,
AwsXRayFormat.KEY_AND_VALUE_DELIMITER,
trace_id,
AwsXRayFormat.KV_PAIR_DELIMITER,
),
"{}{}{}{}".format(
AwsXRayFormat.PARENT_ID_KEY,
AwsXRayFormat.KEY_AND_VALUE_DELIMITER,
span_id,
AwsXRayFormat.KV_PAIR_DELIMITER,
),
"{}{}{}".format(
AwsXRayFormat.SAMPLED_FLAG_KEY,
AwsXRayFormat.KEY_AND_VALUE_DELIMITER,
sampled,
)
])

return carrier

Expand All @@ -93,15 +108,15 @@ def get_extracted_span_context(encompassing_context):
).get_span_context()

class AwsXRayPropagatorTest(unittest.TestCase):
carrier_setter: Setter[TextMapPropagatorT] = CaseInsensitiveDict.__setitem__
carrier_getter: Getter[TextMapPropagatorT] = get_as_list
carrier_setter = CaseInsensitiveDict.__setitem__
carrier_getter = get_as_list
XRAY_PROPAGATOR = AwsXRayFormat()

# Inject Tests

def test_inject_into_non_sampled_context(self):
carrier = CaseInsensitiveDict()

AwsXRayPropagatorTest.XRAY_PROPAGATOR.inject(
AwsXRayPropagatorTest.carrier_setter,
carrier,
Expand All @@ -121,13 +136,13 @@ def test_inject_into_non_sampled_context(self):

def test_inject_into_sampled_context(self):
carrier = CaseInsensitiveDict()

AwsXRayPropagatorTest.XRAY_PROPAGATOR.inject(
AwsXRayPropagatorTest.carrier_setter,
carrier,
build_test_context(trace_flags=TraceFlags(TraceFlags.SAMPLED))
)

self.assertTrue(
set(
carrier.items()
Expand All @@ -138,16 +153,16 @@ def test_inject_into_sampled_context(self):
),
'Failed to inject into context that was already sampled'
)

def test_inject_into_context_with_non_default_state(self):
carrier = CaseInsensitiveDict()

AwsXRayPropagatorTest.XRAY_PROPAGATOR.inject(
AwsXRayPropagatorTest.carrier_setter,
carrier,
build_test_context(trace_state=TraceState({"foo" : "bar"}))
)

# TODO: (NathanielRN) Assert trace state when the propagator supports it
self.assertTrue(
set(
Expand Down Expand Up @@ -183,7 +198,7 @@ def test_extract_empty_carrier_from_invalid_context(self):
get_extracted_span_context(actual_context_encompassing_extracted),
INVALID_SPAN_CONTEXT
)

def test_extract_sampled_context(self):
actual_context_encompassing_extracted = AwsXRayPropagatorTest.XRAY_PROPAGATOR.extract(
AwsXRayPropagatorTest.carrier_getter,
Expand Down Expand Up @@ -223,7 +238,7 @@ def test_extract_different_order(self):
get_extracted_span_context(actual_context_encompassing_extracted),
get_extracted_span_context(build_test_context())
)

def test_extract_with_additional_fields(self):
default_xray_trace_header_dict = build_dict_with_xray_trace_header()
xray_trace_header_dict_with_extra_fields = CaseInsensitiveDict(
Expand All @@ -240,7 +255,7 @@ def test_extract_with_additional_fields(self):
get_extracted_span_context(actual_context_encompassing_extracted),
get_extracted_span_context(build_test_context())
)

def test_extract_invalid_xray_trace_header(self):
actual_context_encompassing_extracted = AwsXRayPropagatorTest.XRAY_PROPAGATOR.extract(
AwsXRayPropagatorTest.carrier_getter,
Expand All @@ -253,20 +268,20 @@ def test_extract_invalid_xray_trace_header(self):
get_extracted_span_context(actual_context_encompassing_extracted),
INVALID_SPAN_CONTEXT
)

def test_extract_invalid_trace_id(self):
actual_context_encompassing_extracted = AwsXRayPropagatorTest.XRAY_PROPAGATOR.extract(
AwsXRayPropagatorTest.carrier_getter,
build_dict_with_xray_trace_header(
trace_id="abcdefghijklmnopqrstuvwxyzabcdef"
trace_id="abcdefghijklmnopqrstuvwxyz123456"
),
)

self.assertEqual(
get_extracted_span_context(actual_context_encompassing_extracted),
INVALID_SPAN_CONTEXT
)

def test_extract_invalid_trace_id_size(self):
actual_context_encompassing_extracted = AwsXRayPropagatorTest.XRAY_PROPAGATOR.extract(
AwsXRayPropagatorTest.carrier_getter,
Expand All @@ -292,7 +307,7 @@ def test_extract_invalid_span_id(self):
get_extracted_span_context(actual_context_encompassing_extracted),
INVALID_SPAN_CONTEXT
)

def test_extract_invalid_span_id_size(self):
actual_context_encompassing_extracted = AwsXRayPropagatorTest.XRAY_PROPAGATOR.extract(
AwsXRayPropagatorTest.carrier_getter,
Expand All @@ -305,7 +320,7 @@ def test_extract_invalid_span_id_size(self):
get_extracted_span_context(actual_context_encompassing_extracted),
INVALID_SPAN_CONTEXT
)

def test_extract_invalid_empty_sampled_flag(self):
actual_context_encompassing_extracted = AwsXRayPropagatorTest.XRAY_PROPAGATOR.extract(
AwsXRayPropagatorTest.carrier_getter,
Expand All @@ -318,7 +333,7 @@ def test_extract_invalid_empty_sampled_flag(self):
get_extracted_span_context(actual_context_encompassing_extracted),
INVALID_SPAN_CONTEXT
)

def test_extract_invalid_sampled_flag_size(self):
actual_context_encompassing_extracted = AwsXRayPropagatorTest.XRAY_PROPAGATOR.extract(
AwsXRayPropagatorTest.carrier_getter,
Expand All @@ -331,7 +346,7 @@ def test_extract_invalid_sampled_flag_size(self):
get_extracted_span_context(actual_context_encompassing_extracted),
INVALID_SPAN_CONTEXT
)

def test_extract_invalid_non_numeric_sampled_flag(self):
actual_context_encompassing_extracted = AwsXRayPropagatorTest.XRAY_PROPAGATOR.extract(
AwsXRayPropagatorTest.carrier_getter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
class AwsXRayIdsGeneratorTest(unittest.TestCase):
def test_ids_are_valid(self):
ids_generator = AwsXRayIdsGenerator()
for _ in range(1_000):
for _ in range(1000):
trace_id = ids_generator.generate_trace_id()
self.assertTrue(trace_id != INVALID_TRACE_ID)
span_id = ids_generator.generate_span_id()
self.assertTrue(span_id != INVALID_TRACE_ID)

def test_id_timestamps_are_acceptable_for_xray(self):
ids_generator = AwsXRayIdsGenerator()
for _ in range(1_000):
for _ in range(1000):
trace_id = ids_generator.generate_trace_id()
trace_id_time = trace_id >> 96
current_time = int(datetime.datetime.utcnow().timestamp())
Expand Down

0 comments on commit f391cd7

Please sign in to comment.