Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 7db21dd

Browse files
authored
Add flask exception information to span attributes (#1188)
1 parent f19e44a commit 7db21dd

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

contrib/opencensus-ext-flask/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
- Add exception information to span attributes
6+
([#1188](https://github.com/census-instrumentation/opencensus-python/pull/1188))
7+
58
## 0.8.1
69
Released 2022-08-03
710

contrib/opencensus-ext-flask/opencensus/ext/flask/flask_middleware.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import logging
1818
import sys
19+
import traceback
1920

2021
import flask
2122
from google.rpc import code_pb2
@@ -40,6 +41,9 @@
4041
HTTP_ROUTE = attributes_helper.COMMON_ATTRIBUTES['HTTP_ROUTE']
4142
HTTP_URL = attributes_helper.COMMON_ATTRIBUTES['HTTP_URL']
4243
HTTP_STATUS_CODE = attributes_helper.COMMON_ATTRIBUTES['HTTP_STATUS_CODE']
44+
ERROR_MESSAGE = attributes_helper.COMMON_ATTRIBUTES['ERROR_MESSAGE']
45+
ERROR_NAME = attributes_helper.COMMON_ATTRIBUTES['ERROR_NAME']
46+
STACKTRACE = attributes_helper.COMMON_ATTRIBUTES['STACKTRACE']
4347

4448
EXCLUDELIST_PATHS = 'EXCLUDELIST_PATHS'
4549
EXCLUDELIST_HOSTNAMES = 'EXCLUDELIST_HOSTNAMES'
@@ -217,16 +221,28 @@ def _teardown_request(self, exception):
217221
code=code_pb2.UNKNOWN,
218222
message=str(exception)
219223
)
220-
# try attaching the stack trace to the span, only populated
221-
# if the app has 'PROPAGATE_EXCEPTIONS', 'DEBUG', or
222-
# 'TESTING' enabled
223-
exc_type, _, exc_traceback = sys.exc_info()
224+
span.add_attribute(
225+
attribute_key=ERROR_NAME,
226+
attribute_value=exception.__class__.__name__)
227+
span.add_attribute(
228+
attribute_key=ERROR_MESSAGE,
229+
attribute_value=str(exception))
230+
231+
if hasattr(exception, '__traceback__'):
232+
exc_traceback = exception.__traceback__
233+
else:
234+
exc_type, _, exc_traceback = sys.exc_info()
224235
if exc_traceback is not None:
225236
span.stack_trace = (
226237
stack_trace.StackTrace.from_traceback(
227238
exc_traceback
228239
)
229240
)
241+
span.add_attribute(
242+
attribute_key=STACKTRACE,
243+
attribute_value='\n'.join(
244+
traceback.format_tb(exc_traceback))
245+
)
230246

231247
tracer.end_span()
232248
tracer.finish()

contrib/opencensus-ext-flask/tests/test_flask_middleware.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,13 @@ def test_teardown_include_exception(self):
383383
exported_spandata.status.canonical_code, code_pb2.UNKNOWN
384384
)
385385
self.assertEqual(exported_spandata.status.description, 'error')
386+
self.assertEqual(
387+
exported_spandata.attributes["error.name"], 'FlaskTestException'
388+
)
389+
self.assertEqual(
390+
exported_spandata.attributes["error.message"], 'error'
391+
)
392+
self.assertIsNotNone(exported_spandata.attributes["error.message"])
386393

387394
def test_teardown_include_exception_and_traceback(self):
388395
mock_exporter = mock.MagicMock()

0 commit comments

Comments
 (0)