27
27
_HTTPStabilityMode ,
28
28
_OpenTelemetrySemanticConventionStability ,
29
29
)
30
- from opentelemetry .instrumentation .urllib3 import URLLib3Instrumentor
30
+ from opentelemetry .instrumentation .urllib3 import (
31
+ RequestInfo ,
32
+ URLLib3Instrumentor ,
33
+ )
31
34
from opentelemetry .instrumentation .utils import (
32
35
suppress_http_instrumentation ,
33
36
suppress_instrumentation ,
42
45
from opentelemetry .semconv .trace import SpanAttributes
43
46
from opentelemetry .test .mock_textmap import MockTextMapPropagator
44
47
from opentelemetry .test .test_base import TestBase
48
+ from opentelemetry .trace import Span
45
49
from opentelemetry .util .http import get_excluded_urls
46
50
47
51
# pylint: disable=too-many-public-methods
@@ -521,10 +525,10 @@ def test_credential_removal(self):
521
525
self .assert_success_span (response , self .HTTP_URL )
522
526
523
527
def test_hooks (self ):
524
- def request_hook (span , request , body , headers ):
528
+ def request_hook (span , pool , request_info ):
525
529
span .update_name ("name set from hook" )
526
530
527
- def response_hook (span , request , response ):
531
+ def response_hook (span , pool , response ):
528
532
span .set_attribute ("response_hook_attr" , "value" )
529
533
530
534
URLLib3Instrumentor ().uninstrument ()
@@ -541,11 +545,17 @@ def response_hook(span, request, response):
541
545
self .assertEqual (span .attributes ["response_hook_attr" ], "value" )
542
546
543
547
def test_request_hook_params (self ):
544
- def request_hook (span , request , headers , body ):
548
+ def request_hook (
549
+ span : Span ,
550
+ _pool : urllib3 .connectionpool .ConnectionPool ,
551
+ request_info : RequestInfo ,
552
+ ) -> None :
553
+ span .set_attribute ("request_hook_method" , request_info .method )
554
+ span .set_attribute ("request_hook_url" , request_info .url )
545
555
span .set_attribute (
546
- "request_hook_headers" , json .dumps (dict (headers ))
556
+ "request_hook_headers" , json .dumps (dict (request_info . headers ))
547
557
)
548
- span .set_attribute ("request_hook_body" , body )
558
+ span .set_attribute ("request_hook_body" , request_info . body )
549
559
550
560
URLLib3Instrumentor ().uninstrument ()
551
561
URLLib3Instrumentor ().instrument (
@@ -564,6 +574,10 @@ def request_hook(span, request, headers, body):
564
574
565
575
span = self .assert_span ()
566
576
577
+ self .assertEqual (span .attributes ["request_hook_method" ], "POST" )
578
+ self .assertEqual (
579
+ span .attributes ["request_hook_url" ], "http://mock/status/200"
580
+ )
567
581
self .assertIn ("request_hook_headers" , span .attributes )
568
582
self .assertEqual (
569
583
span .attributes ["request_hook_headers" ], json .dumps (headers )
@@ -572,8 +586,12 @@ def request_hook(span, request, headers, body):
572
586
self .assertEqual (span .attributes ["request_hook_body" ], body )
573
587
574
588
def test_request_positional_body (self ):
575
- def request_hook (span , request , headers , body ):
576
- span .set_attribute ("request_hook_body" , body )
589
+ def request_hook (
590
+ span : Span ,
591
+ _pool : urllib3 .connectionpool .ConnectionPool ,
592
+ request_info : RequestInfo ,
593
+ ) -> None :
594
+ span .set_attribute ("request_hook_body" , request_info .body )
577
595
578
596
URLLib3Instrumentor ().uninstrument ()
579
597
URLLib3Instrumentor ().instrument (
0 commit comments