5
5
Instana ASGI Middleware
6
6
"""
7
7
8
- from typing import TYPE_CHECKING , Any , Awaitable , Callable , Dict , List , Tuple
8
+ from typing import TYPE_CHECKING , Any , Awaitable , Callable , Dict
9
9
10
10
from opentelemetry .semconv .trace import SpanAttributes
11
11
from opentelemetry .trace import SpanKind
14
14
from instana .propagators .format import Format
15
15
from instana .singletons import agent , tracer
16
16
from instana .util .secrets import strip_secrets_from_query
17
+ from instana .util .traceutils import extract_custom_headers
17
18
18
19
if TYPE_CHECKING :
19
20
from starlette .middleware .exceptions import ExceptionMiddleware
@@ -28,23 +29,6 @@ class InstanaASGIMiddleware:
28
29
def __init__ (self , app : "ExceptionMiddleware" ) -> None :
29
30
self .app = app
30
31
31
- def _extract_custom_headers (
32
- self , span : "InstanaSpan" , headers : List [Tuple [object , ...]]
33
- ) -> None :
34
- if agent .options .extra_http_headers is None :
35
- return
36
- try :
37
- for custom_header in agent .options .extra_http_headers :
38
- # Headers are in the following format: b'x-header-1'
39
- for header_pair in headers :
40
- if header_pair [0 ].decode ("utf-8" ).lower () == custom_header .lower ():
41
- span .set_attribute (
42
- f"http.header.{ custom_header } " ,
43
- header_pair [1 ].decode ("utf-8" ),
44
- )
45
- except Exception :
46
- logger .debug ("extract_custom_headers: " , exc_info = True )
47
-
48
32
def _collect_kvs (self , scope : Dict [str , Any ], span : "InstanaSpan" ) -> None :
49
33
try :
50
34
span .set_attribute ("span.kind" , SpanKind .SERVER )
@@ -93,8 +77,8 @@ async def __call__(
93
77
94
78
with tracer .start_as_current_span ("asgi" , span_context = request_context ) as span :
95
79
self ._collect_kvs (scope , span )
96
- if "headers" in scope and agent . options . extra_http_headers :
97
- self . _extract_custom_headers (span , scope ["headers" ])
80
+ if "headers" in scope :
81
+ extract_custom_headers (span , scope ["headers" ])
98
82
99
83
instana_send = self ._send_with_instana (
100
84
span ,
@@ -125,7 +109,7 @@ async def send_wrapper(response: Dict[str, Any]) -> Awaitable[None]:
125
109
126
110
headers = response .get ("headers" )
127
111
if headers :
128
- self . _extract_custom_headers (current_span , headers )
112
+ extract_custom_headers (current_span , headers )
129
113
tracer .inject (current_span .context , Format .BINARY , headers )
130
114
except Exception :
131
115
logger .debug ("ASGI send_wrapper error: " , exc_info = True )
0 commit comments