|
| 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 | +import logging |
| 19 | + |
| 20 | +from skywalking import Layer, Component, config |
| 21 | +from skywalking.trace.carrier import Carrier |
| 22 | +from skywalking.trace.context import get_context, NoopContext |
| 23 | +from skywalking.trace.span import NoopSpan |
| 24 | +from skywalking.trace.tags import TagHttpMethod, TagHttpURL, TagHttpStatusCode, TagHttpParams |
| 25 | + |
| 26 | +logger = logging.getLogger(__name__) |
| 27 | + |
| 28 | +link_vector = ['https://sanic.readthedocs.io/en/latest'] |
| 29 | +support_matrix = { |
| 30 | + 'sanic': { |
| 31 | + '>=3.14': ['24.12.*'], |
| 32 | + '>=3.10': ['23.12.*', '24.12.*'], |
| 33 | + } |
| 34 | +} |
| 35 | +note = """Sanic 21.9+ plugin using signal listeners. |
| 36 | +For legacy Sanic <=21.3, see sw_sanic. |
| 37 | +Note: Sanic's touchup system recompiles handle_request at startup, |
| 38 | +so we use signal listeners instead of monkey-patching handle_request.""" |
| 39 | + |
| 40 | + |
| 41 | +def install(): |
| 42 | + from sanic import Sanic |
| 43 | + |
| 44 | + # Guard: if handle_request still has write_callback param, this is old Sanic — let sw_sanic handle it |
| 45 | + import inspect |
| 46 | + sig = inspect.signature(Sanic.handle_request) |
| 47 | + if 'write_callback' in sig.parameters: |
| 48 | + return # old Sanic, skip |
| 49 | + |
| 50 | + _original_init = Sanic.__init__ |
| 51 | + |
| 52 | + def _sw_init(self, *args, **kwargs): |
| 53 | + _original_init(self, *args, **kwargs) |
| 54 | + _register_listeners(self) |
| 55 | + |
| 56 | + Sanic.__init__ = _sw_init |
| 57 | + |
| 58 | + |
| 59 | +def _register_listeners(app): |
| 60 | + |
| 61 | + def params_tostring(params): |
| 62 | + return '\n'.join([f"{k}=[{','.join(params.getlist(k))}]" for k, _ in params.items()]) |
| 63 | + |
| 64 | + @app.on_request |
| 65 | + async def sw_on_request(request): |
| 66 | + carrier = Carrier() |
| 67 | + method = request.method |
| 68 | + |
| 69 | + for item in carrier: |
| 70 | + if item.key.capitalize() in request.headers: |
| 71 | + item.val = request.headers[item.key.capitalize()] |
| 72 | + |
| 73 | + span = NoopSpan(NoopContext()) if config.ignore_http_method_check(method) \ |
| 74 | + else get_context().new_entry_span(op=request.path, carrier=carrier) |
| 75 | + |
| 76 | + span.start() |
| 77 | + span.layer = Layer.Http |
| 78 | + span.component = Component.Sanic |
| 79 | + span.peer = f'{request.remote_addr or request.ip}:{request.port}' |
| 80 | + span.tag(TagHttpMethod(method)) |
| 81 | + span.tag(TagHttpURL(request.url.split('?')[0])) |
| 82 | + if config.plugin_sanic_collect_http_params and request.args: |
| 83 | + span.tag(TagHttpParams( |
| 84 | + params_tostring(request.args)[0:config.plugin_http_http_params_length_threshold] |
| 85 | + )) |
| 86 | + |
| 87 | + request.ctx._sw_span = span |
| 88 | + |
| 89 | + @app.on_response |
| 90 | + async def sw_on_response(request, response): |
| 91 | + span = getattr(request.ctx, '_sw_span', None) |
| 92 | + if span is None: |
| 93 | + return |
| 94 | + |
| 95 | + if response is not None: |
| 96 | + span.tag(TagHttpStatusCode(response.status)) |
| 97 | + if response.status >= 400: |
| 98 | + span.error_occurred = True |
| 99 | + |
| 100 | + span.stop() |
0 commit comments