Skip to content
Snippets Groups Projects
Commit e1c70bc2 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

added set_maximum_traceback_length

parent ba164c86
No related branches found
No related tags found
No related merge requests found
# v2.18.9
* added `set_maximum_traceback_length`
......@@ -18,3 +18,7 @@ trace_exception
.. autofunction:: satella.opentracing.trace_exception
set_maximum_traceback_length
----------------------------
.. autofunction:: satella.opentracing.set_maximum_traceback_length
__version__ = '2.18.9a1'
__version__ = '2.18.9'
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']
......@@ -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
})
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:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment