Skip to content

Commit

Permalink
Rename IdsGenerator to IdGenerator (#1651)
Browse files Browse the repository at this point in the history
lzchen authored Feb 26, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 61b63e5 commit 290478e
Showing 17 changed files with 99 additions and 101 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ env:
# Otherwise, set variable to the commit of your branch on
# opentelemetry-python-contrib which is compatible with these Core repo
# changes.
CONTRIB_REPO_SHA: 6e328246c895ff433b14430c9edddfead072287c
CONTRIB_REPO_SHA: c86562fcddcf8fd037492de3880790776abe79ce

jobs:
build:
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v0.18b0...HEAD)

### Changed
- Rename `IdsGenerator` to `IdGenerator`
([#1651])(https://github.com/open-telemetry/opentelemetry-python/pull/1651)

## [0.18b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v0.18b0) - 2021-02-16

### Added
7 changes: 7 additions & 0 deletions docs/sdk/trace.id_generator.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
opentelemetry.sdk.trace.id_generator
====================================

.. automodule:: opentelemetry.sdk.trace.id_generator
:members:
:undoc-members:
:show-inheritance:
7 changes: 0 additions & 7 deletions docs/sdk/trace.ids_generator.rst

This file was deleted.

2 changes: 1 addition & 1 deletion docs/sdk/trace.rst
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ Submodules
.. toctree::

trace.export
trace.ids_generator
trace.id_generator
trace.sampling
util.instrumentation

Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
OTEL_PROPAGATORS = "OTEL_PROPAGATORS"
OTEL_PYTHON_CONTEXT = "OTEL_PYTHON_CONTEXT"
OTEL_PYTHON_DISABLED_INSTRUMENTATIONS = "OTEL_PYTHON_DISABLED_INSTRUMENTATIONS"
OTEL_PYTHON_IDS_GENERATOR = "OTEL_PYTHON_IDS_GENERATOR"
OTEL_PYTHON_ID_GENERATOR = "OTEL_PYTHON_ID_GENERATOR"
OTEL_PYTHON_SERVICE_NAME = "OTEL_PYTHON_SERVICE_NAME"
OTEL_TRACES_EXPORTER = "OTEL_TRACES_EXPORTER"
OTEL_PYTHON_TRACER_PROVIDER = "OTEL_PYTHON_TRACER_PROVIDER"
34 changes: 17 additions & 17 deletions opentelemetry-distro/src/opentelemetry/distro/__init__.py
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@

from opentelemetry import trace
from opentelemetry.environment_variables import (
OTEL_PYTHON_IDS_GENERATOR,
OTEL_PYTHON_ID_GENERATOR,
OTEL_PYTHON_SERVICE_NAME,
OTEL_TRACES_EXPORTER,
)
@@ -33,20 +33,20 @@
BatchExportSpanProcessor,
SpanExporter,
)
from opentelemetry.sdk.trace.ids_generator import IdsGenerator
from opentelemetry.sdk.trace.id_generator import IdGenerator

logger = getLogger(__file__)


EXPORTER_OTLP = "otlp"
EXPORTER_OTLP_SPAN = "otlp_span"

RANDOM_IDS_GENERATOR = "random"
_DEFAULT_IDS_GENERATOR = RANDOM_IDS_GENERATOR
RANDOM_ID_GENERATOR = "random"
_DEFAULT_ID_GENERATOR = RANDOM_ID_GENERATOR


def _get_ids_generator() -> str:
return environ.get(OTEL_PYTHON_IDS_GENERATOR, _DEFAULT_IDS_GENERATOR)
def _get_id_generator() -> str:
return environ.get(OTEL_PYTHON_ID_GENERATOR, _DEFAULT_ID_GENERATOR)


def _get_service_name() -> str:
@@ -77,12 +77,12 @@ def _get_exporter_names() -> Sequence[str]:


def _init_tracing(
exporters: Sequence[SpanExporter], ids_generator: IdsGenerator
exporters: Sequence[SpanExporter], id_generator: IdGenerator
):
service_name = _get_service_name()
provider = TracerProvider(
resource=Resource.create({"service.name": service_name}),
ids_generator=ids_generator(),
id_generator=id_generator(),
)
trace.set_tracer_provider(provider)

@@ -141,26 +141,26 @@ def _import_exporters(
return trace_exporters


def _import_ids_generator(ids_generator_name: str) -> IdsGenerator:
def _import_id_generator(id_generator_name: str) -> IdGenerator:
# pylint: disable=unbalanced-tuple-unpacking
[
(ids_generator_name, ids_generator_impl)
(id_generator_name, id_generator_impl)
] = _import_tracer_provider_config_components(
[ids_generator_name.strip()], "opentelemetry_ids_generator"
[id_generator_name.strip()], "opentelemetry_id_generator"
)

if issubclass(ids_generator_impl, IdsGenerator):
return ids_generator_impl
if issubclass(id_generator_impl, IdGenerator):
return id_generator_impl

raise RuntimeError("{0} is not an IdsGenerator".format(ids_generator_name))
raise RuntimeError("{0} is not an IdGenerator".format(id_generator_name))


def _initialize_components():
exporter_names = _get_exporter_names()
trace_exporters = _import_exporters(exporter_names)
ids_generator_name = _get_ids_generator()
ids_generator = _import_ids_generator(ids_generator_name)
_init_tracing(trace_exporters, ids_generator)
id_generator_name = _get_id_generator()
id_generator = _import_id_generator(id_generator_name)
_init_tracing(trace_exporters, id_generator)


class Configurator(BaseConfigurator):
41 changes: 19 additions & 22 deletions opentelemetry-distro/tests/test_configurator.py
Original file line number Diff line number Diff line change
@@ -18,24 +18,21 @@
from unittest.mock import patch

from opentelemetry.distro import (
_get_ids_generator,
_import_ids_generator,
_get_id_generator,
_import_id_generator,
_init_tracing,
)
from opentelemetry.environment_variables import (
OTEL_PYTHON_IDS_GENERATOR,
OTEL_PYTHON_ID_GENERATOR,
OTEL_PYTHON_SERVICE_NAME,
)
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace.ids_generator import (
IdsGenerator,
RandomIdsGenerator,
)
from opentelemetry.sdk.trace.id_generator import IdGenerator, RandomIdGenerator


class Provider:
def __init__(self, resource=None, ids_generator=None):
self.ids_generator = ids_generator
def __init__(self, resource=None, id_generator=None):
self.id_generator = id_generator
self.processor = None
self.resource = resource

@@ -60,7 +57,7 @@ class OTLPExporter:
pass


class CustomIdsGenerator(IdsGenerator):
class CustomIdGenerator(IdGenerator):
def generate_span_id(self):
pass

@@ -103,12 +100,12 @@ def tearDown(self):
# pylint: disable=protected-access
def test_trace_init_default(self):
environ[OTEL_PYTHON_SERVICE_NAME] = "my-test-service"
_init_tracing({"zipkin": Exporter}, RandomIdsGenerator)
_init_tracing({"zipkin": Exporter}, RandomIdGenerator)

self.assertEqual(self.set_provider_mock.call_count, 1)
provider = self.set_provider_mock.call_args[0][0]
self.assertIsInstance(provider, Provider)
self.assertIsInstance(provider.ids_generator, RandomIdsGenerator)
self.assertIsInstance(provider.id_generator, RandomIdGenerator)
self.assertIsInstance(provider.processor, Processor)
self.assertIsInstance(provider.processor.exporter, Exporter)
self.assertEqual(
@@ -117,12 +114,12 @@ def test_trace_init_default(self):

def test_trace_init_otlp(self):
environ[OTEL_PYTHON_SERVICE_NAME] = "my-otlp-test-service"
_init_tracing({"otlp": OTLPExporter}, RandomIdsGenerator)
_init_tracing({"otlp": OTLPExporter}, RandomIdGenerator)

self.assertEqual(self.set_provider_mock.call_count, 1)
provider = self.set_provider_mock.call_args[0][0]
self.assertIsInstance(provider, Provider)
self.assertIsInstance(provider.ids_generator, RandomIdsGenerator)
self.assertIsInstance(provider.id_generator, RandomIdGenerator)
self.assertIsInstance(provider.processor, Processor)
self.assertIsInstance(provider.processor.exporter, OTLPExporter)
self.assertIsInstance(provider.resource, Resource)
@@ -132,17 +129,17 @@ def test_trace_init_otlp(self):
)
del environ[OTEL_PYTHON_SERVICE_NAME]

@patch.dict(environ, {OTEL_PYTHON_IDS_GENERATOR: "custom_ids_generator"})
@patch("opentelemetry.distro.IdsGenerator", new=IdsGenerator)
@patch.dict(environ, {OTEL_PYTHON_ID_GENERATOR: "custom_id_generator"})
@patch("opentelemetry.distro.IdGenerator", new=IdGenerator)
@patch("opentelemetry.distro.iter_entry_points")
def test_trace_init_custom_ids_generator(self, mock_iter_entry_points):
def test_trace_init_custom_id_generator(self, mock_iter_entry_points):
mock_iter_entry_points.configure_mock(
return_value=[
IterEntryPoint("custom_ids_generator", CustomIdsGenerator)
IterEntryPoint("custom_id_generator", CustomIdGenerator)
]
)
ids_generator_name = _get_ids_generator()
ids_generator = _import_ids_generator(ids_generator_name)
_init_tracing({}, ids_generator)
id_generator_name = _get_id_generator()
id_generator = _import_id_generator(id_generator_name)
_init_tracing({}, id_generator)
provider = self.set_provider_mock.call_args[0][0]
self.assertIsInstance(provider.ids_generator, CustomIdsGenerator)
self.assertIsInstance(provider.id_generator, CustomIdGenerator)
2 changes: 1 addition & 1 deletion opentelemetry-instrumentation/README.rst
Original file line number Diff line number Diff line change
@@ -72,7 +72,7 @@ Well known trace exporter names:

When present the value is passed on to the relevant exporter initializer as ``service_name`` argument.

* ``--ids-generator`` or ``OTEL_PYTHON_IDS_GENERATOR``
* ``--id-generator`` or ``OTEL_PYTHON_ID_GENERATOR``

Used to specify which IDs Generator to use for the global Tracer Provider. By default, it
will use the random IDs generator.
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@
from shutil import which

from opentelemetry.environment_variables import (
OTEL_PYTHON_IDS_GENERATOR,
OTEL_PYTHON_ID_GENERATOR,
OTEL_PYTHON_SERVICE_NAME,
OTEL_TRACES_EXPORTER,
)
@@ -51,14 +51,14 @@ def parse_args():
)

parser.add_argument(
"--ids-generator",
"--id-generator",
required=False,
help="""
The IDs Generator to be used with the Tracer Provider.
Examples:
--ids-generator=random
--id-generator=random
""",
)

@@ -85,8 +85,8 @@ def load_config_from_cli_args(args):
environ[OTEL_TRACES_EXPORTER] = args.trace_exporter
if args.service_name:
environ[OTEL_PYTHON_SERVICE_NAME] = args.service_name
if args.ids_generator:
environ[OTEL_PYTHON_IDS_GENERATOR] = args.ids_generator
if args.id_generator:
environ[OTEL_PYTHON_ID_GENERATOR] = args.id_generator


def run() -> None:
4 changes: 2 additions & 2 deletions opentelemetry-sdk/setup.cfg
Original file line number Diff line number Diff line change
@@ -52,8 +52,8 @@ opentelemetry_tracer_provider =
sdk_tracer_provider = opentelemetry.sdk.trace:TracerProvider
opentelemetry_exporter =
console_span = opentelemetry.sdk.trace.export:ConsoleSpanExporter
opentelemetry_ids_generator =
random = opentelemetry.sdk.trace.ids_generator:RandomIdsGenerator
opentelemetry_id_generator =
random = opentelemetry.sdk.trace.id_generator:RandomIdGenerator

[options.extras_require]
test =
23 changes: 10 additions & 13 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
@@ -47,10 +47,7 @@
)
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import sampling
from opentelemetry.sdk.trace.ids_generator import (
IdsGenerator,
RandomIdsGenerator,
)
from opentelemetry.sdk.trace.id_generator import IdGenerator, RandomIdGenerator
from opentelemetry.sdk.util import BoundedDict, BoundedList
from opentelemetry.sdk.util.instrumentation import InstrumentationInfo
from opentelemetry.trace import SpanContext
@@ -840,13 +837,13 @@ def __init__(
span_processor: Union[
SynchronousMultiSpanProcessor, ConcurrentMultiSpanProcessor
],
ids_generator: IdsGenerator,
id_generator: IdGenerator,
instrumentation_info: InstrumentationInfo,
) -> None:
self.sampler = sampler
self.resource = resource
self.span_processor = span_processor
self.ids_generator = ids_generator
self.id_generator = id_generator
self.instrumentation_info = instrumentation_info

