diff --git a/sdk-extension/opentelemetry-sdk-extension-aws/CHANGELOG.md b/sdk-extension/opentelemetry-sdk-extension-aws/CHANGELOG.md new file mode 100644 index 00000000000..e69de29bb2d diff --git a/sdk-extension/opentelemetry-sdk-extension-aws/LICENSE b/sdk-extension/opentelemetry-sdk-extension-aws/LICENSE new file mode 100644 index 00000000000..e69de29bb2d diff --git a/sdk-extension/opentelemetry-sdk-extension-aws/MANIFEST.rst b/sdk-extension/opentelemetry-sdk-extension-aws/MANIFEST.rst new file mode 100644 index 00000000000..e69de29bb2d diff --git a/sdk-extension/opentelemetry-sdk-extension-aws/README.md b/sdk-extension/opentelemetry-sdk-extension-aws/README.md new file mode 100644 index 00000000000..e69de29bb2d diff --git a/sdk-extension/opentelemetry-sdk-extension-aws/setup.cfg b/sdk-extension/opentelemetry-sdk-extension-aws/setup.cfg new file mode 100644 index 00000000000..1380b526545 --- /dev/null +++ b/sdk-extension/opentelemetry-sdk-extension-aws/setup.cfg @@ -0,0 +1,49 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +[metadata] +name = opentelemetry-sdk-extension +description = AWS SDK extension for OpenTelemetry +long_description = file: README.rst +long_description_content_type = text/x-rst +author = OpenTelemetry Authors +author_email = cncf-opentelemetry-contributors@lists.cncf.io +url = https://github.com/open-telemetry/opentelemetry-python/tree/master/sdk-extension/opentelemetry-sdk-extension-aws +platforms = any +license = Apache-2.0 +classifiers = + Development Status :: 4 - Beta + Intended Audience :: Developers + License :: OSI Approved :: Apache Software License + Programming Language :: Python + Programming Language :: Python :: 3 + Programming Language :: Python :: 3.5 + Programming Language :: Python :: 3.6 + Programming Language :: Python :: 3.7 + Programming Language :: Python :: 3.8 + +[options] +python_requires = >=3.5 +package_dir= + =src +packages=find_namespace: +install_requires = + opentelemetry-api == 0.14.dev0 + +[options.extras_require] +test = + opentelemetry-test == 0.14.dev0 + +[options.packages.find] +where = src diff --git a/sdk-extension/opentelemetry-sdk-extension-aws/setup.py b/sdk-extension/opentelemetry-sdk-extension-aws/setup.py new file mode 100644 index 00000000000..a5b0803743e --- /dev/null +++ b/sdk-extension/opentelemetry-sdk-extension-aws/setup.py @@ -0,0 +1,26 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import os + +import setuptools + +BASE_DIR = os.path.dirname(__file__) +VERSION_FILENAME = os.path.join( + BASE_DIR, "src", "opentelemetry", "sdk", "extension", "aws", "version.py" +) +PACKAGE_INFO = {} +with open(VERSION_FILENAME) as f: + exec(f.read(), PACKAGE_INFO) + +setuptools.setup(version=PACKAGE_INFO["__version__"]) diff --git a/sdk-extension/opentelemetry-sdk-extension-aws/src/opentelemetry/sdk/extension/aws/__init__.py b/sdk-extension/opentelemetry-sdk-extension-aws/src/opentelemetry/sdk/extension/aws/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/sdk-extension/opentelemetry-sdk-extension-aws/src/opentelemetry/sdk/extension/aws/trace/__init__.py b/sdk-extension/opentelemetry-sdk-extension-aws/src/opentelemetry/sdk/extension/aws/trace/__init__.py new file mode 100644 index 00000000000..be8c0984f5c --- /dev/null +++ b/sdk-extension/opentelemetry-sdk-extension-aws/src/opentelemetry/sdk/extension/aws/trace/__init__.py @@ -0,0 +1,19 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from opentelemetry.sdk.extension.aws.trace.aws_xray_ids_generator import ( + AWSXRayIdsGenerator, +) + +__all__ = ["AWSXRayIdsGenerator"] diff --git a/sdk-extension/opentelemetry-sdk-extension-aws/src/opentelemetry/sdk/extension/aws/trace/aws_xray_ids_generator.py b/sdk-extension/opentelemetry-sdk-extension-aws/src/opentelemetry/sdk/extension/aws/trace/aws_xray_ids_generator.py new file mode 100644 index 00000000000..99e2cd1f25f --- /dev/null +++ b/sdk-extension/opentelemetry-sdk-extension-aws/src/opentelemetry/sdk/extension/aws/trace/aws_xray_ids_generator.py @@ -0,0 +1,39 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import datetime +import random + +from opentelemetry import trace + +EPOCH = datetime.datetime.utcfromtimestamp(0) + + +class AWSXRayIdsGenerator(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 + more than 30 days ago are rejected, a purely random `TraceId` risks being + rejected by the service. + + See: https://docs.aws.amazon.com/xray/latest/devguide/xray-api-sendingdata.html#xray-api-traceids + """ + + def generate_span_id(self) -> int: + return trace.RandomIdsGenerator().generate_span_id() + + def generate_trace_id(self) -> int: + trace_time = int(datetime.datetime.now().timestamp()) + trace_identifier = random.getrandbits(96) + return (trace_time << 96) + trace_identifier diff --git a/sdk-extension/opentelemetry-sdk-extension-aws/src/opentelemetry/sdk/extension/aws/version.py b/sdk-extension/opentelemetry-sdk-extension-aws/src/opentelemetry/sdk/extension/aws/version.py new file mode 100644 index 00000000000..0f990278982 --- /dev/null +++ b/sdk-extension/opentelemetry-sdk-extension-aws/src/opentelemetry/sdk/extension/aws/version.py @@ -0,0 +1,15 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +__version__ = "0.14.dev0" diff --git a/sdk-extension/opentelemetry-sdk-extension-aws/tests/__init__.py b/sdk-extension/opentelemetry-sdk-extension-aws/tests/__init__.py new file mode 100644 index 00000000000..b0a6f428417 --- /dev/null +++ b/sdk-extension/opentelemetry-sdk-extension-aws/tests/__init__.py @@ -0,0 +1,13 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/sdk-extension/opentelemetry-sdk-extension-aws/tests/trace/__init__.py b/sdk-extension/opentelemetry-sdk-extension-aws/tests/trace/__init__.py new file mode 100644 index 00000000000..b0a6f428417 --- /dev/null +++ b/sdk-extension/opentelemetry-sdk-extension-aws/tests/trace/__init__.py @@ -0,0 +1,13 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/sdk-extension/opentelemetry-sdk-extension-aws/tests/trace/test_aws_xray_ids_generator.py b/sdk-extension/opentelemetry-sdk-extension-aws/tests/trace/test_aws_xray_ids_generator.py new file mode 100644 index 00000000000..46f012cbaad --- /dev/null +++ b/sdk-extension/opentelemetry-sdk-extension-aws/tests/trace/test_aws_xray_ids_generator.py @@ -0,0 +1,44 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import datetime +import unittest +from unittest import mock + +from opentelemetry import trace as trace_api +from opentelemetry.sdk import resources, trace +from opentelemetry.sdk.extension.aws.trace import AwsXRayIdsGenerator +from opentelemetry.trace.span import INVALID_TRACE_ID + + +class AwsXRayIdsGeneratorTest(unittest.TestCase): + def test_ids_are_valid(self): + ids_generator = AwsXRayIdsGenerator() + for _ in range(1_000): + 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): + trace_id = ids_generator.generate_trace_id() + trace_id_time = trace_id >> 96 + current_time = int(datetime.datetime.now().timestamp()) + self.assertLess(trace_id_time, current_time) + one_month_ago_time = int( + datetime.datetime.now() - datetime.timedelta(30) + ) + self.assertGreater(trace_id_time, one_month_ago_time)