def test_middleware_exception(self): """Ensure exceptions raised in middleware are properly handled. Uncaught exceptions should result in error tagged spans. """ from .app.middleware import ExceptionMiddleware wsgiapp = ExceptionMiddleware(self._wsgiapp) app = PylonsTraceMiddleware(wsgiapp, self.tracer, service='web') app = fixture.TestApp(app) with pytest.raises(Exception): app.get(url_for(controller='root', action='index')) spans = self.tracer.writer.pop() assert spans, spans assert len(spans) == 1 span = spans[0] assert span.service == 'web' assert span.resource == 'None.None' assert span.error == 1 assert span.get_tag('http.status_code') == '500' assert span.get_tag(errors.ERROR_MSG) == 'Middleware exception' assert span.get_tag(errors.ERROR_TYPE) == 'exceptions.Exception' assert span.get_tag(errors.ERROR_STACK)
def test_controller_exception(self): """Ensure exceptions thrown in controllers can be handled. No error tags should be set in the span. """ from .app.middleware import ExceptionToSuccessMiddleware wsgiapp = ExceptionToSuccessMiddleware(self._wsgiapp) app = PylonsTraceMiddleware(wsgiapp, self.tracer, service='web') app = fixture.TestApp(app) app.get(url_for(controller='root', action='raise_exception')) spans = self.tracer.writer.pop() assert spans, spans assert len(spans) == 1 span = spans[0] assert span.service == 'web' assert span.resource == 'root.raise_exception' assert span.error == 0 assert span.get_tag('http.status_code') == '200' assert span.get_tag(errors.ERROR_MSG) is None assert span.get_tag(errors.ERROR_TYPE) is None assert span.get_tag(errors.ERROR_STACK) is None assert span.span_type == 'http'
def test_mw_exc_success(self): """Ensure exceptions can be properly handled by other middleware. No error should be reported in the span. """ from .app.middleware import ExceptionMiddleware, ExceptionToSuccessMiddleware wsgiapp = ExceptionMiddleware(self._wsgiapp) wsgiapp = ExceptionToSuccessMiddleware(wsgiapp) app = PylonsTraceMiddleware(wsgiapp, self.tracer, service='web') app = fixture.TestApp(app) app.get(url_for(controller='root', action='index')) spans = self.tracer.writer.pop() assert spans, spans assert len(spans) == 1 span = spans[0] assert span.service == 'web' assert span.resource == 'None.None' assert span.error == 0 assert span.get_tag('http.status_code') == '200' assert span.get_tag(errors.ERROR_MSG) is None assert span.get_tag(errors.ERROR_TYPE) is None assert span.get_tag(errors.ERROR_STACK) is None
def test_mw_exc_success(self): """Ensure exceptions can be properly handled by other middleware. No error should be reported in the span. """ from .app.middleware import ExceptionMiddleware from .app.middleware import ExceptionToSuccessMiddleware wsgiapp = ExceptionMiddleware(self._wsgiapp) wsgiapp = ExceptionToSuccessMiddleware(wsgiapp) app = PylonsTraceMiddleware(wsgiapp, self.tracer, service="web") app = fixture.TestApp(app) app.get(url_for(controller="root", action="index")) spans = self.pop_spans() assert spans, spans assert len(spans) == 1 span = spans[0] assert_is_measured(span) assert span.service == "web" assert span.resource == "None.None" assert span.error == 0 assert span.get_tag(http.URL) == "http://localhost:80/" assert_span_http_status_code(span, 200) assert span.get_tag(errors.ERROR_MSG) is None assert span.get_tag(errors.ERROR_TYPE) is None assert span.get_tag(errors.ERROR_STACK) is None
def setUp(self): super(PylonsTestCase, self).setUp() # initialize a real traced Pylons app wsgiapp = loadapp('config:test.ini', relative_to=PylonsTestCase.conf_dir) self._wsgiapp = wsgiapp app = PylonsTraceMiddleware(wsgiapp, self.tracer, service='web') self.app = fixture.TestApp(app)
def test_middleware_exception(self): """Ensure exceptions raised in middleware are properly handled. Uncaught exceptions should result in error tagged spans. """ from .app.middleware import ExceptionMiddleware wsgiapp = ExceptionMiddleware(self._wsgiapp) app = PylonsTraceMiddleware(wsgiapp, self.tracer, service="web") app = fixture.TestApp(app) with pytest.raises(Exception): app.get(url_for(controller="root", action="index")) spans = self.pop_spans() assert spans, spans assert len(spans) == 1 span = spans[0] assert_is_measured(span) assert span.service == "web" assert span.resource == "None.None" assert span.error == 1 assert span.get_tag(http.URL) == "http://localhost:80/" assert_span_http_status_code(span, 500) assert span.get_tag(errors.ERROR_MSG) == "Middleware exception" assert span.get_tag(errors.ERROR_TYPE) == "exceptions.Exception" assert span.get_tag(errors.ERROR_STACK)
def test_pylons_string_code(): writer = DummyWriter() tracer = Tracer() tracer.writer = writer app = FakeWSGIApp() traced = PylonsTraceMiddleware(app, tracer, service="p") # successful request eq_(writer.pop(), []) app.code = '200 OK' app.body = ['woo'] app.environ = { 'REQUEST_METHOD': 'GET', 'pylons.routes_dict': { 'controller': 'foo', 'action': 'bar', } } try: out = traced(app.environ, app.start_response_string_code) except Exception as e: pass eq_(tracer.current_span(), None) spans = writer.pop() ok_(spans, spans) eq_(len(spans), 1) s = spans[0] eq_(s.error, 1) eq_(s.get_tag("error.msg"), "Custom exception") sc = int(s.get_tag("http.status_code")) eq_(sc, 512) ok_(s.get_tag("error.stack"))
def test_controller_exception(self): """Ensure exceptions thrown in controllers can be handled. No error tags should be set in the span. """ from .app.middleware import ExceptionToSuccessMiddleware wsgiapp = ExceptionToSuccessMiddleware(self._wsgiapp) app = PylonsTraceMiddleware(wsgiapp, self.tracer, service="web") app = fixture.TestApp(app) app.get(url_for(controller="root", action="raise_exception")) spans = self.pop_spans() assert spans, spans assert len(spans) == 1 span = spans[0] assert_is_measured(span) assert span.service == "web" assert span.resource == "root.raise_exception" assert span.error == 0 assert span.get_tag(http.URL) == "http://localhost:80/raise_exception" assert_span_http_status_code(span, 200) assert http.QUERY_STRING not in span.meta assert span.get_tag(errors.ERROR_MSG) is None assert span.get_tag(errors.ERROR_TYPE) is None assert span.get_tag(errors.ERROR_STACK) is None assert span.span_type == "web"
def test_pylons_exception_with_code_method(): writer = DummyWriter() tracer = Tracer() tracer.writer = writer app = FakeWSGIApp() traced = PylonsTraceMiddleware(app, tracer, service="p") app.code = '200 OK' app.body = ['woo'] app.environ = { 'REQUEST_METHOD': 'GET', 'pylons.routes_dict': { 'controller': 'foo', 'action': 'bar', } } try: out = traced(app.environ, app.start_response_exception_code_method) assert False except ExceptionWithCodeMethod: pass spans = writer.pop() ok_(spans, spans) eq_(len(spans), 1) s = spans[0] eq_(s.error, 1) eq_(s.get_tag('error.msg'), 'Exception with code method') eq_(int(s.get_tag('http.status_code')), 500)
def setUp(self): # initialize a real traced Pylons app self.tracer = get_dummy_tracer() wsgiapp = loadapp('config:test.ini', relative_to=PylonsTestCase.conf_dir) app = PylonsTraceMiddleware(wsgiapp, self.tracer, service='web') self.app = fixture.TestApp(app)
def test_exc_client_failure(self): from .app.middleware import ExceptionToClientErrorMiddleware wsgiapp = ExceptionToClientErrorMiddleware(self._wsgiapp) app = PylonsTraceMiddleware(wsgiapp, self.tracer, service='web') app = fixture.TestApp(app) app.get(url_for(controller='root', action='raise_exception'), status=404) spans = self.tracer.writer.pop() assert spans, spans assert len(spans) == 1 span = spans[0] assert span.service == 'web' assert span.resource == 'root.raise_exception' assert span.error == 0 assert span.get_tag('http.status_code') == '404' assert span.get_tag(errors.ERROR_MSG) is None assert span.get_tag(errors.ERROR_TYPE) is None assert span.get_tag(errors.ERROR_STACK) is None
def test_exc_success(self): from .app.middleware import ExceptionToSuccessMiddleware wsgiapp = ExceptionToSuccessMiddleware(self._wsgiapp) app = PylonsTraceMiddleware(wsgiapp, self.tracer, service='web') app = fixture.TestApp(app) app.get(url_for(controller='root', action='raise_exception')) spans = self.tracer.writer.pop() ok_(spans, spans) eq_(len(spans), 1) span = spans[0] eq_(span.service, 'web') eq_(span.resource, 'root.raise_exception') eq_(span.error, 0) eq_(span.get_tag('http.status_code'), '200') eq_(span.get_tag(errors.ERROR_MSG), None) eq_(span.get_tag(errors.ERROR_TYPE), None) eq_(span.get_tag(errors.ERROR_STACK), None)
def test_exc_success(self): from .app.middleware import ExceptionToSuccessMiddleware wsgiapp = ExceptionToSuccessMiddleware(self._wsgiapp) app = PylonsTraceMiddleware(wsgiapp, self.tracer, service='web') app = fixture.TestApp(app) app.get(url_for(controller='root', action='raise_exception')) spans = self.tracer.writer.pop() assert spans, spans assert len(spans) == 1 span = spans[0] assert span.service == 'web' assert span.resource == 'root.raise_exception' assert span.error == 0 assert span.get_tag(http.URL) == 'http://localhost:80/raise_exception' assert span.get_metric('http.status_code') == 200 assert span.get_tag(errors.ERROR_MSG) is None assert span.get_tag(errors.ERROR_TYPE) is None assert span.get_tag(errors.ERROR_STACK) is None
def test_pylons_exceptions(): # ensures the reported status code is 500 even if a wrong # status code is set and that the stacktrace points to the # right function writer = DummyWriter() tracer = Tracer() tracer.writer = writer app = FakeWSGIApp() traced = PylonsTraceMiddleware(app, tracer, service="p") # successful request eq_(writer.pop(), []) app.code = '200 OK' app.body = ['woo'] app.environ = { 'REQUEST_METHOD': 'GET', 'pylons.routes_dict': { 'controller': 'foo', 'action': 'bar', } } try: out = traced(app.environ, app.start_response_exception) except Exception as e: pass eq_(tracer.current_span(), None) spans = writer.pop() ok_(spans, spans) eq_(len(spans), 1) s = spans[0] eq_(s.error, 1) eq_(s.get_tag('error.msg'), 'Some exception') eq_(int(s.get_tag('http.status_code')), 500) ok_('start_response_exception' in s.get_tag('error.stack')) ok_('Exception: Some exception' in s.get_tag('error.stack'))
def test_exc_success(self): from .app.middleware import ExceptionToSuccessMiddleware wsgiapp = ExceptionToSuccessMiddleware(self._wsgiapp) app = PylonsTraceMiddleware(wsgiapp, self.tracer, service="web") app = fixture.TestApp(app) app.get(url_for(controller="root", action="raise_exception")) spans = self.pop_spans() assert spans, spans assert len(spans) == 1 span = spans[0] assert_is_measured(span) assert span.service == "web" assert span.resource == "root.raise_exception" assert span.error == 0 assert span.get_tag(http.URL) == "http://localhost:80/raise_exception" assert_span_http_status_code(span, 200) assert span.get_tag(errors.ERROR_MSG) is None assert span.get_tag(errors.ERROR_TYPE) is None assert span.get_tag(errors.ERROR_STACK) is None
def test_pylons(): writer = DummyWriter() tracer = Tracer() tracer.writer = writer app = FakeWSGIApp() traced = PylonsTraceMiddleware(app, tracer, service="p") # successful request eq_(writer.pop(), []) app.code = '200 OK' app.body = ['woo'] app.environ = { 'REQUEST_METHOD': 'GET', 'pylons.routes_dict': { 'controller': 'foo', 'action': 'bar', } } start = time.time() out = traced(app.environ, app.start_response) end = time.time() eq_(out, app.body) eq_(app.code, app.out_code) eq_(tracer.current_span(), None) spans = writer.pop() ok_(spans, spans) eq_(len(spans), 1) s = spans[0] eq_(s.service, "p") eq_(s.resource, "foo.bar") ok_(s.start >= start) ok_(s.duration <= end - start) eq_(s.error, 0) eq_(s.meta.get(http.STATUS_CODE), '200')