Skip to content

Commit 7fdc0c0

Browse files
committed
Typo and lint fixes
1 parent 06879c1 commit 7fdc0c0

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -321,33 +321,36 @@ def _assert_exemplars_present(
321321
):
322322
metrics_list = self.memory_metrics_reader.get_metrics_data()
323323
print(metrics_list)
324-
found = {name: 0 for name in metric_names}
324+
metrics = []
325325
for resource_metric in (
326326
getattr(metrics_list, "resource_metrics", []) or []
327327
):
328328
for scope_metric in (
329329
getattr(resource_metric, "scope_metrics", []) or []
330330
):
331-
for metric in getattr(scope_metric, "metrics", []) or []:
332-
if metric.name not in metric_names:
333-
continue
334-
for point in metric.data.data_points:
335-
found[metric.name] += 1
336-
exemplars = getattr(point, "exemplars", None)
337-
self.assertIsNotNone(
338-
exemplars,
339-
msg=f"Expected exemplars list attribute on histogram data point for {metric.name} ({context})",
340-
)
341-
self.assertGreater(
342-
len(exemplars or []),
343-
0,
344-
msg=f"Expected at least one exemplar on histogram data point for {metric.name} ({context}) but none found.",
345-
)
346-
for ex in exemplars or []:
347-
if hasattr(ex, "span_id"):
348-
self.assertNotEqual(ex.span_id, 0)
349-
if hasattr(ex, "trace_id"):
350-
self.assertNotEqual(ex.trace_id, 0)
331+
metrics.extend(getattr(scope_metric, "metrics", []) or [])
332+
333+
found = {name: 0 for name in metric_names}
334+
for metric in metrics:
335+
if metric.name not in metric_names:
336+
continue
337+
for point in metric.data.data_points:
338+
found[metric.name] += 1
339+
exemplars = getattr(point, "exemplars", None)
340+
self.assertIsNotNone(
341+
exemplars,
342+
msg=f"Expected exemplars list attribute on histogram data point for {metric.name} ({context})",
343+
)
344+
self.assertGreater(
345+
len(exemplars or []),
346+
0,
347+
msg=f"Expected at least one exemplar on histogram data point for {metric.name} ({context}) but none found.",
348+
)
349+
for ex in exemplars or []:
350+
if hasattr(ex, "span_id"):
351+
self.assertNotEqual(ex.span_id, 0)
352+
if hasattr(ex, "trace_id"):
353+
self.assertNotEqual(ex.trace_id, 0)
351354
for name, count in found.items():
352355
self.assertGreater(
353356
count,

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,7 @@ def _start_response(
654654
return _start_response
655655

656656
# pylint: disable=too-many-branches
657+
# pylint: disable=too-many-locals
657658
def __call__(
658659
self, environ: WSGIEnvironment, start_response: StartResponse
659660
):
@@ -705,13 +706,16 @@ def __call__(
705706
self._sem_conv_opt_in_mode,
706707
)
707708
iterable = self.wsgi(environ, start_response)
708-
return _iterate_and_close_with_span(iterable, span, token)
709+
return _end_span_after_iterating(iterable, span, token)
709710
except Exception as ex:
710711
if _report_new(self._sem_conv_opt_in_mode):
711712
req_attrs[ERROR_TYPE] = type(ex).__qualname__
712713
if span.is_recording():
713714
span.set_attribute(ERROR_TYPE, type(ex).__qualname__)
714715
span.set_status(Status(StatusCode.ERROR, str(ex)))
716+
span.end()
717+
if token is not None:
718+
context.detach(token)
715719
raise
716720
finally:
717721
duration_s = default_timer() - start
@@ -735,16 +739,13 @@ def __call__(
735739
context=active_metric_ctx,
736740
)
737741
self.active_requests_counter.add(-1, active_requests_count_attrs)
738-
span.end()
739-
if token is not None:
740-
context.detach(token)
741742

742743

743744
# Put this in a subfunction to not delay the call to the wrapped
744745
# WSGI application (instrumentation should change the application
745746
# behavior as little as possible).
746-
def _iterate_and_close_with_span(
747-
iterable: Iterable[T], span: trace.Span
747+
def _end_span_after_iterating(
748+
iterable: Iterable[T], span: trace.Span, token: object
748749
) -> Iterable[T]:
749750
try:
750751
with trace.use_span(span):
@@ -753,6 +754,9 @@ def _iterate_and_close_with_span(
753754
close = getattr(iterable, "close", None)
754755
if close:
755756
close()
757+
span.end()
758+
if token is not None:
759+
context.detach(token)
756760

757761

758762
# TODO: inherit from opentelemetry.instrumentation.propagators.Setter

0 commit comments

Comments
 (0)