Skip to content

Commit 0f37eec

Browse files
committed
Fix active span in WSGI instrumentation.
1 parent dd098ed commit 0f37eec

File tree

1 file changed

+21
-19
lines changed
  • ext/opentelemetry-ext-wsgi/src/opentelemetry/ext/wsgi

1 file changed

+21
-19
lines changed

ext/opentelemetry-ext-wsgi/src/opentelemetry/ext/wsgi/__init__.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,25 +112,27 @@ def __call__(self, environ, start_response):
112112
)
113113
span.start()
114114
try:
115-
self._add_request_attributes(span, environ)
116-
start_response = self._create_start_response(span, start_response)
117-
118-
iterable = self.wsgi(environ, start_response)
119-
120-
# Put this in a subfunction to not delay the call to the wrapped
121-
# WSGI application (instrumentation should change the application
122-
# behavior as little as possible).
123-
def iter_result(iterable, span):
124-
try:
125-
for yielded in iterable:
126-
yield yielded
127-
finally:
128-
close = getattr(iterable, "close", None)
129-
if close:
130-
close()
131-
span.end()
132-
133-
return iter_result(iterable, span)
115+
with tracer.use_span(span):
116+
self._add_request_attributes(span, environ)
117+
start_response = self._create_start_response(span, start_response)
118+
119+
iterable = self.wsgi(environ, start_response)
120+
121+
# Put this in a subfunction to not delay the call to the wrapped
122+
# WSGI application (instrumentation should change the application
123+
# behavior as little as possible).
124+
def iter_result(iterable, span):
125+
try:
126+
with tracer.use_span(span):
127+
for yielded in iterable:
128+
yield yielded
129+
finally:
130+
close = getattr(iterable, "close", None)
131+
if close:
132+
close()
133+
span.end()
134+
135+
return iter_result(iterable, span)
134136
except: # noqa
135137
span.end()
136138
raise

0 commit comments

Comments
 (0)