Skip to content

Commit

Permalink
Create SDK extension format for AWS SDK Extension
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanielRN committed Oct 1, 2020
1 parent 982b667 commit 64d4692
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 0 deletions.
Empty file.
Empty file.
Empty file.
Empty file.
49 changes: 49 additions & 0 deletions sdk-extension/opentelemetry-sdk-extension-aws/setup.cfg
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions sdk-extension/opentelemetry-sdk-extension-aws/setup.py
Original file line number Diff line number Diff line change
@@ -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__"])
Original file line number Diff line number Diff line change
@@ -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"]
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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"
13 changes: 13 additions & 0 deletions sdk-extension/opentelemetry-sdk-extension-aws/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 64d4692

Please sign in to comment.