|
| 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 pyramid.router import Router |
| 27 | + |
| 28 | + def _sw_invoke_request(self, request, *args, **kwargs): |
| 29 | + context = get_context() |
| 30 | + carrier = Carrier() |
| 31 | + |
| 32 | + for item in carrier: |
| 33 | + val = request.headers.get(item.key) |
| 34 | + |
| 35 | + if val is not None: |
| 36 | + item.val = val |
| 37 | + |
| 38 | + with context.new_entry_span(op=request.path, carrier=carrier) as span: |
| 39 | + span.layer = Layer.Http |
| 40 | + span.component = Component.Pyramid |
| 41 | + span.peer = request.remote_host or request.remote_addr |
| 42 | + |
| 43 | + span.tag(Tag(key=tags.HttpMethod, val=request.method)) |
| 44 | + span.tag(Tag(key=tags.HttpUrl, val=str(request.url))) |
| 45 | + |
| 46 | + resp = _invoke_request(self, request, *args, **kwargs) |
| 47 | + |
| 48 | + span.tag(Tag(key=tags.HttpStatus, val=resp.status_code, overridable=True)) |
| 49 | + |
| 50 | + if resp.status_code >= 400: |
| 51 | + span.error_occurred = True |
| 52 | + |
| 53 | + return resp |
| 54 | + |
| 55 | + _invoke_request = Router.invoke_request |
| 56 | + Router.invoke_request = _sw_invoke_request |
0 commit comments