@@ -698,19 +698,21 @@ class TestHTTPAppWithCustomHeadersParameters(TestBase):
698
698
699
699
def setUp (self ):
700
700
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 (
704
703
http_capture_headers_server_request = ["a.*" , "b.*" ],
705
704
http_capture_headers_server_response = ["c.*" , "d.*" ],
706
705
http_capture_headers_sanitize_fields = [".*secret.*" ],
707
706
)
708
- self .client = TestClient ( self . app )
707
+ self .app = None
709
708
710
709
def tearDown (self ) -> None :
711
710
super ().tearDown ()
712
711
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 ()
714
716
715
717
@staticmethod
716
718
def _create_app ():
@@ -728,8 +730,11 @@ async def _():
728
730
729
731
return app
730
732
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 (
733
738
"/foobar" ,
734
739
headers = {
735
740
"apple" : "red" ,
@@ -755,8 +760,60 @@ def test_http_custom_request_headers_in_span_attributes(self):
755
760
self .assertSpanHasAttributes (server_span , expected )
756
761
self .assertNotIn ("http.request.header.fig" , server_span .attributes )
757
762
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" )
760
817
self .assertEqual (200 , resp .status_code )
761
818
span_list = self .memory_exporter .get_finished_spans ()
762
819
self .assertEqual (len (span_list ), 3 )
0 commit comments