4
4
Instrumentation for Sanic
5
5
https://sanicframework.org/en/
6
6
"""
7
+
7
8
try :
8
9
import sanic
9
10
import wrapt
27
28
from sanic .request import Request
28
29
from sanic .response import HTTPResponse
29
30
30
-
31
31
@wrapt .patch_function_wrapper ("sanic.app" , "Sanic.__init__" )
32
32
def init_with_instana (
33
33
wrapped : Callable [..., sanic .app .Sanic .__init__ ],
@@ -43,7 +43,7 @@ def request_with_instana(request: Request) -> None:
43
43
try :
44
44
if "http" not in request .scheme :
45
45
return
46
-
46
+
47
47
headers = request .headers .copy ()
48
48
parent_context = tracer .extract (Format .HTTP_HEADERS , headers )
49
49
@@ -54,8 +54,8 @@ def request_with_instana(request: Request) -> None:
54
54
token = context .attach (ctx )
55
55
request .ctx .token = token
56
56
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 )
59
59
span .set_attribute (SpanAttributes .HTTP_METHOD , request .method )
60
60
span .set_attribute (SpanAttributes .HTTP_HOST , request .host )
61
61
if hasattr (request , "url" ):
@@ -65,9 +65,10 @@ def request_with_instana(request: Request) -> None:
65
65
66
66
if isinstance (query , (str , bytes )) and len (query ):
67
67
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
+ )
71
72
span .set_attribute ("http.params" , scrubbed_params )
72
73
73
74
if agent .options .extra_http_headers :
@@ -77,7 +78,6 @@ def request_with_instana(request: Request) -> None:
77
78
except Exception :
78
79
logger .debug ("request_with_instana: " , exc_info = True )
79
80
80
-
81
81
@app .exception (Exception )
82
82
def exception_with_instana (request : Request , exception : Exception ) -> None :
83
83
try :
@@ -95,7 +95,6 @@ def exception_with_instana(request: Request, exception: Exception) -> None:
95
95
except Exception :
96
96
logger .debug ("exception_with_instana: " , exc_info = True )
97
97
98
-
99
98
@app .middleware ("response" )
100
99
def response_with_instana (request : Request , response : HTTPResponse ) -> None :
101
100
try :
@@ -107,12 +106,14 @@ def response_with_instana(request: Request, response: HTTPResponse) -> None:
107
106
if status_code :
108
107
if int (status_code ) >= 500 :
109
108
span .mark_as_errored ()
110
- span .set_attribute ('http.status_code' , status_code )
109
+ span .set_attribute (SpanAttributes . HTTP_STATUS_CODE , status_code )
111
110
112
111
if hasattr (response , "headers" ):
113
112
extract_custom_headers (span , response .headers )
114
113
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
+ )
116
117
117
118
if span .is_recording ():
118
119
span .end ()
@@ -125,4 +126,4 @@ def response_with_instana(request: Request, response: HTTPResponse) -> None:
125
126
logger .debug ("response_with_instana: " , exc_info = True )
126
127
127
128
except ImportError :
128
- pass
129
+ pass
0 commit comments