def __call__(self, environ, start_response): env = environ def new_start_response(status, headers, exc_info=None): """Modified start response with additional headers.""" internal_tracer.inject(span.context, ot.Format.HTTP_HEADERS, headers) res = start_response(status, headers, exc_info) sc = status.split(' ')[0] if 500 <= int(sc) <= 511: span.set_tag("error", True) ec = span.tags.get('ec', 0) span.set_tag("ec", ec + 1) span.set_tag(tags.HTTP_STATUS_CODE, sc) span.finish() return res if 'HTTP_X_INSTANA_T' in env and 'HTTP_X_INSTANA_S' in env: ctx = internal_tracer.extract(ot.Format.HTTP_HEADERS, env) span = internal_tracer.start_span("wsgi", child_of=ctx) else: span = internal_tracer.start_span("wsgi") span.set_tag(tags.HTTP_URL, env['PATH_INFO']) span.set_tag("http.params", env['QUERY_STRING']) span.set_tag(tags.HTTP_METHOD, env['REQUEST_METHOD']) span.set_tag("http.host", env['HTTP_HOST']) return self.app(environ, new_start_response)
def test_basic_request(self): span = tracer.start_span("test") response = self.client.service.ask_question(u'Why u like dat?', 5) span.finish() spans = self.recorder.queued_spans() assert_equals(3, len(spans)) wsgi_span = spans[0] soap_span = spans[1] test_span = spans[2] assert_equals(1, len(response)) assert_equals(1, len(response[0])) assert (type(response[0]) is list) assert_equals("test", test_span.data.sdk.name) assert_equals(test_span.t, soap_span.t) assert_equals(soap_span.p, test_span.s) assert_equals(wsgi_span.t, soap_span.t) assert_equals(wsgi_span.p, soap_span.s) assert_equals(None, soap_span.error) assert_equals(None, soap_span.ec) assert_equals('ask_question', soap_span.data.soap.action) assert_equals('http://localhost:4132/', soap_span.data.http.url) assert_equals(None, tracer.current_context())
def test_get_request(self): span = tracer.start_span("test") r = self.http.request('GET', 'http://127.0.0.1:5000/') span.finish() spans = self.recorder.queued_spans() assert_equals(2, len(spans)) first_span = spans[1] second_span = spans[0] assert(r) assert_equals(200, r.status) assert_equals("test", first_span.data.sdk.name) assert_equals("urllib3", second_span.n) assert_equals(200, second_span.data.http.status) assert_equals("http://127.0.0.1:5000/", second_span.data.http.url) assert_equals("GET", second_span.data.http.method) assert_equals(None, second_span.error) assert_equals(None, second_span.ec) assert_equals(second_span.t, first_span.t) assert_equals(second_span.p, first_span.s) assert_equals(None, tracer.current_context())
def test_client_error(self): span = tracer.start_span("test") r = None try: r = self.http.request('GET', 'http://doesnotexist.asdf:5000/504', retries=False, timeout=urllib3.Timeout(connect=0.5, read=0.5)) except Exception: pass span.finish() spans = self.recorder.queued_spans() assert_equals(2, len(spans)) first_span = spans[1] second_span = spans[0] assert_equals(None, r) assert_equals("test", first_span.data.sdk.name) assert_equals("urllib3", second_span.n) assert_equals(None, second_span.data.http.status) assert_equals("http://doesnotexist.asdf:5000/504", second_span.data.http.url) assert_equals("GET", second_span.data.http.method) assert_equals(True, second_span.error) assert_equals(1, second_span.ec) assert_equals(second_span.t, first_span.t) assert_equals(second_span.p, first_span.s)
def test_exception_logging(self): span = tracer.start_span("test") try: r = self.http.request('GET', 'http://127.0.0.1:5000/exception') except Exception: pass span.finish() spans = self.recorder.queued_spans() assert_equals(2, len(spans)) first_span = spans[1] second_span = spans[0] assert(r) assert_equals(500, r.status) assert_equals("test", first_span.data.sdk.name) assert_equals("urllib3", second_span.n) assert_equals(500, second_span.data.http.status) assert_equals("http://127.0.0.1:5000/exception", second_span.data.http.url) assert_equals("GET", second_span.data.http.method) assert_equals(True, second_span.error) assert_equals(1, second_span.ec) assert_equals(second_span.t, first_span.t) assert_equals(second_span.p, first_span.s)
def process_request(self, request): try: env = request.environ if 'HTTP_X_INSTANA_T' in env and 'HTTP_X_INSTANA_S' in env: ctx = internal_tracer.extract(ot.Format.HTTP_HEADERS, env) span = internal_tracer.start_span("django", child_of=ctx) else: span = internal_tracer.start_span("django") span.set_tag(ext.HTTP_URL, env['PATH_INFO']) span.set_tag("http.params", env['QUERY_STRING']) span.set_tag(ext.HTTP_METHOD, request.method) span.set_tag("http.host", env['HTTP_HOST']) self.span = span except Exception as e: logger.debug("Instana middleware @ process_response: ", e)
def test_requestspkg_put(self): span = tracer.start_span("test") r = requests.put('http://127.0.0.1:5000/notfound') span.finish() spans = self.recorder.queued_spans() assert_equals(2, len(spans)) first_span = spans[1] second_span = spans[0] assert_equals(404, r.status_code) assert_equals("test", first_span.data.sdk.name) assert_equals("urllib3", second_span.n) assert_equals(404, second_span.data.http.status) assert_equals("http://127.0.0.1:5000/notfound", second_span.data.http.url) assert_equals("PUT", second_span.data.http.method) assert_equals(None, second_span.error) assert_equals(None, second_span.ec) assert_equals(second_span.t, first_span.t) assert_equals(second_span.p, first_span.s)
def test_301_redirect(self): span = tracer.start_span("test") r = self.http.request('GET', 'http://127.0.0.1:5000/301') span.finish() spans = self.recorder.queued_spans() assert_equals(3, len(spans)) first_span = spans[2] second_span = spans[1] third_span = spans[0] assert(r) assert_equals(second_span.t, first_span.t) assert_equals(second_span.p, first_span.s) assert_equals(third_span.t, first_span.t) assert_equals(third_span.p, first_span.s) assert(first_span.d) assert(second_span.d) assert(third_span.d)
def test_requestspkg_get(self): span = tracer.start_span("test") r = requests.get('http://127.0.0.1:5000/', timeout=2) span.finish() spans = self.recorder.queued_spans() assert_equals(2, len(spans)) first_span = spans[1] second_span = spans[0] assert(r) assert_equals(200, r.status_code) assert_equals("test", first_span.data.sdk.name) assert_equals("urllib3", second_span.n) assert_equals(200, second_span.data.http.status) assert_equals("http://127.0.0.1:5000/", second_span.data.http.url) assert_equals("GET", second_span.data.http.method) assert_equals(None, second_span.error) assert_equals(None, second_span.ec) assert_equals(second_span.t, first_span.t) assert_equals(second_span.p, first_span.s)
def test_5xx_request(self): span = tracer.start_span("test") r = self.http.request('GET', 'http://127.0.0.1:5000/504') span.finish() spans = self.recorder.queued_spans() assert_equals(2, len(spans)) first_span = spans[1] second_span = spans[0] assert(r) assert_equals(504, r.status) assert_equals("test", first_span.data.sdk.name) assert_equals("urllib3", second_span.n) assert_equals(504, second_span.data.http.status) assert_equals("http://127.0.0.1:5000/504", second_span.data.http.url) assert_equals("GET", second_span.data.http.method) assert_equals(True, second_span.error) assert_equals(1, second_span.ec) assert_equals(second_span.t, first_span.t) assert_equals(second_span.p, first_span.s)
def test_server_fault(self): response = None try: span = tracer.start_span("test") response = self.client.service.server_fault() except: pass finally: span.finish() spans = self.recorder.queued_spans() assert_equals(3, len(spans)) wsgi_span = spans[0] soap_span = spans[1] test_span = spans[2] assert_equals(None, response) assert_equals("test", test_span.data.sdk.name) assert_equals(test_span.t, soap_span.t) assert_equals(soap_span.p, test_span.s) assert_equals(wsgi_span.t, soap_span.t) assert_equals(wsgi_span.p, soap_span.s) assert_equals(True, soap_span.error) assert_equals(1, soap_span.ec) assert ('logs' in soap_span.data.custom.__dict__) assert_equals(1, len(soap_span.data.custom.logs.keys())) tskey = list(soap_span.data.custom.logs.keys())[0] assert ('message' in soap_span.data.custom.logs[tskey]) assert_equals(u"Server raised fault: 'Server side fault example.'", soap_span.data.custom.logs[tskey]['message']) assert_equals('server_fault', soap_span.data.soap.action) assert_equals('http://localhost:4132/', soap_span.data.http.url) assert_equals(None, tracer.current_context())