|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +from skywalking import Layer, Component |
| 19 | +from skywalking.trace.carrier import Carrier |
| 20 | +from skywalking.trace.context import get_context |
| 21 | +from skywalking.trace.span import NoopSpan |
| 22 | +from skywalking.trace.tags import TagHttpMethod, TagHttpURL, TagHttpParams, TagHttpStatusCode |
| 23 | + |
| 24 | + |
| 25 | +def install(): |
| 26 | + from falcon import API, request, response |
| 27 | + |
| 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 | + |
| 34 | + def _sw_falcon_api(this: API, env, start_response): |
| 35 | + context = get_context() |
| 36 | + carrier = Carrier() |
| 37 | + headers = get_headers(env) |
| 38 | + 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 |
| 45 | + |
| 46 | + from falcon import RequestOptions |
| 47 | + |
| 48 | + req = request.Request(env, RequestOptions()) |
| 49 | + span.op = str(req.url).split("?")[0] |
| 50 | + span.peer = "%s:%s" % (req.remote_addr, req.port) |
| 51 | + |
| 52 | + span.tag(TagHttpMethod(req.method)) |
| 53 | + span.tag(TagHttpURL(str(req.url))) |
| 54 | + if req.params: |
| 55 | + span.tag(TagHttpParams(params_tostring(req.params)[0:])) |
| 56 | + |
| 57 | + resp = _original_falcon_api(this, env, start_response) |
| 58 | + |
| 59 | + from falcon import ResponseOptions |
| 60 | + |
| 61 | + resp_obj = response.Response(ResponseOptions()) |
| 62 | + |
| 63 | + resp_status = parse_status(resp_obj.status) |
| 64 | + if int(resp_status[0]) >= 400: |
| 65 | + span.error_occurred = True |
| 66 | + |
| 67 | + span.tag(TagHttpStatusCode(int(resp_status[0]))) |
| 68 | + |
| 69 | + return resp |
| 70 | + |
| 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() |
| 76 | + |
| 77 | + return _original_falcon_handle_exception(this, req, resp, ex, params) |
| 78 | + |
| 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