From 147b96bd21834fd81ce86db03f11a5e5b31e24a2 Mon Sep 17 00:00:00 2001 From: Vipin Balachandran Date: Tue, 15 Mar 2016 07:26:46 -0700 Subject: Add exception type to stop trace info This patch adds the exception type name to stop trace info. Using this info, it will be easier to identify the methods/ APIs which failed. Change-Id: Iae1b8bea131716d151744526fdc8ed7ac9f3500b --- osprofiler/profiler.py | 6 +++++- osprofiler/tests/test_profiler.py | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/osprofiler/profiler.py b/osprofiler/profiler.py index 7e2c9b1..3ef1443 100644 --- a/osprofiler/profiler.py +++ b/osprofiler/profiler.py @@ -248,7 +248,11 @@ class Trace(object): start(self._name, info=self._info) def __exit__(self, etype, value, traceback): - stop() + if etype: + info = {"etype": reflection.get_class_name(etype)} + stop(info=info) + else: + stop() class _Profiler(object): diff --git a/osprofiler/tests/test_profiler.py b/osprofiler/tests/test_profiler.py index d5a2b8a..fd5d856 100644 --- a/osprofiler/tests/test_profiler.py +++ b/osprofiler/tests/test_profiler.py @@ -156,6 +156,18 @@ class WithTraceTestCase(test.TestCase): mock_stop.reset_mock() mock_stop.assert_called_once_with() + @mock.patch("osprofiler.profiler.stop") + @mock.patch("osprofiler.profiler.start") + def test_with_trace_etype(self, mock_start, mock_stop): + + def foo(): + with profiler.Trace("foo"): + raise ValueError("bar") + + self.assertRaises(ValueError, foo) + mock_start.assert_called_once_with("foo", info=None) + mock_stop.assert_called_once_with(info={"etype": "ValueError"}) + @profiler.trace("function", info={"info": "some_info"}) def tracede_func(i): -- cgit v1.2.1