diff --git a/opentelemetry-api/src/opentelemetry/trace/__init__.py b/opentelemetry-api/src/opentelemetry/trace/__init__.py index 1795192254f..0750e8913f5 100644 --- a/opentelemetry-api/src/opentelemetry/trace/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/__init__.py @@ -81,6 +81,9 @@ get_current_span, set_span_in_context, ) +from opentelemetry.trace.ids_generator import ( + IdsGenerator +) from opentelemetry.trace.span import ( DEFAULT_TRACE_OPTIONS, DEFAULT_TRACE_STATE, @@ -435,6 +438,7 @@ def get_tracer_provider() -> TracerProvider: __all__ = [ "DEFAULT_TRACE_OPTIONS", "DEFAULT_TRACE_STATE", + "IdsGenerator", "INVALID_SPAN", "INVALID_SPAN_CONTEXT", "INVALID_SPAN_ID", diff --git a/opentelemetry-api/src/opentelemetry/trace/ids_generator.py b/opentelemetry-api/src/opentelemetry/trace/ids_generator.py new file mode 100644 index 00000000000..82a81d2ad32 --- /dev/null +++ b/opentelemetry-api/src/opentelemetry/trace/ids_generator.py @@ -0,0 +1,33 @@ +# 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 abc + +class IdsGenerator(abc.ABC): + @abc.abstractmethod + def generate_span_id(self) -> int: + """Get a new random span ID. + + Returns: + A random 64-bit int for use as a span ID + """ + + + @abc.abstractmethod + def generate_trace_id(self) -> int: + """Get a new random trace ID. + + Returns: + A random 128-bit int for use as a trace ID + """ diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index 13819ed35b0..9b48e1875ca 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -41,6 +41,7 @@ from opentelemetry.sdk import util from opentelemetry.sdk.resources import Resource from opentelemetry.sdk.trace import sampling +from opentelemetry.sdk.trace.random_ids_generator import RandomIdsGenerator from opentelemetry.sdk.util import BoundedDict, BoundedList from opentelemetry.sdk.util.instrumentation import InstrumentationInfo from opentelemetry.trace import SpanContext @@ -663,24 +664,6 @@ def record_exception(self, exception: Exception) -> None: ) -def generate_span_id() -> int: - """Get a new random span ID. - - Returns: - A random 64-bit int for use as a span ID - """ - return random.getrandbits(64) - - -def generate_trace_id() -> int: - """Get a new random trace ID. - - Returns: - A random 128-bit int for use as a trace ID - """ - return random.getrandbits(128) - - class Tracer(trace_api.Tracer): """See `opentelemetry.trace.Tracer`. @@ -733,7 +716,7 @@ def start_span( # pylint: disable=too-many-locals if parent_context is None or not parent_context.is_valid: parent = parent_context = None - trace_id = generate_trace_id() + trace_id = self.source.ids_generator.generate_trace_id() trace_flags = None trace_state = None else: @@ -757,7 +740,7 @@ def start_span( # pylint: disable=too-many-locals ) context = trace_api.SpanContext( trace_id, - generate_span_id(), + self.source.ids_generator.generate_span_id(), is_remote=False, trace_flags=trace_flags, trace_state=trace_state, @@ -826,10 +809,12 @@ def __init__( active_span_processor: Union[ SynchronousMultiSpanProcessor, ConcurrentMultiSpanProcessor ] = None, + ids_generator: trace_api.IdsGenerator = RandomIdsGenerator() ): self._active_span_processor = ( active_span_processor or SynchronousMultiSpanProcessor() ) + self.ids_generator = ids_generator self.resource = resource self.sampler = sampler self._atexit_handler = None diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/random_ids_generator.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/random_ids_generator.py new file mode 100644 index 00000000000..3fa45cd704f --- /dev/null +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/random_ids_generator.py @@ -0,0 +1,25 @@ +# 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 random + +from opentelemetry import trace as trace_api + +class RandomIdsGenerator(trace_api.IdsGenerator): + def generate_span_id(self) -> int: + return random.getrandbits(64) + + + def generate_trace_id(self) -> int: + return random.getrandbits(128)