44Instrumentation for Sanic
55https://sanicframework.org/en/
66"""
7+
78try :
89 import sanic
910 import wrapt
2728 from sanic .request import Request
2829 from sanic .response import HTTPResponse
2930
30-
3131 @wrapt .patch_function_wrapper ("sanic.app" , "Sanic.__init__" )
3232 def init_with_instana (
3333 wrapped : Callable [..., sanic .app .Sanic .__init__ ],
@@ -43,7 +43,7 @@ def request_with_instana(request: Request) -> None:
4343 try :
4444 if "http" not in request .scheme :
4545 return
46-
46+
4747 headers = request .headers .copy ()
4848 parent_context = tracer .extract (Format .HTTP_HEADERS , headers )
4949
@@ -54,8 +54,8 @@ def request_with_instana(request: Request) -> None:
5454 token = context .attach (ctx )
5555 request .ctx .token = token
5656
57- span .set_attribute (' span.kind' , SpanKind .CLIENT )
58- span .set_attribute (' http.path' , request .path )
57+ span .set_attribute (" span.kind" , SpanKind .CLIENT )
58+ span .set_attribute (" http.path" , request .path )
5959 span .set_attribute (SpanAttributes .HTTP_METHOD , request .method )
6060 span .set_attribute (SpanAttributes .HTTP_HOST , request .host )
6161 if hasattr (request , "url" ):
@@ -65,9 +65,10 @@ def request_with_instana(request: Request) -> None:
6565
6666 if isinstance (query , (str , bytes )) and len (query ):
6767 if isinstance (query , bytes ):
68- query = query .decode ('utf-8' )
69- scrubbed_params = strip_secrets_from_query (query , agent .options .secrets_matcher ,
70- agent .options .secrets_list )
68+ query = query .decode ("utf-8" )
69+ scrubbed_params = strip_secrets_from_query (
70+ query , agent .options .secrets_matcher , agent .options .secrets_list
71+ )
7172 span .set_attribute ("http.params" , scrubbed_params )
7273
7374 if agent .options .extra_http_headers :
@@ -77,7 +78,6 @@ def request_with_instana(request: Request) -> None:
7778 except Exception :
7879 logger .debug ("request_with_instana: " , exc_info = True )
7980
80-
8181 @app .exception (Exception )
8282 def exception_with_instana (request : Request , exception : Exception ) -> None :
8383 try :
@@ -95,7 +95,6 @@ def exception_with_instana(request: Request, exception: Exception) -> None:
9595 except Exception :
9696 logger .debug ("exception_with_instana: " , exc_info = True )
9797
98-
9998 @app .middleware ("response" )
10099 def response_with_instana (request : Request , response : HTTPResponse ) -> None :
101100 try :
@@ -107,12 +106,14 @@ def response_with_instana(request: Request, response: HTTPResponse) -> None:
107106 if status_code :
108107 if int (status_code ) >= 500 :
109108 span .mark_as_errored ()
110- span .set_attribute ('http.status_code' , status_code )
109+ span .set_attribute (SpanAttributes . HTTP_STATUS_CODE , status_code )
111110
112111 if hasattr (response , "headers" ):
113112 extract_custom_headers (span , response .headers )
114113 tracer .inject (span .context , Format .HTTP_HEADERS , response .headers )
115- response .headers ['Server-Timing' ] = "intid;desc=%s" % span .context .trace_id
114+ response .headers ["Server-Timing" ] = (
115+ "intid;desc=%s" % span .context .trace_id
116+ )
116117
117118 if span .is_recording ():
118119 span .end ()
@@ -125,4 +126,4 @@ def response_with_instana(request: Request, response: HTTPResponse) -> None:
125126 logger .debug ("response_with_instana: " , exc_info = True )
126127
127128except ImportError :
128- pass
129+ pass
0 commit comments