Skip to content

Commit

Permalink
- fix(request_header_capture)
Browse files Browse the repository at this point in the history
- test(request_header_capture)

Signed-off-by: Varsha GS <[email protected]>
  • Loading branch information
GSVarsha committed Jan 11, 2024
1 parent 0729ace commit 715edda
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 7 deletions.
12 changes: 5 additions & 7 deletions instana/instrumentation/pyramid/tweens.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ class InstanaTweenFactory(object):
def __init__(self, handler, registry):
self.handler = handler

def _extract_custom_headers(self, span, headers, format):
def _extract_custom_headers(self, span, headers):
if agent.options.extra_http_headers is None:
return
try:
for custom_header in agent.options.extra_http_headers:
# Headers are available in this format: HTTP_X_CAPTURE_THIS
pyramid_header = ('HTTP_' + custom_header.upper()).replace('-', '_') if format else custom_header
if pyramid_header in headers:
span.set_tag("http.header.%s" % custom_header, headers[pyramid_header])
if custom_header in headers:
span.set_tag("http.header.%s" % custom_header, headers[custom_header])

except Exception:
logger.debug("extract_custom_headers: ", exc_info=True)
Expand All @@ -44,7 +42,7 @@ def __call__(self, request):
if request.matched_route is not None:
scope.span.set_tag("http.path_tpl", request.matched_route.pattern)

self._extract_custom_headers(scope.span, request.headers, format=True)
self._extract_custom_headers(scope.span, request.headers)

if len(request.query_string):
scrubbed_params = strip_secrets_from_query(request.query_string, agent.options.secrets_matcher,
Expand All @@ -55,7 +53,7 @@ def __call__(self, request):
try:
response = self.handler(request)

self._extract_custom_headers(scope.span, response.headers, format=False)
self._extract_custom_headers(scope.span, response.headers)

tracer.inject(scope.span.context, ot.Format.HTTP_HEADERS, response.headers)
response.headers['Server-Timing'] = "intid;desc=%s" % scope.span.context.trace_id
Expand Down
75 changes: 75 additions & 0 deletions tests/frameworks/test_pyramid.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,78 @@ def test_response_header_capture(self):
self.assertEqual("Ok too", sdk_custom_tags["http.header.X-Capture-That"])

agent.options.extra_http_headers = original_extra_http_headers

def test_request_header_capture(self):
original_extra_http_headers = agent.options.extra_http_headers
agent.options.extra_http_headers = ["X-Capture-This-Too", "X-Capture-That-Too"]

request_headers = {
"X-Capture-This-Too": "this too",
"X-Capture-That-Too": "that too",
}

with tracer.start_active_span("test"):
response = self.http.request(
"GET", testenv["pyramid_server"] + "/", headers=request_headers
)

spans = self.recorder.queued_spans()
self.assertEqual(3, len(spans))

pyramid_span = spans[0]
urllib3_span = spans[1]
test_span = spans[2]

self.assertTrue(response)
self.assertEqual(200, response.status)

# Same traceId
self.assertEqual(test_span.t, urllib3_span.t)
self.assertEqual(urllib3_span.t, pyramid_span.t)

# Parent relationships
self.assertEqual(urllib3_span.p, test_span.s)
self.assertEqual(pyramid_span.p, urllib3_span.s)

# Synthetic
self.assertIsNone(pyramid_span.sy)
self.assertIsNone(urllib3_span.sy)
self.assertIsNone(test_span.sy)

# Error logging
self.assertIsNone(test_span.ec)
self.assertIsNone(urllib3_span.ec)
self.assertIsNone(pyramid_span.ec)

# HTTP SDK span
self.assertEqual("sdk", pyramid_span.n)

self.assertTrue(pyramid_span.data["sdk"])
self.assertEqual('http', pyramid_span.data["sdk"]["name"])
self.assertEqual('entry', pyramid_span.data["sdk"]["type"])

sdk_custom_tags = pyramid_span.data["sdk"]["custom"]["tags"]
self.assertEqual('127.0.0.1:' + str(testenv['pyramid_port']), sdk_custom_tags["http.host"])
self.assertEqual('/', sdk_custom_tags["http.url"])
self.assertEqual('GET', sdk_custom_tags["http.method"])
self.assertEqual(200, sdk_custom_tags["http.status"])
self.assertNotIn("message", sdk_custom_tags)
self.assertNotIn("http.path_tpl", sdk_custom_tags)

# urllib3
self.assertEqual("test", test_span.data["sdk"]["name"])
self.assertEqual("urllib3", urllib3_span.n)
self.assertEqual(200, urllib3_span.data["http"]["status"])
self.assertEqual(testenv["pyramid_server"] + '/', urllib3_span.data["http"]["url"])
self.assertEqual("GET", urllib3_span.data["http"]["method"])
self.assertIsNotNone(urllib3_span.stack)
self.assertTrue(type(urllib3_span.stack) is list)
self.assertTrue(len(urllib3_span.stack) > 1)

# custom headers
self.assertTrue(sdk_custom_tags["http.header.X-Capture-This-Too"])
self.assertEqual("this too", sdk_custom_tags["http.header.X-Capture-This-Too"])
self.assertTrue(sdk_custom_tags["http.header.X-Capture-That-Too"])
self.assertEqual("that too", sdk_custom_tags["http.header.X-Capture-That-Too"])

agent.options.extra_http_headers = original_extra_http_headers

0 comments on commit 715edda

Please sign in to comment.