Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename IdsGenerator to IdGenerator #1651

Merged
merged 8 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions docs/sdk/trace.ids_generator.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
opentelemetry.sdk.trace.ids_generator
opentelemetry.sdk.trace.id_generator
=====================================
lzchen marked this conversation as resolved.
Show resolved Hide resolved

.. automodule:: opentelemetry.sdk.trace.ids_generator
.. automodule:: opentelemetry.sdk.trace.id_generator
:members:
:undoc-members:
:show-inheritance:
2 changes: 1 addition & 1 deletion docs/sdk/trace.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Submodules
.. toctree::

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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,
)
Expand All @@ -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:
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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):
Expand Down
42 changes: 21 additions & 21 deletions opentelemetry-distro/tests/test_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@
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

Expand All @@ -60,7 +60,7 @@ class OTLPExporter:
pass


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

Expand Down Expand Up @@ -103,12 +103,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(
Expand All @@ -117,12 +117,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)
Expand All @@ -132,17 +132,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
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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
""",
)

Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-sdk/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
24 changes: 12 additions & 12 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
)
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
Expand Down Expand Up @@ -840,13 +840,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
Expand Down Expand Up @@ -901,7 +901,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:
Expand All @@ -925,7 +925,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,
Expand Down Expand Up @@ -1003,15 +1003,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
Expand All @@ -1030,7 +1030,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
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -40,7 +40,7 @@ def generate_trace_id(self) -> int:
"""


class RandomIdsGenerator(IdsGenerator):
class RandomIdGenerator(IdGenerator):
"""The default IDs generator for TracerProvider which randomly generates all
lzchen marked this conversation as resolved.
Show resolved Hide resolved
bits when generating IDs.
"""
Expand Down
Loading