Skip to content

Commit 751bca9

Browse files
committed
add plugins support for bottle
1 parent 72903ff commit 751bca9

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

skywalking/plugins/sw_bottle.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.carrier import Carrier
20+
from skywalking.trace.context import get_context, NoopContext
21+
from skywalking.trace.span import NoopSpan
22+
from skywalking.trace.tags import TagHttpMethod, TagHttpURL, TagHttpStatusCode
23+
from bottle import Router
24+
25+
link_vector = ['http://bottlepy.org/docs/dev/']
26+
27+
def do(environ):
28+
carrier = Carrier()
29+
path = environ.get('PATH_INFO')
30+
query = environ.get('QUERY_STRING')
31+
if query and query != '':
32+
path = path + '?' + query
33+
34+
method = environ['REQUEST_METHOD'].upper()
35+
36+
span = NoopSpan(NoopContext()) if config.ignore_http_method_check(method) \
37+
else get_context().new_entry_span(op=path.split('?')[0], carrier=carrier)
38+
39+
with span:
40+
url = f"http://{environ.get('HTTP_HOST')}{path}"
41+
span.layer = Layer.Http
42+
span.component = Component.General
43+
span.peer = f'{environ.get("REMOTE_ADDR")}:{environ.get("REMOTE_PORT")}'
44+
span.tag(TagHttpMethod(method))
45+
span.tag(TagHttpURL(url))
46+
47+
def install():
48+
_match = Router.match
49+
50+
def sw_match(self, environ):
51+
method = environ['REQUEST_METHOD'].upper()
52+
path = environ['PATH_INFO'] or '/'
53+
54+
do(environ)
55+
56+
return _match(self, environ)
57+
58+
Router.match = sw_match

0 commit comments

Comments
 (0)