|
15 | 15 | # limitations under the License. |
16 | 16 | # |
17 | 17 |
|
18 | | -from skywalking import Layer, Component |
| 18 | +from skywalking import Layer, Component, config |
19 | 19 | from skywalking.trace.carrier import Carrier |
20 | | -from skywalking.trace.context import get_context |
| 20 | +from skywalking.trace.context import get_context, NoopContext |
21 | 21 | from skywalking.trace.span import NoopSpan |
22 | | -from skywalking.trace.tags import TagHttpMethod, TagHttpURL, TagHttpParams, TagHttpStatusCode |
| 22 | +from skywalking.trace.tags import TagHttpMethod, TagHttpURL, TagHttpParams, TagHttpStatusCode, TagHttpStatusMsg |
23 | 23 |
|
24 | 24 |
|
25 | 25 | def install(): |
26 | | - from falcon import API, request, response |
| 26 | + from falcon import API, request, RequestOptions |
27 | 27 |
|
28 | 28 | _original_falcon_api = API.__call__ |
29 | | - _original_falcon_handle_exception = API._handle_exception |
30 | | - |
31 | | - def params_tostring(params): |
32 | | - return "\n".join([k + "=" + v for k, v in params.items()]) |
33 | 29 |
|
34 | 30 | def _sw_falcon_api(this: API, env, start_response): |
35 | 31 | context = get_context() |
36 | 32 | carrier = Carrier() |
37 | | - headers = get_headers(env) |
| 33 | + req = request.Request(env, RequestOptions()) |
| 34 | + headers = req.headers |
| 35 | + method = req.method |
| 36 | + |
38 | 37 | for item in carrier: |
39 | | - key = item.key.replace("_", "-") if "_" in item.key else item.key |
40 | | - if key.capitalize() in headers: |
41 | | - item.val = headers[key.capitalize()] |
42 | | - with context.new_entry_span(op="/", carrier=carrier) as span: |
43 | | - span.layer = Layer.Http |
44 | | - span.component = Component.Falcon |
| 38 | + key = item.key.upper() |
| 39 | + if key in headers: |
| 40 | + item.val = headers[key] |
45 | 41 |
|
46 | | - from falcon import RequestOptions |
| 42 | + span = NoopSpan(NoopContext()) if config.ignore_http_method_check(method) \ |
| 43 | + else context.new_entry_span(op=req.path, carrier=carrier) |
47 | 44 |
|
48 | | - req = request.Request(env, RequestOptions()) |
49 | | - span.op = str(req.url).split("?")[0] |
50 | | - span.peer = "%s:%s" % (req.remote_addr, req.port) |
| 45 | + with span: |
| 46 | + span.layer = Layer.Http |
| 47 | + span.component = Component.Falcon |
| 48 | + span.peer = req.remote_addr |
51 | 49 |
|
52 | | - span.tag(TagHttpMethod(req.method)) |
| 50 | + span.tag(TagHttpMethod(method)) |
53 | 51 | span.tag(TagHttpURL(str(req.url))) |
54 | | - if req.params: |
55 | | - span.tag(TagHttpParams(params_tostring(req.params)[0:])) |
56 | 52 |
|
57 | | - resp = _original_falcon_api(this, env, start_response) |
| 53 | + if req.params: |
| 54 | + span.tag(TagHttpParams(','.join([k + '=' + v for k, v in req.params.items()]))) |
58 | 55 |
|
59 | | - from falcon import ResponseOptions |
| 56 | + def _start_response(resp_status, headers): |
| 57 | + try: |
| 58 | + code, msg = resp_status.split(' ', 1) |
| 59 | + code = int(code) |
| 60 | + except Exception: |
| 61 | + code, msg = 500, 'Internal Server Error' |
60 | 62 |
|
61 | | - resp_obj = response.Response(ResponseOptions()) |
| 63 | + if code >= 400: |
| 64 | + span.error_occurred = True |
62 | 65 |
|
63 | | - resp_status = parse_status(resp_obj.status) |
64 | | - if int(resp_status[0]) >= 400: |
65 | | - span.error_occurred = True |
| 66 | + span.tag(TagHttpStatusCode(code)) |
| 67 | + span.tag(TagHttpStatusMsg(msg)) |
66 | 68 |
|
67 | | - span.tag(TagHttpStatusCode(int(resp_status[0]))) |
| 69 | + return start_response(resp_status, headers) |
68 | 70 |
|
69 | | - return resp |
| 71 | + try: |
| 72 | + return _original_falcon_api(this, env, _start_response) |
70 | 73 |
|
71 | | - def _sw_handle_exception(this: API, req, resp, ex, params): |
72 | | - if ex is not None: |
73 | | - entry_span = get_context().active_span() |
74 | | - if entry_span is not None and type(entry_span) is not NoopSpan: |
75 | | - entry_span.raised() |
| 74 | + except Exception: |
| 75 | + span.raised() |
76 | 76 |
|
77 | | - return _original_falcon_handle_exception(this, req, resp, ex, params) |
| 77 | + raise |
78 | 78 |
|
79 | 79 | API.__call__ = _sw_falcon_api |
80 | | - API._handle_exception = _sw_handle_exception |
81 | | - |
82 | | - |
83 | | -def get_headers(env): |
84 | | - headers = {} |
85 | | - wsgi_content_headers = frozenset(["CONTENT_TYPE", "CONTENT_LENGTH"]) |
86 | | - |
87 | | - for name, value in env.items(): |
88 | | - if name.startswith("HTTP_"): |
89 | | - headers[name[5:].replace("_", "-")] = value |
90 | | - |
91 | | - elif name in wsgi_content_headers: |
92 | | - headers[name.replace("_", "-")] = value |
93 | | - |
94 | | - return headers |
95 | | - |
96 | | - |
97 | | -def parse_status(status_str): |
98 | | - return status_str.split(" ") if status_str else [404, "status is empty"] |
0 commit comments