Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_client_response_header_capture(self):
original_extra_headers = agent.extra_headers
agent.extra_headers = ['X-Capture-This']
async def test():
with async_tracer.start_active_span('test'):
async with aiohttp.ClientSession() as session:
return await self.fetch(session, testenv["wsgi_server"] + "/response_headers")
response = self.loop.run_until_complete(test())
spans = self.recorder.queued_spans()
self.assertEqual(3, len(spans))
wsgi_span = spans[0]
aiohttp_span = spans[1]
test_span = spans[2]
def test_client_response_header_capture(self):
original_extra_headers = agent.extra_headers
agent.extra_headers = ['X-Capture-This']
async def test():
with async_tracer.start_active_span('test'):
async with aiohttp.ClientSession() as session:
return await self.fetch(session, testenv["wsgi_server"] + "/response_headers")
response = self.loop.run_until_complete(test())
spans = self.recorder.queued_spans()
self.assertEqual(3, len(spans))
wsgi_span = spans[0]
aiohttp_span = spans[1]
test_span = spans[2]
self.assertIsNone(async_tracer.active_span)
def test_response_header_capture(self):
original_extra_headers = agent.extra_headers
agent.extra_headers = ['X-Capture-This']
with tracer.start_active_span('test'):
r = self.http.request('GET', testenv["wsgi_server"] + '/response_headers')
spans = self.recorder.queued_spans()
self.assertEqual(3, len(spans))
wsgi_span = spans[0]
urllib3_span = spans[1]
test_span = spans[2]
assert(r)
self.assertEqual(200, r.status)
self.assertIsNone(tracer.active_span)
# Same traceId
res = start_response(status, headers, exc_info)
sc = status.split(' ')[0]
if 500 <= int(sc) <= 511:
scope.span.mark_as_errored()
scope.span.set_tag(tags.HTTP_STATUS_CODE, sc)
scope.close()
return res
else:
return start_response(status, headers, exc_info)
ctx = tracer.extract(ot.Format.HTTP_HEADERS, env)
scope = env['stan_scope'] = tracer.start_active_span("wsgi", child_of=ctx)
if hasattr(agent, 'extra_headers') and agent.extra_headers is not None:
for custom_header in agent.extra_headers:
# Headers are available in this format: HTTP_X_CAPTURE_THIS
wsgi_header = ('HTTP_' + custom_header.upper()).replace('-', '_')
if wsgi_header in env:
scope.span.set_tag("http.%s" % custom_header, env[wsgi_header])
if 'PATH_INFO' in env:
scope.span.set_tag('http.path', env['PATH_INFO'])
if 'QUERY_STRING' in env and len(env['QUERY_STRING']):
scrubbed_params = strip_secrets(env['QUERY_STRING'], agent.secrets_matcher, agent.secrets_list)
scope.span.set_tag("http.params", scrubbed_params)
if 'REQUEST_METHOD' in env:
scope.span.set_tag(tags.HTTP_METHOD, env['REQUEST_METHOD'])
if 'HTTP_HOST' in env:
scope.span.set_tag("http.host", env['HTTP_HOST'])
ctx = tornado_tracer.extract(opentracing.Format.HTTP_HEADERS, instance.request.headers)
scope = tornado_tracer.start_active_span('tornado-server', child_of=ctx)
# Query param scrubbing
if instance.request.query is not None and len(instance.request.query) > 0:
cleaned_qp = strip_secrets(instance.request.query, agent.secrets_matcher, agent.secrets_list)
scope.span.set_tag("http.params", cleaned_qp)
url = "%s://%s%s" % (instance.request.protocol, instance.request.host, instance.request.path)
scope.span.set_tag("http.url", url)
scope.span.set_tag("http.method", instance.request.method)
scope.span.set_tag("handler", instance.__class__.__name__)
# Custom header tracking support
if hasattr(agent, 'extra_headers') and agent.extra_headers is not None:
for custom_header in agent.extra_headers:
if custom_header in instance.request.headers:
scope.span.set_tag("http.%s" % custom_header, instance.request.headers[custom_header])
setattr(instance.request, "_instana", scope)
# Set the context response headers now because tornado doesn't give us a better option to do so
# later for this request.
tornado_tracer.inject(scope.span.context, opentracing.Format.HTTP_HEADERS, instance._headers)
instance.set_header(name='Server-Timing', value="intid;desc=%s" % scope.span.context.trace_id)
return wrapped(*argv, **kwargs)
except Exception:
logger.debug("tornado execute", exc_info=True)
async def stan_request_end(session, trace_config_ctx, params):
try:
scope = trace_config_ctx.scope
if scope is not None:
scope.span.set_tag('http.status_code', params.response.status)
if hasattr(agent, 'extra_headers') and agent.extra_headers is not None:
for custom_header in agent.extra_headers:
if custom_header in params.response.headers:
scope.span.set_tag("http.%s" % custom_header, params.response.headers[custom_header])
if 500 <= params.response.status <= 599:
scope.span.mark_as_errored({"http.error": params.response.reason})
scope.close()
except Exception:
logger.debug("stan_request_end", exc_info=True)
def collect_response(scope, response):
try:
scope.span.set_tag(ext.HTTP_STATUS_CODE, response.status)
if hasattr(agent, 'extra_headers') and agent.extra_headers is not None:
for custom_header in agent.extra_headers:
if custom_header in response.headers:
scope.span.set_tag("http.%s" % custom_header, response.headers[custom_header])
if 500 <= response.status <= 599:
scope.span.mark_as_errored()
except Exception:
logger.debug("collect_response", exc_info=True)
scope = tornado_tracer.start_active_span('tornado-server', child_of=ctx)
# Query param scrubbing
if instance.request.query is not None and len(instance.request.query) > 0:
cleaned_qp = strip_secrets(instance.request.query, agent.secrets_matcher, agent.secrets_list)
scope.span.set_tag("http.params", cleaned_qp)
url = "%s://%s%s" % (instance.request.protocol, instance.request.host, instance.request.path)
scope.span.set_tag("http.url", url)
scope.span.set_tag("http.method", instance.request.method)
scope.span.set_tag("handler", instance.__class__.__name__)
# Custom header tracking support
if hasattr(agent, 'extra_headers') and agent.extra_headers is not None:
for custom_header in agent.extra_headers:
if custom_header in instance.request.headers:
scope.span.set_tag("http.%s" % custom_header, instance.request.headers[custom_header])
setattr(instance.request, "_instana", scope)
# Set the context response headers now because tornado doesn't give us a better option to do so
# later for this request.
tornado_tracer.inject(scope.span.context, opentracing.Format.HTTP_HEADERS, instance._headers)
instance.set_header(name='Server-Timing', value="intid;desc=%s" % scope.span.context.trace_id)
return wrapped(*argv, **kwargs)
except Exception:
logger.debug("tornado execute", exc_info=True)
def before_request_with_instana(*argv, **kwargs):
try:
env = flask.request.environ
ctx = None
if 'HTTP_X_INSTANA_T' in env and 'HTTP_X_INSTANA_S' in env:
ctx = tracer.extract(opentracing.Format.HTTP_HEADERS, env)
flask.g.scope = tracer.start_active_span('wsgi', child_of=ctx)
span = flask.g.scope.span
if hasattr(agent, 'extra_headers') and agent.extra_headers is not None:
for custom_header in agent.extra_headers:
# Headers are available in this format: HTTP_X_CAPTURE_THIS
header = ('HTTP_' + custom_header.upper()).replace('-', '_')
if header in env:
span.set_tag("http.%s" % custom_header, env[header])
span.set_tag(ext.HTTP_METHOD, flask.request.method)
if 'PATH_INFO' in env:
span.set_tag(ext.HTTP_URL, env['PATH_INFO'])
if 'QUERY_STRING' in env and len(env['QUERY_STRING']):
scrubbed_params = strip_secrets(env['QUERY_STRING'], agent.secrets_matcher, agent.secrets_list)
span.set_tag("http.params", scrubbed_params)
if 'HTTP_HOST' in env:
span.set_tag("http.host", env['HTTP_HOST'])
if hasattr(flask.request.url_rule, 'rule') and \
path_tpl_re.search(flask.request.url_rule.rule) is not None: