Skip to content

Commit ab6ce7d

Browse files
committed
Create SDK extension format for AWS SDK Extension
1 parent c8df54b commit ab6ce7d

File tree

13 files changed

+218
-0
lines changed

13 files changed

+218
-0
lines changed

sdk-extension/opentelemetry-sdk-extension-aws/CHANGELOG.md

Whitespace-only changes.

sdk-extension/opentelemetry-sdk-extension-aws/LICENSE

Whitespace-only changes.

sdk-extension/opentelemetry-sdk-extension-aws/MANIFEST.rst

Whitespace-only changes.

sdk-extension/opentelemetry-sdk-extension-aws/README.md

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
[metadata]
16+
name = opentelemetry-sdk-extension
17+
description = AWS SDK extension for OpenTelemetry
18+
long_description = file: README.rst
19+
long_description_content_type = text/x-rst
20+
author = OpenTelemetry Authors
21+
author_email = [email protected]
22+
url = https://github.com/open-telemetry/opentelemetry-python/tree/master/sdk-extension/opentelemetry-sdk-extension-aws
23+
platforms = any
24+
license = Apache-2.0
25+
classifiers =
26+
Development Status :: 4 - Beta
27+
Intended Audience :: Developers
28+
License :: OSI Approved :: Apache Software License
29+
Programming Language :: Python
30+
Programming Language :: Python :: 3
31+
Programming Language :: Python :: 3.5
32+
Programming Language :: Python :: 3.6
33+
Programming Language :: Python :: 3.7
34+
Programming Language :: Python :: 3.8
35+
36+
[options]
37+
python_requires = >=3.5
38+
package_dir=
39+
=src
40+
packages=find_namespace:
41+
install_requires =
42+
opentelemetry-api == 0.14.dev0
43+
44+
[options.extras_require]
45+
test =
46+
opentelemetry-test == 0.14.dev0
47+
48+
[options.packages.find]
49+
where = src
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
import os
15+
16+
import setuptools
17+
18+
BASE_DIR = os.path.dirname(__file__)
19+
VERSION_FILENAME = os.path.join(
20+
BASE_DIR, "src", "opentelemetry", "sdk", "extension", "aws", "version.py"
21+
)
22+
PACKAGE_INFO = {}
23+
with open(VERSION_FILENAME) as f:
24+
exec(f.read(), PACKAGE_INFO)
25+
26+
setuptools.setup(version=PACKAGE_INFO["__version__"])

sdk-extension/opentelemetry-sdk-extension-aws/src/opentelemetry/sdk/extension/aws/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from opentelemetry.sdk.extension.aws.trace.aws_xray_ids_generator import (
16+
AWSXRayIdsGenerator,
17+
)
18+
19+
__all__ = ["AWSXRayIdsGenerator"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import datetime
16+
import random
17+
18+
from opentelemetry import trace
19+
20+
EPOCH = datetime.datetime.utcfromtimestamp(0)
21+
22+
23+
class AWSXRayIdsGenerator(IdsGenerator):
24+
"""Generates tracing IDs compatible with the AWS X-Ray tracing service. In
25+
the X-Ray system, the first 32 bits of the `TraceId` are the Unix epoch time
26+
in seconds. Since spans (AWS calls them segments) with an embedded timestamp
27+
more than 30 days ago are rejected, a purely random `TraceId` risks being
28+
rejected by the service.
29+
30+
See: https://docs.aws.amazon.com/xray/latest/devguide/xray-api-sendingdata.html#xray-api-traceids
31+
"""
32+
33+
def generate_span_id(self) -> int:
34+
return trace.RandomIdsGenerator().generate_span_id()
35+
36+
def generate_trace_id(self) -> int:
37+
trace_time = int(datetime.datetime.now().timestamp())
38+
trace_identifier = random.getrandbits(96)
39+
return (trace_time << 96) + trace_identifier
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
__version__ = "0.14.dev0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import datetime
16+
import unittest
17+
from unittest import mock
18+
19+
from opentelemetry import trace as trace_api
20+
from opentelemetry.sdk import resources, trace
21+
from opentelemetry.sdk.extension.aws.trace import AwsXRayIdsGenerator
22+
from opentelemetry.trace.span import INVALID_TRACE_ID
23+
24+
25+
class AwsXRayIdsGeneratorTest(unittest.TestCase):
26+
def test_ids_are_valid(self):
27+
ids_generator = AwsXRayIdsGenerator()
28+
for _ in range(1_000):
29+
trace_id = ids_generator.generate_trace_id()
30+
self.assertTrue(trace_id != INVALID_TRACE_ID)
31+
span_id = ids_generator.generate_span_id()
32+
self.assertTrue(span_id != INVALID_TRACE_ID)
33+
34+
def test_id_timestamps_are_acceptable_for_xray(self):
35+
ids_generator = AwsXRayIdsGenerator()
36+
for _ in range(1_000):
37+
trace_id = ids_generator.generate_trace_id()
38+
trace_id_time = trace_id >> 96
39+
current_time = int(datetime.datetime.now().timestamp())
40+
self.assertLess(trace_id_time, current_time)
41+
one_month_ago_time = int(
42+
datetime.datetime.now() - datetime.timedelta(30)
43+
)
44+
self.assertGreater(trace_id_time, one_month_ago_time)

0 commit comments

Comments
 (0)