|
| 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, config |
| 19 | +from skywalking.trace.context import get_context, NoopContext |
| 20 | +from skywalking.trace.span import NoopSpan |
| 21 | +from skywalking.trace.tags import TagHttpMethod, TagHttpURL, TagHttpStatusCode |
| 22 | + |
| 23 | +link_vector = ['https://www.python-httpx.org/'] |
| 24 | +support_matrix = { |
| 25 | + 'httpx': { |
| 26 | + '>=3.7': ['0.23.*', '0.22.*'] |
| 27 | + } |
| 28 | +} |
| 29 | +note = """""" |
| 30 | + |
| 31 | + |
| 32 | +def install(): |
| 33 | + from httpx import _client |
| 34 | + from httpx import USE_CLIENT_DEFAULT |
| 35 | + |
| 36 | + _async_send = _client.AsyncClient.send |
| 37 | + _send = _client.Client.send |
| 38 | + |
| 39 | + async def _sw_async_send(self, request, *, stream=False, auth=USE_CLIENT_DEFAULT, |
| 40 | + follow_redirects=USE_CLIENT_DEFAULT, ): |
| 41 | + url_object = request.url |
| 42 | + |
| 43 | + span = NoopSpan(NoopContext()) if config.ignore_http_method_check( |
| 44 | + request.method) else get_context().new_exit_span(op=url_object.path or '/', peer=url_object.netloc.decode(), |
| 45 | + component=Component.HTTPX) |
| 46 | + with span: |
| 47 | + carrier = span.inject() |
| 48 | + span.layer = Layer.Http |
| 49 | + |
| 50 | + if not request.headers: |
| 51 | + request.headers = {} |
| 52 | + for item in carrier: |
| 53 | + request.headers[item.key] = item.val |
| 54 | + |
| 55 | + span.tag(TagHttpMethod(request.method.upper())) |
| 56 | + url_safe = str(url_object).replace(url_object.username, '').replace(url_object.password, '') |
| 57 | + |
| 58 | + span.tag(TagHttpURL(url_safe)) |
| 59 | + |
| 60 | + res = await _async_send(self, request, stream=stream, auth=auth, follow_redirects=follow_redirects) |
| 61 | + |
| 62 | + status_code = res.status_code |
| 63 | + span.tag(TagHttpStatusCode(status_code)) |
| 64 | + |
| 65 | + if status_code >= 400: |
| 66 | + span.error_occurred = True |
| 67 | + |
| 68 | + return res |
| 69 | + |
| 70 | + _client.AsyncClient.send = _sw_async_send |
| 71 | + |
| 72 | + def _sw_send(self, request, *, stream=False, auth=USE_CLIENT_DEFAULT, follow_redirects=USE_CLIENT_DEFAULT, ): |
| 73 | + url_object = request.url |
| 74 | + |
| 75 | + span = NoopSpan(NoopContext()) if config.ignore_http_method_check( |
| 76 | + request.method) else get_context().new_exit_span(op=url_object.path or '/', peer=url_object.netloc.decode(), |
| 77 | + component=Component.HTTPX) |
| 78 | + with span: |
| 79 | + carrier = span.inject() |
| 80 | + span.layer = Layer.Http |
| 81 | + |
| 82 | + if not request.headers: |
| 83 | + request.headers = {} |
| 84 | + for item in carrier: |
| 85 | + request.headers[item.key] = item.val |
| 86 | + |
| 87 | + span.tag(TagHttpMethod(request.method.upper())) |
| 88 | + url_safe = str(url_object).replace(url_object.username, '').replace(url_object.password, '') |
| 89 | + |
| 90 | + span.tag(TagHttpURL(url_safe)) |
| 91 | + |
| 92 | + res = _send(self, request, stream=stream, auth=auth, follow_redirects=follow_redirects) |
| 93 | + |
| 94 | + status_code = res.status_code |
| 95 | + span.tag(TagHttpStatusCode(status_code)) |
| 96 | + |
| 97 | + if status_code >= 400: |
| 98 | + span.error_occurred = True |
| 99 | + |
| 100 | + return res |
| 101 | + |
| 102 | + _client.Client.send = _sw_send |
0 commit comments