Skip to content

Commit a941855

Browse files
committed
Add ids_generator model for trace and span id generation
1 parent 90d7400 commit a941855

File tree

4 files changed

+67
-20
lines changed

4 files changed

+67
-20
lines changed

Diff for: opentelemetry-api/src/opentelemetry/trace/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@
8181
get_current_span,
8282
set_span_in_context,
8383
)
84+
from opentelemetry.trace.ids_generator import (
85+
IdsGenerator
86+
)
8487
from opentelemetry.trace.span import (
8588
DEFAULT_TRACE_OPTIONS,
8689
DEFAULT_TRACE_STATE,
@@ -435,6 +438,7 @@ def get_tracer_provider() -> TracerProvider:
435438
__all__ = [
436439
"DEFAULT_TRACE_OPTIONS",
437440
"DEFAULT_TRACE_STATE",
441+
"IdsGenerator",
438442
"INVALID_SPAN",
439443
"INVALID_SPAN_CONTEXT",
440444
"INVALID_SPAN_ID",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 abc
16+
17+
class IdsGenerator(abc.ABC):
18+
@abc.abstractmethod
19+
def generate_span_id(self) -> int:
20+
"""Get a new random span ID.
21+
22+
Returns:
23+
A random 64-bit int for use as a span ID
24+
"""
25+
26+
27+
@abc.abstractmethod
28+
def generate_trace_id(self) -> int:
29+
"""Get a new random trace ID.
30+
31+
Returns:
32+
A random 128-bit int for use as a trace ID
33+
"""

Diff for: opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py

+5-20
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from opentelemetry.sdk import util
4242
from opentelemetry.sdk.resources import Resource
4343
from opentelemetry.sdk.trace import sampling
44+
from opentelemetry.sdk.trace.random_ids_generator import RandomIdsGenerator
4445
from opentelemetry.sdk.util import BoundedDict, BoundedList
4546
from opentelemetry.sdk.util.instrumentation import InstrumentationInfo
4647
from opentelemetry.trace import SpanContext
@@ -663,24 +664,6 @@ def record_exception(self, exception: Exception) -> None:
663664
)
664665

665666

666-
def generate_span_id() -> int:
667-
"""Get a new random span ID.
668-
669-
Returns:
670-
A random 64-bit int for use as a span ID
671-
"""
672-
return random.getrandbits(64)
673-
674-
675-
def generate_trace_id() -> int:
676-
"""Get a new random trace ID.
677-
678-
Returns:
679-
A random 128-bit int for use as a trace ID
680-
"""
681-
return random.getrandbits(128)
682-
683-
684667
class Tracer(trace_api.Tracer):
685668
"""See `opentelemetry.trace.Tracer`.
686669
@@ -733,7 +716,7 @@ def start_span( # pylint: disable=too-many-locals
733716

734717
if parent_context is None or not parent_context.is_valid:
735718
parent = parent_context = None
736-
trace_id = generate_trace_id()
719+
trace_id = self.source.ids_generator.generate_trace_id()
737720
trace_flags = None
738721
trace_state = None
739722
else:
@@ -757,7 +740,7 @@ def start_span( # pylint: disable=too-many-locals
757740
)
758741
context = trace_api.SpanContext(
759742
trace_id,
760-
generate_span_id(),
743+
self.source.ids_generator.generate_span_id(),
761744
is_remote=False,
762745
trace_flags=trace_flags,
763746
trace_state=trace_state,
@@ -826,10 +809,12 @@ def __init__(
826809
active_span_processor: Union[
827810
SynchronousMultiSpanProcessor, ConcurrentMultiSpanProcessor
828811
] = None,
812+
ids_generator: trace_api.IdsGenerator = RandomIdsGenerator()
829813
):
830814
self._active_span_processor = (
831815
active_span_processor or SynchronousMultiSpanProcessor()
832816
)
817+
self.ids_generator = ids_generator
833818
self.resource = resource
834819
self.sampler = sampler
835820
self._atexit_handler = None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 random
16+
17+
from opentelemetry import trace as trace_api
18+
19+
class RandomIdsGenerator(trace_api.IdsGenerator):
20+
def generate_span_id(self) -> int:
21+
return random.getrandbits(64)
22+
23+
24+
def generate_trace_id(self) -> int:
25+
return random.getrandbits(128)

0 commit comments

Comments
 (0)