Skip to content

Commit 52e0851

Browse files
samuelcolvinocelotl
authored andcommitted
same logic for instrument()
1 parent 2abcb45 commit 52e0851

File tree

2 files changed

+79
-10
lines changed

2 files changed

+79
-10
lines changed

instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def client_response_hook(span: Span, message: dict):
7676
if span and span.is_recording():
7777
span.set_attribute("custom_user_attribute_from_response_hook", "some-value")
7878
79-
FastAPIInstrumentor().instrument_app(server_request_hook=server_request_hook, client_request_hook=client_request_hook, client_response_hook=client_response_hook)
79+
FastAPIInstrumentor().instrument(server_request_hook=server_request_hook, client_request_hook=client_request_hook, client_response_hook=client_response_hook)
8080
8181
Capture HTTP request and response headers
8282
*****************************************
@@ -285,6 +285,15 @@ def _instrument(self, **kwargs):
285285
_InstrumentedFastAPI._client_response_hook = kwargs.get(
286286
"client_response_hook"
287287
)
288+
_InstrumentedFastAPI._http_capture_headers_server_request = kwargs.get(
289+
"http_capture_headers_server_request"
290+
)
291+
_InstrumentedFastAPI._http_capture_headers_server_response = (
292+
kwargs.get("http_capture_headers_server_response")
293+
)
294+
_InstrumentedFastAPI._http_capture_headers_sanitize_fields = (
295+
kwargs.get("http_capture_headers_sanitize_fields")
296+
)
288297
_excluded_urls = kwargs.get("excluded_urls")
289298
_InstrumentedFastAPI._excluded_urls = (
290299
_excluded_urls_from_env
@@ -327,6 +336,9 @@ def __init__(self, *args, **kwargs):
327336
client_response_hook=_InstrumentedFastAPI._client_response_hook,
328337
tracer_provider=_InstrumentedFastAPI._tracer_provider,
329338
meter=meter,
339+
http_capture_headers_server_request=_InstrumentedFastAPI._http_capture_headers_server_request,
340+
http_capture_headers_server_response=_InstrumentedFastAPI._http_capture_headers_server_response,
341+
http_capture_headers_sanitize_fields=_InstrumentedFastAPI._http_capture_headers_sanitize_fields,
330342
)
331343
self._is_instrumented_by_opentelemetry = True
332344
_InstrumentedFastAPI._instrumented_fastapi_apps.add(self)

instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py

+66-9
Original file line numberDiff line numberDiff line change
@@ -698,19 +698,21 @@ class TestHTTPAppWithCustomHeadersParameters(TestBase):
698698

699699
def setUp(self):
700700
super().setUp()
701-
self.app = self._create_app()
702-
otel_fastapi.FastAPIInstrumentor().instrument_app(
703-
self.app,
701+
self.instrumentor = otel_fastapi.FastAPIInstrumentor()
702+
self.kwargs = dict(
704703
http_capture_headers_server_request=["a.*", "b.*"],
705704
http_capture_headers_server_response=["c.*", "d.*"],
706705
http_capture_headers_sanitize_fields=[".*secret.*"],
707706
)
708-
self.client = TestClient(self.app)
707+
self.app = None
709708

710709
def tearDown(self) -> None:
711710
super().tearDown()
712711
with self.disable_logging():
713-
otel_fastapi.FastAPIInstrumentor().uninstrument_app(self.app)
712+
if self.app:
713+
self.instrumentor.uninstrument_app(self.app)
714+
else:
715+
self.instrumentor.uninstrument()
714716

715717
@staticmethod
716718
def _create_app():
@@ -728,8 +730,11 @@ async def _():
728730

729731
return app
730732

731-
def test_http_custom_request_headers_in_span_attributes(self):
732-
resp = self.client.get(
733+
def test_http_custom_request_headers_in_span_attributes_app(self):
734+
self.app = self._create_app()
735+
self.instrumentor.instrument_app(self.app, **self.kwargs)
736+
737+
resp = TestClient(self.app).get(
733738
"/foobar",
734739
headers={
735740
"apple": "red",
@@ -755,8 +760,60 @@ def test_http_custom_request_headers_in_span_attributes(self):
755760
self.assertSpanHasAttributes(server_span, expected)
756761
self.assertNotIn("http.request.header.fig", server_span.attributes)
757762

758-
def test_http_custom_response_headers_in_span_attributes(self):
759-
resp = self.client.get("/foobar")
763+
def test_http_custom_request_headers_in_span_attributes_instr(self):
764+
"""As above, but use instrument(), not instrument_app()."""
765+
self.instrumentor.instrument(**self.kwargs)
766+
767+
resp = TestClient(self._create_app()).get(
768+
"/foobar",
769+
headers={
770+
"apple": "red",
771+
"banana-secret": "yellow",
772+
"fig": "green",
773+
},
774+
)
775+
self.assertEqual(200, resp.status_code)
776+
span_list = self.memory_exporter.get_finished_spans()
777+
self.assertEqual(len(span_list), 3)
778+
779+
server_span = [
780+
span for span in span_list if span.kind == trace.SpanKind.SERVER
781+
][0]
782+
783+
expected = {
784+
# apple should be included because it starts with a
785+
"http.request.header.apple": ("red",),
786+
# same with banana because it starts with b,
787+
# redacted because it contains "secret"
788+
"http.request.header.banana_secret": ("[REDACTED]",),
789+
}
790+
self.assertSpanHasAttributes(server_span, expected)
791+
self.assertNotIn("http.request.header.fig", server_span.attributes)
792+
793+
def test_http_custom_response_headers_in_span_attributes_app(self):
794+
self.app = self._create_app()
795+
self.instrumentor.instrument_app(self.app, **self.kwargs)
796+
resp = TestClient(self.app).get("/foobar")
797+
self.assertEqual(200, resp.status_code)
798+
span_list = self.memory_exporter.get_finished_spans()
799+
self.assertEqual(len(span_list), 3)
800+
801+
server_span = [
802+
span for span in span_list if span.kind == trace.SpanKind.SERVER
803+
][0]
804+
805+
expected = {
806+
"http.response.header.carrot": ("bar",),
807+
"http.response.header.date_secret": ("[REDACTED]",),
808+
}
809+
self.assertSpanHasAttributes(server_span, expected)
810+
self.assertNotIn("http.response.header.egg", server_span.attributes)
811+
812+
def test_http_custom_response_headers_in_span_attributes_inst(self):
813+
"""As above, but use instrument(), not instrument_app()."""
814+
self.instrumentor.instrument(**self.kwargs)
815+
816+
resp = TestClient(self._create_app()).get("/foobar")
760817
self.assertEqual(200, resp.status_code)
761818
span_list = self.memory_exporter.get_finished_spans()
762819
self.assertEqual(len(span_list), 3)

0 commit comments

Comments
 (0)