@contextmanager
@@ -901,7 +898,7 @@ def start_span( # pylint: disable=too-many-locals
# is_valid determines root span
if parent_span_context is None or not parent_span_context.is_valid:
parent_span_context = None
trace_id = self.ids_generator.generate_trace_id()
trace_id = self.id_generator.generate_trace_id()
trace_flags = None
trace_state = None
else:
@@ -925,7 +922,7 @@ def start_span( # pylint: disable=too-many-locals
)
span_context = trace_api.SpanContext(
trace_id,
self.ids_generator.generate_span_id(),
self.id_generator.generate_span_id(),
is_remote=False,
trace_flags=trace_flags,
trace_state=sampling_result.trace_state,
@@ -1003,15 +1000,15 @@ def __init__(
active_span_processor: Union[
SynchronousMultiSpanProcessor, ConcurrentMultiSpanProcessor
] = None,
ids_generator: IdsGenerator = None,
id_generator: IdGenerator = None,
):
self._active_span_processor = (
active_span_processor or SynchronousMultiSpanProcessor()
)
if ids_generator is None:
self.ids_generator = RandomIdsGenerator()
if id_generator is None:
self.id_generator = RandomIdGenerator()
else:
self.ids_generator = ids_generator
self.id_generator = id_generator
self.resource = resource
self.sampler = sampler
self._atexit_handler = None
@@ -1030,7 +1027,7 @@ def get_tracer(
self.sampler,
self.resource,
self._active_span_processor,
self.ids_generator,
self.id_generator,
InstrumentationInfo(
instrumenting_module_name, instrumenting_library_version
),
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@
import random


class IdsGenerator(abc.ABC):
class IdGenerator(abc.ABC):
@abc.abstractmethod
def generate_span_id(self) -> int:
"""Get a new span ID.
@@ -40,8 +40,8 @@ def generate_trace_id(self) -> int:
"""


class RandomIdsGenerator(IdsGenerator):
"""The default IDs generator for TracerProvider which randomly generates all
class RandomIdGenerator(IdGenerator):
"""The default ID generator for TracerProvider which randomly generates all
bits when generating IDs.
"""

18 changes: 9 additions & 9 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@
OTEL_TRACES_SAMPLER_ARG,
)
from opentelemetry.sdk.trace import Resource, sampling
from opentelemetry.sdk.trace.ids_generator import RandomIdsGenerator
from opentelemetry.sdk.trace.id_generator import RandomIdGenerator
from opentelemetry.sdk.util import ns_to_iso_str
from opentelemetry.sdk.util.instrumentation import InstrumentationInfo
from opentelemetry.trace.status import StatusCode
@@ -791,15 +791,15 @@ def test_invalid_event_attributes(self):
self.assertEqual(root.events[3].attributes, {"attr2": (1, 2)})

def test_links(self):
ids_generator = RandomIdsGenerator()
id_generator = RandomIdGenerator()
other_context1 = trace_api.SpanContext(
trace_id=ids_generator.generate_trace_id(),
span_id=ids_generator.generate_span_id(),
trace_id=id_generator.generate_trace_id(),
span_id=id_generator.generate_span_id(),
is_remote=False,
)
other_context2 = trace_api.SpanContext(
trace_id=ids_generator.generate_trace_id(),
span_id=ids_generator.generate_span_id(),
trace_id=id_generator.generate_trace_id(),
span_id=id_generator.generate_span_id(),
is_remote=False,
)

@@ -1297,12 +1297,12 @@ class TestSpanLimits(unittest.TestCase):
def test_span_environment_limits(self):
reload(trace)
tracer = new_tracer()
ids_generator = RandomIdsGenerator()
id_generator = RandomIdGenerator()
some_links = [
trace_api.Link(
trace_api.SpanContext(
trace_id=ids_generator.generate_trace_id(),
span_id=ids_generator.generate_span_id(),
trace_id=id_generator.generate_trace_id(),
span_id=id_generator.generate_span_id(),
is_remote=False,
)
)
Original file line number Diff line number Diff line change
@@ -94,9 +94,9 @@ def extract(
self._trace_id_regex.fullmatch(trace_id) is None
or self._span_id_regex.fullmatch(span_id) is None
):
ids_generator = trace.get_tracer_provider().ids_generator
trace_id = ids_generator.generate_trace_id()
span_id = ids_generator.generate_span_id()
id_generator = trace.get_tracer_provider().id_generator
trace_id = id_generator.generate_trace_id()
span_id = id_generator.generate_span_id()
sampled = "0"

else:
20 changes: 10 additions & 10 deletions propagator/opentelemetry-propagator-b3/tests/test_b3_format.py
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@

import opentelemetry.propagators.b3 as b3_format # pylint: disable=no-name-in-module,import-error
import opentelemetry.sdk.trace as trace
import opentelemetry.sdk.trace.ids_generator as ids_generator
import opentelemetry.sdk.trace.id_generator as id_generator
import opentelemetry.trace as trace_api
from opentelemetry.context import get_current
from opentelemetry.trace.propagation.textmap import DictGetter
@@ -38,7 +38,7 @@ def get_child_parent_new_carrier(old_carrier):
"child",
trace_api.SpanContext(
parent_span_context.trace_id,
ids_generator.RandomIdsGenerator().generate_span_id(),
id_generator.RandomIdGenerator().generate_span_id(),
is_remote=False,
trace_flags=parent_span_context.trace_flags,
trace_state=parent_span_context.trace_state,
@@ -56,15 +56,15 @@ def get_child_parent_new_carrier(old_carrier):
class TestB3Format(unittest.TestCase):
@classmethod
def setUpClass(cls):
id_generator = ids_generator.RandomIdsGenerator()
generator = id_generator.RandomIdGenerator()
cls.serialized_trace_id = b3_format.format_trace_id(
id_generator.generate_trace_id()
generator.generate_trace_id()
)
cls.serialized_span_id = b3_format.format_span_id(
id_generator.generate_span_id()
generator.generate_span_id()
)
cls.serialized_parent_id = b3_format.format_span_id(
id_generator.generate_span_id()
generator.generate_span_id()
)

def setUp(self) -> None:
@@ -256,10 +256,10 @@ def test_missing_trace_id(self):
self.assertEqual(span_context.trace_id, trace_api.INVALID_TRACE_ID)

@patch(
"opentelemetry.sdk.trace.ids_generator.RandomIdsGenerator.generate_trace_id"
"opentelemetry.sdk.trace.id_generator.RandomIdGenerator.generate_trace_id"
)
@patch(
"opentelemetry.sdk.trace.ids_generator.RandomIdsGenerator.generate_span_id"
"opentelemetry.sdk.trace.id_generator.RandomIdGenerator.generate_span_id"
)
def test_invalid_trace_id(
self, mock_generate_span_id, mock_generate_trace_id
@@ -282,10 +282,10 @@ def test_invalid_trace_id(
self.assertEqual(span_context.span_id, 2)

@patch(
"opentelemetry.sdk.trace.ids_generator.RandomIdsGenerator.generate_trace_id"
"opentelemetry.sdk.trace.id_generator.RandomIdGenerator.generate_trace_id"
)
@patch(
"opentelemetry.sdk.trace.ids_generator.RandomIdsGenerator.generate_span_id"
"opentelemetry.sdk.trace.id_generator.RandomIdGenerator.generate_span_id"
)
def test_invalid_span_id(
self, mock_generate_span_id, mock_generate_trace_id
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@
from unittest.mock import Mock

import opentelemetry.sdk.trace as trace
import opentelemetry.sdk.trace.ids_generator as ids_generator
import opentelemetry.sdk.trace.id_generator as id_generator
import opentelemetry.trace as trace_api
from opentelemetry import baggage
from opentelemetry.propagators import ( # pylint: disable=no-name-in-module
@@ -43,7 +43,7 @@ def get_context_new_carrier(old_carrier, carrier_baggage=None):
"child",
trace_api.SpanContext(
parent_span_context.trace_id,
ids_generator.RandomIdsGenerator().generate_span_id(),
id_generator.RandomIdGenerator().generate_span_id(),
is_remote=False,
trace_flags=parent_span_context.trace_flags,
trace_state=parent_span_context.trace_state,
@@ -68,10 +68,10 @@ def _format_uber_trace_id(trace_id, span_id, parent_span_id, flags):
class TestJaegerPropagator(unittest.TestCase):
@classmethod
def setUpClass(cls):
id_generator = ids_generator.RandomIdsGenerator()
cls.trace_id = id_generator.generate_trace_id()
cls.span_id = id_generator.generate_span_id()
cls.parent_span_id = id_generator.generate_span_id()
generator = id_generator.RandomIdGenerator()
cls.trace_id = generator.generate_trace_id()
cls.span_id = generator.generate_span_id()
cls.parent_span_id = generator.generate_span_id()
cls.serialized_uber_trace_id = _format_uber_trace_id(
cls.trace_id, cls.span_id, cls.parent_span_id, 11
)

0 comments on commit 290478e

Please sign in to comment.