|
| 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 import tags |
| 20 | +from skywalking.trace.carrier import Carrier |
| 21 | +from skywalking.trace.context import get_context |
| 22 | +from skywalking.trace.tags import Tag |
| 23 | + |
| 24 | + |
| 25 | +def install(): |
| 26 | + from aiohttp import ClientSession |
| 27 | + from aiohttp.web_protocol import RequestHandler |
| 28 | + from multidict import CIMultiDict, MultiDict, MultiDictProxy |
| 29 | + from yarl import URL |
| 30 | + |
| 31 | + async def _sw_request(self: ClientSession, method: str, str_or_url, **kwargs): |
| 32 | + url = URL(str_or_url).with_user(None).with_password(None) |
| 33 | + peer = '%s:%d' % (url.host or '', url.port) |
| 34 | + context = get_context() |
| 35 | + |
| 36 | + with context.new_exit_span(op=url.path or "/", peer=peer) as span: |
| 37 | + span.layer = Layer.Http |
| 38 | + span.component = Component.AioHttp |
| 39 | + span.tag(Tag(key=tags.HttpMethod, val=method.upper())) # pyre-ignore |
| 40 | + span.tag(Tag(key=tags.HttpUrl, val=url)) # pyre-ignore |
| 41 | + |
| 42 | + carrier = span.inject() |
| 43 | + headers = kwargs.get('headers') |
| 44 | + |
| 45 | + if headers is None: |
| 46 | + headers = kwargs['headers'] = CIMultiDict() |
| 47 | + elif not isinstance(headers, (MultiDictProxy, MultiDict)): |
| 48 | + headers = CIMultiDict(headers) |
| 49 | + |
| 50 | + for item in carrier: |
| 51 | + headers.add(item.key, item.val) |
| 52 | + |
| 53 | + res = await _request(self, method, str_or_url, **kwargs) |
| 54 | + |
| 55 | + span.tag(Tag(key=tags.HttpStatus, val=res.status, overridable=True)) |
| 56 | + |
| 57 | + if res.status >= 400: |
| 58 | + span.error_occurred = True |
| 59 | + |
| 60 | + return res |
| 61 | + |
| 62 | + _request = ClientSession._request |
| 63 | + ClientSession._request = _sw_request |
| 64 | + |
| 65 | + async def _sw_handle_request(self, request, start_time: float): |
| 66 | + context = get_context() |
| 67 | + carrier = Carrier() |
| 68 | + |
| 69 | + for item in carrier: |
| 70 | + val = request.headers.get(item.key) |
| 71 | + |
| 72 | + if val is not None: |
| 73 | + item.val = val |
| 74 | + |
| 75 | + with context.new_entry_span(op=request.path, carrier=carrier) as span: |
| 76 | + span.layer = Layer.Http |
| 77 | + span.component = Component.AioHttp |
| 78 | + span.peer = '%s:%d' % request._transport_peername if isinstance(request._transport_peername, (list, tuple))\ |
| 79 | + else request._transport_peername |
| 80 | + |
| 81 | + span.tag(Tag(key=tags.HttpMethod, val=request.method)) # pyre-ignore |
| 82 | + span.tag(Tag(key=tags.HttpUrl, val=str(request.url))) # pyre-ignore |
| 83 | + |
| 84 | + resp, reset = await _handle_request(self, request, start_time) |
| 85 | + |
| 86 | + span.tag(Tag(key=tags.HttpStatus, val=resp.status, overridable=True)) |
| 87 | + |
| 88 | + if resp.status >= 400: |
| 89 | + span.error_occurred = True |
| 90 | + |
| 91 | + return resp, reset |
| 92 | + |
| 93 | + _handle_request = RequestHandler._handle_request |
| 94 | + RequestHandler._handle_request = _sw_handle_request |
0 commit comments