Skip to content

Commit

Permalink
Set span status on uwsgi errors
Browse files Browse the repository at this point in the history
Signed-off-by: Abhilash Gnan <abhilashgnan@gmail.com>
  • Loading branch information
jan25 committed Jun 27, 2020
1 parent 3c366b2 commit 051b4fc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
This library provides a WSGI middleware that can be used on any WSGI framework
(such as Django / Flask) to track requests timing through OpenTelemetry.
Expand Down Expand Up @@ -211,8 +210,8 @@ def __call__(self, environ, start_response):
return _end_span_after_iterating(
iterable, span, self.tracer, token
)
except: # noqa
# TODO Set span status (cf. https://github.com/open-telemetry/opentelemetry-python/issues/292)
except Exception as ex:
span.set_status(Status(StatusCanonicalCode.INTERNAL, str(ex)))
span.end()
context.detach(token)
raise
Expand Down
24 changes: 23 additions & 1 deletion ext/opentelemetry-ext-wsgi/tests/test_wsgi_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import opentelemetry.ext.wsgi as otel_wsgi
from opentelemetry import trace as trace_api
from opentelemetry.test.wsgitestutil import WsgiTestBase
from opentelemetry.trace.status import StatusCanonicalCode


class Response:
Expand Down Expand Up @@ -73,9 +74,19 @@ def error_wsgi(environ, start_response):
return [b"*"]


def error_wsgi_unhandled(environ, start_response):
assert isinstance(environ, dict)
raise ValueError


class TestWsgiApplication(WsgiTestBase):
def validate_response(
self, response, error=None, span_name="HTTP GET", http_method="GET"
self,
response,
error=None,
span_name="HTTP GET",
span_status=None,
http_method="GET",
):
while True:
try:
Expand Down Expand Up @@ -113,6 +124,8 @@ def validate_response(
if http_method is not None:
expected_attributes["http.method"] = http_method
self.assertEqual(span_list[0].attributes, expected_attributes)
if span_status is not None:
self.assertEqual(span_list[0].status.canonical_code, span_status)

def test_basic_wsgi_call(self):
app = otel_wsgi.OpenTelemetryMiddleware(simple_wsgi)
Expand Down Expand Up @@ -148,6 +161,15 @@ def test_wsgi_exc_info(self):
response = app(self.environ, self.start_response)
self.validate_response(response, error=ValueError)

def test_wsgi_internal_error(self):
app = otel_wsgi.OpenTelemetryMiddleware(error_wsgi_unhandled)
self.assertRaises(ValueError, app, self.environ, self.start_response)
span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)
self.assertEqual(
span_list[0].status.canonical_code, StatusCanonicalCode.INTERNAL,
)

def test_override_span_name(self):
"""Test that span_names can be overwritten by our callback function."""
span_name = "Dymaxion"
Expand Down

0 comments on commit 051b4fc

Please sign in to comment.