Skip to content

Commit

Permalink
Add flask exception information to span attributes (#1188)
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen authored Feb 7, 2023
1 parent f19e44a commit 7db21dd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
3 changes: 3 additions & 0 deletions contrib/opencensus-ext-flask/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

- Add exception information to span attributes
([#1188](https://github.com/census-instrumentation/opencensus-python/pull/1188))

## 0.8.1
Released 2022-08-03

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import logging
import sys
import traceback

import flask
from google.rpc import code_pb2
Expand All @@ -40,6 +41,9 @@
HTTP_ROUTE = attributes_helper.COMMON_ATTRIBUTES['HTTP_ROUTE']
HTTP_URL = attributes_helper.COMMON_ATTRIBUTES['HTTP_URL']
HTTP_STATUS_CODE = attributes_helper.COMMON_ATTRIBUTES['HTTP_STATUS_CODE']
ERROR_MESSAGE = attributes_helper.COMMON_ATTRIBUTES['ERROR_MESSAGE']
ERROR_NAME = attributes_helper.COMMON_ATTRIBUTES['ERROR_NAME']
STACKTRACE = attributes_helper.COMMON_ATTRIBUTES['STACKTRACE']

EXCLUDELIST_PATHS = 'EXCLUDELIST_PATHS'
EXCLUDELIST_HOSTNAMES = 'EXCLUDELIST_HOSTNAMES'
Expand Down Expand Up @@ -217,16 +221,28 @@ def _teardown_request(self, exception):
code=code_pb2.UNKNOWN,
message=str(exception)
)
# try attaching the stack trace to the span, only populated
# if the app has 'PROPAGATE_EXCEPTIONS', 'DEBUG', or
# 'TESTING' enabled
exc_type, _, exc_traceback = sys.exc_info()
span.add_attribute(
attribute_key=ERROR_NAME,
attribute_value=exception.__class__.__name__)
span.add_attribute(
attribute_key=ERROR_MESSAGE,
attribute_value=str(exception))

if hasattr(exception, '__traceback__'):
exc_traceback = exception.__traceback__
else:
exc_type, _, exc_traceback = sys.exc_info()
if exc_traceback is not None:
span.stack_trace = (
stack_trace.StackTrace.from_traceback(
exc_traceback
)
)
span.add_attribute(
attribute_key=STACKTRACE,
attribute_value='\n'.join(
traceback.format_tb(exc_traceback))
)

tracer.end_span()
tracer.finish()
Expand Down
7 changes: 7 additions & 0 deletions contrib/opencensus-ext-flask/tests/test_flask_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,13 @@ def test_teardown_include_exception(self):
exported_spandata.status.canonical_code, code_pb2.UNKNOWN
)
self.assertEqual(exported_spandata.status.description, 'error')
self.assertEqual(
exported_spandata.attributes["error.name"], 'FlaskTestException'
)
self.assertEqual(
exported_spandata.attributes["error.message"], 'error'
)
self.assertIsNotNone(exported_spandata.attributes["error.message"])

def test_teardown_include_exception_and_traceback(self):
mock_exporter = mock.MagicMock()
Expand Down

0 comments on commit 7db21dd

Please sign in to comment.