Skip to content

Commit a47a202

Browse files
authored
Fix open-telemetry#1848 opentracing shim exception reporting (open-telemetry#1878)
1 parent d26699d commit a47a202

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2828
- Update protos to latest version release 0.9.0
2929
([#1873](https://github.com/open-telemetry/opentelemetry-python/pull/1873))
3030

31+
### Fixed
32+
- Updated `opentelementry-opentracing-shim` `ScopeShim` to report exceptions in
33+
opentelemetry specification format, rather than opentracing spec format.
34+
3135
## [1.2.0, 0.21b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.2.0-0.21b0) - 2021-05-11
3236

3337
### Added

shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@
8686
# pylint:disable=no-member
8787

8888
import logging
89-
from typing import Optional, TypeVar, Union
89+
from types import TracebackType
90+
from typing import Optional, Type, TypeVar, Union
9091

9192
from deprecated import deprecated
9293
from opentracing import (
@@ -401,16 +402,24 @@ def close(self):
401402
ends the associated span**, regardless of the value passed in
402403
*finish_on_close* when activating the span.
403404
"""
405+
self._end_span_scope(None, None, None)
404406

405-
detach(self._token)
407+
def __exit__(self, exc_type, exc_val, exc_tb):
408+
"""
409+
Override the __exit__ method of `opentracing.scope.Scope` so we can report
410+
exceptions correctly in opentelemetry specification format.
411+
"""
412+
self._end_span_scope(exc_type, exc_val, exc_tb)
406413

414+
def _end_span_scope(
415+
self,
416+
exc_type: Optional[Type[BaseException]],
417+
exc_val: Optional[BaseException],
418+
exc_tb: Optional[TracebackType],
419+
) -> None:
420+
detach(self._token)
407421
if self._span_cm is not None:
408-
# We don't have error information to pass to `__exit__()` so we
409-
# pass `None` in all arguments. If the OpenTelemetry tracer
410-
# implementation requires this information, the `__exit__()` method
411-
# on `opentracing.Scope` should be overridden and modified to pass
412-
# the relevant values to this `close()` method.
413-
self._span_cm.__exit__(None, None, None)
422+
self._span_cm.__exit__(exc_type, exc_val, exc_tb)
414423
else:
415424
self._span.unwrap().end()
416425

shim/opentelemetry-opentracing-shim/tests/test_shim.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# pylint:disable=no-member
1717

1818
import time
19+
import traceback
1920
from unittest import TestCase
2021
from unittest.mock import Mock
2122

@@ -475,12 +476,30 @@ def test_span_on_error(self):
475476
"""
476477

477478
# Raise an exception while a span is active.
478-
with self.assertRaises(Exception):
479+
with self.assertRaises(Exception) as exc_ctx:
479480
with self.shim.start_active_span("TestName") as scope:
480-
raise Exception
481+
raise Exception("bad thing")
481482

483+
ex = exc_ctx.exception
484+
expected_stack = "".join(
485+
traceback.format_exception(
486+
etype=type(ex), value=ex, tb=ex.__traceback__
487+
)
488+
)
482489
# Verify exception details have been added to span.
483-
self.assertEqual(scope.span.unwrap().attributes["error"], True)
490+
exc_event = scope.span.unwrap().events[0]
491+
492+
self.assertEqual(exc_event.name, "exception")
493+
self.assertEqual(
494+
exc_event.attributes["exception.message"], "bad thing"
495+
)
496+
self.assertEqual(
497+
exc_event.attributes["exception.type"], Exception.__name__
498+
)
499+
# cannot get the whole stacktrace so just assert exception part is contained
500+
self.assertIn(
501+
expected_stack, exc_event.attributes["exception.stacktrace"]
502+
)
484503

485504
def test_inject_http_headers(self):
486505
"""Test `inject()` method for Format.HTTP_HEADERS."""

0 commit comments

Comments
 (0)