@@ -101,39 +101,33 @@ class ScopeShim(opentracing.Scope):
101
101
activation/deactivation while using OpenTracing `Scope` objects for
102
102
presentation.
103
103
104
- There are two ways to construct a `ScopeShim` object: using the `span`
105
- argument and using the `span_cm` argument. One and only one of `span` or
106
- `span_cm` must be specified.
107
-
108
- When calling the initializer while passing a `SpanShim` object in the
109
- `span` argument, the `ScopeShim` is initialized as a regular `Scope`
110
- object.
111
-
112
- When calling the initializer while passing a context manager *generator* in
113
- the `span_cm` argument (as returned by the `use_span()` method of
114
- OpenTelemetry `Tracer` objects, the resulting `ScopeShim` becomes
115
- usable as a context manager (using `with` statements).
116
-
117
- It is necessary to have both ways for constructing `ScopeShim`
118
- objects because in some cases we need to create the object from a context
119
- manager, in which case our only way of retrieving a `Span` object is by
120
- calling the `__enter__()` method on the context manager, which makes the
121
- span active in the OpenTelemetry tracer; whereas in other cases we need to
122
- accept a `SpanShim` object and wrap it in a `ScopeShim`.
104
+ There are two ways to construct a `ScopeShim` object: using the default
105
+ initializer and using the `from_context_manager()` class method.
106
+
107
+ It is necessary to have both ways for constructing `ScopeShim` objects
108
+ because in some cases we need to create the object from a context manager,
109
+ in which case our only way of retrieving a `Span` object is by calling the
110
+ `__enter__()` method on the context manager, which makes the span active in
111
+ the OpenTelemetry tracer; whereas in other cases we need to accept a
112
+ `SpanShim` object and wrap it in a `ScopeShim`.
123
113
"""
124
114
125
- def __init__ (self , manager , span = None , span_cm = None ):
115
+ def __init__ (self , manager , span , span_cm = None ):
126
116
super ().__init__ (manager , span )
127
117
self ._span_cm = span_cm
128
118
129
- # If a span context manager is provided, extract the `Span` object from
130
- # it, wrap the extracted span and save it as an attribute.
131
- if self ._span_cm is not None :
132
- otel_span = self ._span_cm .__enter__ ()
133
- span_context = SpanContextShim (otel_span .get_context ())
134
- self ._span = SpanShim (
135
- self ._manager .tracer , span_context , otel_span
136
- )
119
+ # TODO: Change type of `manager` argument to `opentracing.ScopeManager`? We
120
+ # need to get rid of `manager.tracer` for this.
121
+ @classmethod
122
+ def from_context_manager (cls , manager , span_cm ):
123
+ """Constructs a `ScopeShim` from an OpenTelemetry `Span` context
124
+ manager (as returned by `Tracer.use_span()`).
125
+ """
126
+
127
+ otel_span = span_cm .__enter__ ()
128
+ span_context = SpanContextShim (otel_span .get_context ())
129
+ span = SpanShim (manager .tracer , span_context , otel_span )
130
+ return cls (manager , span , span_cm )
137
131
138
132
def close (self ):
139
133
if self ._span_cm is not None :
@@ -159,7 +153,7 @@ def activate(self, span, finish_on_close):
159
153
span_cm = self ._tracer .unwrap ().use_span (
160
154
span .unwrap (), end_on_exit = finish_on_close
161
155
)
162
- return ScopeShim (self , span_cm = span_cm )
156
+ return ScopeShim . from_context_manager (self , span_cm = span_cm )
163
157
164
158
@property
165
159
def active (self ):
0 commit comments