Skip to content

Commit 44ab8ff

Browse files
authored
Merge pull request #488 from instana/fastapi-header-capture
FastAPI: capture responseHeadersOnEntrySpans
2 parents 884edc3 + c04d279 commit 44ab8ff

File tree

4 files changed

+521
-361
lines changed

4 files changed

+521
-361
lines changed

instana/instrumentation/asgi.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ def __init__(self, app):
2020
self.app = app
2121

2222
def _extract_custom_headers(self, span, headers):
23+
if agent.options.extra_http_headers is None:
24+
return
2325
try:
2426
for custom_header in agent.options.extra_http_headers:
2527
# Headers are in the following format: b'x-header-1'
@@ -84,6 +86,7 @@ async def send_wrapper(response):
8486

8587
headers = response.get('headers')
8688
if headers is not None:
89+
self._extract_custom_headers(span, headers)
8790
async_tracer.inject(span.context, opentracing.Format.BINARY, headers)
8891
except Exception:
8992
logger.debug("send_wrapper: ", exc_info=True)

tests/apps/fastapi_app/__init__.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,24 @@
66
from instana.log import logger
77

88
testenv["fastapi_port"] = 10816
9-
testenv["fastapi_server"] = ("http://127.0.0.1:" + str(testenv["fastapi_port"]))
9+
testenv["fastapi_server"] = "http://127.0.0.1:" + str(testenv["fastapi_port"])
10+
1011

1112
def launch_fastapi():
1213
from .app import fastapi_server
1314
from instana.singletons import agent
1415

1516
# Hack together a manual custom headers list; We'll use this in tests
16-
agent.options.extra_http_headers = [u'X-Capture-This', u'X-Capture-That']
17+
agent.options.extra_http_headers = [
18+
"X-Capture-This",
19+
"X-Capture-That",
20+
"X-Capture-This-Too",
21+
"X-Capture-That-Too",
22+
]
1723

18-
uvicorn.run(fastapi_server, host='127.0.0.1', port=testenv['fastapi_port'], log_level="critical")
24+
uvicorn.run(
25+
fastapi_server,
26+
host="127.0.0.1",
27+
port=testenv["fastapi_port"],
28+
log_level="critical",
29+
)

tests/apps/fastapi_app/app.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# (c) Copyright IBM Corp. 2021
22
# (c) Copyright Instana Inc. 2020
33

4-
from fastapi import FastAPI, HTTPException
4+
from fastapi import FastAPI, HTTPException, Response
55
from fastapi.exceptions import RequestValidationError
66
from fastapi.responses import PlainTextResponse
77
from starlette.exceptions import HTTPException as StarletteHTTPException
@@ -24,6 +24,14 @@ async def root():
2424
async def user(user_id):
2525
return {"user": user_id}
2626

27+
@fastapi_server.get("/response_headers")
28+
async def response_headers():
29+
headers = {
30+
'X-Capture-This-Too': 'this too',
31+
'X-Capture-That-Too': 'that too'
32+
}
33+
return Response("Stan wuz here with headers!", headers=headers)
34+
2735
@fastapi_server.get("/400")
2836
async def four_zero_zero():
2937
raise HTTPException(status_code=400, detail="400 response")

0 commit comments

Comments
 (0)