Skip to content

Commit f41f83d

Browse files
authored
All named tracers now share the same context (open-telemetry#424)
The conversation in open-telemetry/opentelemetry-specification#455 is specifying that the tracer is no longer responsible for handling the setting and getting of active spans. As such, named tracers would only be responsible for creating new spans, but not for setting the active one. This implies that there tracers do not have their own active spans. In addition, there is a benefit of having a single active span, as it vastly simplifies the process to modify the current span (no need to explicitly retrieve the tracer responsible for getting the span).
1 parent 18edbbe commit f41f83d

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

opentelemetry-api/src/opentelemetry/trace/propagation/__init__.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,4 @@
1616
from opentelemetry.trace import INVALID_SPAN_CONTEXT, Span, SpanContext
1717

1818
_SPAN_CONTEXT_KEY = "extracted-span-context"
19-
_SPAN_KEY = "current-span"
20-
21-
22-
def get_span_key(tracer_source_id: Optional[str] = None) -> str:
23-
key = _SPAN_KEY
24-
if tracer_source_id is not None:
25-
key = "{}-{}".format(key, tracer_source_id)
26-
return key
19+
SPAN_KEY = "current-span"

opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from opentelemetry.sdk import util
2828
from opentelemetry.sdk.util import BoundedDict, BoundedList
2929
from opentelemetry.trace import SpanContext, sampling
30-
from opentelemetry.trace.propagation import get_span_key
30+
from opentelemetry.trace.propagation import SPAN_KEY
3131
from opentelemetry.trace.status import Status, StatusCanonicalCode
3232
from opentelemetry.util import time_ns, types
3333

@@ -542,9 +542,7 @@ def use_span(
542542
"""See `opentelemetry.trace.Tracer.use_span`."""
543543
try:
544544
context_snapshot = context_api.get_current()
545-
context_api.set_current(
546-
context_api.set_value(self.source.key, span)
547-
)
545+
context_api.set_current(context_api.set_value(SPAN_KEY, span))
548546
try:
549547
yield span
550548
finally:
@@ -577,9 +575,6 @@ def __init__(
577575
sampler: sampling.Sampler = trace_api.sampling.ALWAYS_ON,
578576
shutdown_on_exit: bool = True,
579577
):
580-
# TODO: How should multiple TracerSources behave? Should they get their own contexts?
581-
# This could be done by adding `str(id(self))` to the slot name.
582-
self.key = get_span_key(tracer_source_id=str(id(self)))
583578
self._active_span_processor = MultiSpanProcessor()
584579
self.sampler = sampler
585580
self._atexit_handler = None
@@ -601,8 +596,9 @@ def get_tracer(
601596
),
602597
)
603598

604-
def get_current_span(self) -> Span:
605-
return context_api.get_value(self.key) # type: ignore
599+
@staticmethod
600+
def get_current_span() -> Span:
601+
return context_api.get_value(SPAN_KEY) # type: ignore
606602

607603
def add_span_processor(self, span_processor: SpanProcessor) -> None:
608604
"""Registers a new :class:`SpanProcessor` for this `TracerSource`.

opentelemetry-sdk/tests/trace/test_trace.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,22 @@ def test_span_processor_for_source(self):
201201
span2.span_processor, tracer_source._active_span_processor
202202
)
203203

204+
def test_get_current_span_multiple_tracers(self):
205+
"""In the case where there are multiple tracers,
206+
get_current_span will return the same active span
207+
for both tracers.
208+
"""
209+
tracer_1 = new_tracer()
210+
tracer_2 = new_tracer()
211+
root = tracer_1.start_span("root")
212+
with tracer_1.use_span(root, True):
213+
self.assertIs(tracer_1.get_current_span(), root)
214+
self.assertIs(tracer_2.get_current_span(), root)
215+
216+
# outside of the loop, both should not reference a span.
217+
self.assertIs(tracer_1.get_current_span(), None)
218+
self.assertIs(tracer_2.get_current_span(), None)
219+
204220
def test_start_span_implicit(self):
205221
tracer = new_tracer()
206222

0 commit comments

Comments
 (0)