Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Consistently return ScopeShim and SpanShim objects in the OT shim #242

Closed
@johananl

Description

@johananl

In its current implementation, the OT shim doesn't preserve ScopeShim and SpanShim objects after creating them. This results in situations such as the following:

  1. The instrumentation creates and activates a span using start_active_span(), which results in a ScopeShim object containing a SpanShim object.
  2. The instrumentation then obtains the currently-active span using the shim's active_span property.
  3. The ScopeShim returned by active_span isn't the same object as the one returned from start_active_span().

The reason for the behavior described above is that we only keep state in the OpenTelemetry tracer. We don't really have an OpenTracing tracer or scope manager which keep track of spans. When the user queries the shim's API for the currently-active span for example, we construct a new object from the existing OpenTelemetry object. So the returned object contains the same OpenTelemetry span, but the wrapper objects themselves aren't the same OpenTracing objects as the ones returned to the user during span creation and activation.

There doesn't seem to be an easy way to address this issue. @Oberon00 has some ideas on how this can be implemented.

@property
def active(self):
span = self._tracer.unwrap().get_current_span()
if span is None:
return None
span_context = SpanContextShim(span.get_context())
wrapped_span = SpanShim(self._tracer, span_context, span)
return ScopeShim(self, span=wrapped_span)
# TODO: The returned `ScopeShim` instance here always ends the
# corresponding span, regardless of the `finish_on_close` value used
# when activating the span. This is because here we return a *new*
# `ScopeShim` rather than returning a saved instance of `ScopeShim`.
# https://github.com/open-telemetry/opentelemetry-python/pull/211/files#r335398792

# TODO: We can't check for equality of self.shim.active_span and
# scope.span because the same OpenTelemetry span is returned inside
# different SpanShim objects. A possible solution is described
# here:
# https://github.com/open-telemetry/opentelemetry-python/issues/161#issuecomment-534136274

# TODO: Check equality of the spans themselves rather than
# their context once the SpanShim reconstruction problem has
# been addressed (see previous TODO).

Relevant discussions:

#211 (comment)
#161 (comment)

Metadata

Metadata

Labels

release:required-for-gaTo be resolved before GA releaseshimOpenTracing or OpenCensus compatibilitytracing

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions