Skip to content

Commit

Permalink
Add AWS Propagator Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanielRN committed Nov 4, 2020
1 parent 2a3a8f3 commit 01b26e2
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 173 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
fail-fast: false # ensures the entire test matrix is run, even if one permutation fails
matrix:
python-version: [ py35, py36, py37, py38, pypy3 ]
package: ["instrumentation", "exporter"]
package: ["instrumentation", "exporter", "sdkextension"]
os: [ ubuntu-latest ]
include:
# py35-instrumentation segfaults on 18.04 so we instead run on 20.04
Expand Down
6 changes: 6 additions & 0 deletions sdk-extension/opentelemetry-sdk-extension-aws/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Changelog

## Unreleased

- Provide components needed to Configure OTel SDK for Tracing with AWS X-Ray
([#130](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/130))
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,11 +39,11 @@ package_dir=
=src
packages=find_namespace:
install_requires =
opentelemetry-api == 0.14.b0
opentelemetry-api == 0.15.dev0

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

[options.extras_require]
test =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

from opentelemetry.sdk.extension.aws.trace.aws_xray_ids_generator import (
AWSXRayIdsGenerator,
AwsXRayIdsGenerator,
)

__all__ = ["AWSXRayIdsGenerator"]
__all__ = ["AwsXRayIdsGenerator"]
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from opentelemetry import trace


class AWSXRayIdsGenerator(trace.IdsGenerator):
class AwsXRayIdsGenerator(trace.IdsGenerator):
"""Generates tracing IDs compatible with the AWS X-Ray tracing service. In
the X-Ray system, the first 32 bits of the `TraceId` are the Unix epoch time
in seconds. Since spans (AWS calls them segments) with an embedded timestamp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
_logger = logging.getLogger(__name__)


class AWSXRayFormat(TextMapPropagator):
class AwsXRayFormat(TextMapPropagator):
"""Propagator for the AWS X-Ray Trace Header propagation protocol.
See: https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-tracingheader
Expand Down Expand Up @@ -62,13 +62,21 @@ def extract(
carrier: TextMapPropagatorT,
context: typing.Optional[Context] = None,
) -> Context:
if not carrier:
raise ValueError(("Could not extract from carrier: %s", carrier))
trace_header_list = get_from_carrier(carrier, self.TRACE_HEADER_KEY)

trace_header = get_from_carrier(carrier, self.TRACE_HEADER_KEY)
if not trace_header_list or len(trace_header_list) != 1:
return trace.set_span_in_context(
trace.INVALID_SPAN,
context=context
)

if not trace_header or trace_header == '':
return trace.set_span_in_context(trace.INVALID_SPAN)
trace_header = trace_header_list[0]

if not trace_header:
return trace.set_span_in_context(
trace.INVALID_SPAN,
context=context
)

trace_id = trace.INVALID_TRACE_ID
span_id = trace.INVALID_SPAN_ID
Expand All @@ -91,7 +99,10 @@ def extract(
key_and_value_delimiter_index = stripped_kv_pair.index(self.KEY_AND_VALUE_DELIMITER)
except ValueError as _:
_logger.error(("Error parsing X-Ray trace header. Invalid key value pair: %s. Returning INVALID span context.", kv_pair_subset))
return trace.set_span_in_context(trace.INVALID_SPAN)
return trace.set_span_in_context(
trace.INVALID_SPAN,
context=context
)

value = stripped_kv_pair[key_and_value_delimiter_index + 1:]

Expand All @@ -101,17 +112,37 @@ def extract(
value[self.TRACE_ID_DELIMITER_INDEX_1] != self.TRACE_ID_DELIMITER or
value[self.TRACE_ID_DELIMITER_INDEX_2] != self.TRACE_ID_DELIMITER):
_logger.error(("Invalid TraceId in X-Ray trace header: '%s' with value '%s'. Returning INVALID span context.", self.TRACE_HEADER_KEY, trace_header))
return trace.INVALID_SPAN_CONTEXT
return trace.set_span_in_context(
trace.INVALID_SPAN,
context=context
)

timestamp_subset = value[self.TRACE_ID_DELIMITER_INDEX_1 + 1: self.TRACE_ID_DELIMITER_INDEX_2]
unique_id_subset = value[self.TRACE_ID_DELIMITER_INDEX_2 + 1: self.TRACE_ID_LENGTH]
trace_id = int(timestamp_subset + unique_id_subset, 16)
try:
trace_id = int(timestamp_subset + unique_id_subset, 16)
except ValueError as _:
_logger.error(("Invalid TraceId in X-Ray trace header: '%s' with value '%s'. Returning INVALID span context.", self.TRACE_HEADER_KEY, trace_header))
return trace.set_span_in_context(
trace.INVALID_SPAN,
context=context
)
elif stripped_kv_pair.startswith(self.PARENT_ID_KEY):
if len(value) != self.PARENT_ID_LENGTH:
_logger.error(("Invalid ParentId in X-Ray trace header: '%s' with value '%s'. Returning INVALID span context.", self.TRACE_HEADER_KEY, trace_header))
return trace.INVALID_SPAN_CONTEXT

span_id = int(value, 16)
return trace.set_span_in_context(
trace.INVALID_SPAN,
context=context
)

try:
span_id = int(value, 16)
except ValueError as _:
_logger.error(("Invalid TraceId in X-Ray trace header: '%s' with value '%s'. Returning INVALID span context.", self.TRACE_HEADER_KEY, trace_header))
return trace.set_span_in_context(
trace.INVALID_SPAN,
context=context
)
elif stripped_kv_pair.startswith(self.SAMPLED_FLAG_KEY):
is_sampled_flag_valid = True

Expand All @@ -129,7 +160,10 @@ def extract(

if not is_sampled_flag_valid:
_logger.error(("Invalid Sampling flag in X-Ray trace header: '%s' with value '%s'. Returning INVALID span context.", self.TRACE_HEADER_KEY, trace_header))
return trace.INVALID_SPAN_CONTEXT
return trace.set_span_in_context(
trace.INVALID_SPAN,
context=context
)

options = 0
if sampled:
Expand All @@ -144,7 +178,11 @@ def extract(
)

if not span_context.is_valid:
return context
_logger.error("Invalid Span Extracted. Insertting INVALID span into provided context.")
return trace.set_span_in_context(
trace.INVALID_SPAN,
context=context
)

return trace.set_span_in_context(
trace.DefaultSpan(
Expand All @@ -159,12 +197,9 @@ def inject(
carrier: TextMapPropagatorT,
context: typing.Optional[Context] = None,
) -> None:
if not carrier:
raise ValueError(("Could not extract from carrier: %s", carrier))

span = trace.get_current_span(context=context)

span_context = span.get_context()
span_context = span.get_span_context()
if not span_context.is_valid:
return

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

__version__ = "0.14.b0"
__version__ = "0.15.dev0"
Loading

0 comments on commit 01b26e2

Please sign in to comment.