diff --git a/CHANGELOG.md b/CHANGELOG.md index cbacc859a9c3a61fe331c7f1e0de9d2e22035e96..397978ea4eb88a54efac6e2874e0869f7b0005a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1 +1,3 @@ # v2.18.9 + +* added `set_maximum_traceback_length` diff --git a/docs/opentracing.rst b/docs/opentracing.rst index 27652559a00ea14ac1ac281c8e517fcb683aeb63..24871c4c11206560003c21f51d30aa232676ae6c 100644 --- a/docs/opentracing.rst +++ b/docs/opentracing.rst @@ -18,3 +18,7 @@ trace_exception .. autofunction:: satella.opentracing.trace_exception +set_maximum_traceback_length +---------------------------- + +.. autofunction:: satella.opentracing.set_maximum_traceback_length diff --git a/satella/__init__.py b/satella/__init__.py index 9e3d5cd9f8cc25778c5eb09912016ecf604bce9a..ea6194d08a490ef16b2914a31e3758c4b63fa621 100644 --- a/satella/__init__.py +++ b/satella/__init__.py @@ -1,2 +1,2 @@ -__version__ = '2.18.9a1' +__version__ = '2.18.9' diff --git a/satella/opentracing/__init__.py b/satella/opentracing/__init__.py index d00a1a01832d4c5f288dd381bed747a44a1d8540..ea16b249d099dc26555f4ff90d807453d5042899 100644 --- a/satella/opentracing/__init__.py +++ b/satella/opentracing/__init__.py @@ -1,4 +1,4 @@ -from .exceptions import trace_exception +from .exceptions import trace_exception, set_maximum_traceback_length from .trace import trace_future, trace_function -__all__ = ['trace_future', 'trace_function', 'trace_exception'] +__all__ = ['trace_future', 'trace_function', 'trace_exception', 'set_maximum_traceback_length'] diff --git a/satella/opentracing/exceptions.py b/satella/opentracing/exceptions.py index f81f75754892f1c6388bed5b1d8b727f39356c7e..89eed5cc768e89fb1ce0fb796cdd2055bb51b9da 100644 --- a/satella/opentracing/exceptions.py +++ b/satella/opentracing/exceptions.py @@ -6,10 +6,23 @@ from satella.coding.typing import ExceptionClassType from satella.instrumentation import Traceback from satella.opentracing.trace import Span +max_traceback_length = 65536 + + +def set_maximum_traceback_length(length: int) -> None: + """ + Set a new maximum traceback length + + :param length: maximum traceback length, in bytes + """ + global max_traceback_length + max_traceback_length = length + def trace_exception(span: tp.Optional[Span], exc_type: tp.Optional[ExceptionClassType] = None, exc_val: tp.Optional[Exception] = None, - exc_tb: tp.Optional[types.TracebackType] = None) -> None: + exc_tb: tp.Optional[types.TracebackType] = None, + max_tb_length: tp.Optional[int] = None) -> None: """ Log an exception's information to the chosen span, as logs and tags. @@ -17,19 +30,28 @@ def trace_exception(span: tp.Optional[Span], exc_type: tp.Optional[ExceptionClas :param exc_type: exception type. If None this will be taken from sys.exc_info. :param exc_val: exception value. If None this will be taken from sys.exc_info. :param exc_tb: exception traceback. If None this will be taken from sys.exc_info. + :param max_tb_length: maximum traceback length. If traceback is longer than that, + it will be trimmed. The default is 65536. You can set it by + :meth:`satella.opentracing.set_maximum_traceback_length` """ if span is None: return + max_tb_length = max_tb_length or max_traceback_length + if exc_type is None: exc_type, exc_val, exc_tb = sys.exc_info() if exc_type is None: return + tb = Traceback(exc_tb.tb_frame).pretty_format() + if len(tb) > max_tb_length: + tb = tb[:max_tb_length] + span.set_tag('error', True) span.log_kv({'event': 'error', 'message': str(exc_val), 'error.object': exc_val, 'error.kind': exc_type, - 'stack': Traceback(exc_tb.tb_frame).pretty_format() + 'stack': tb }) diff --git a/tests/test_opentracing.py b/tests/test_opentracing.py index acbcbc195e3ae53306f454b48b9190efdbed8473..ff869a647d79f1064cf32efda9f72278243ea449 100644 --- a/tests/test_opentracing.py +++ b/tests/test_opentracing.py @@ -1,7 +1,8 @@ import unittest from satella.coding.concurrent import call_in_separate_thread -from satella.opentracing import trace_exception, trace_future, trace_function +from satella.opentracing import trace_exception, trace_future, trace_function, \ + set_maximum_traceback_length from unittest import mock @@ -63,6 +64,7 @@ class TestOpentracing(unittest.TestCase): def test_trace_exception_none(self): trace_exception(None) + set_maximum_traceback_length(32768) span = mock.MagicMock() try: