Skip to content

Commit ab9ee6e

Browse files
committed
Add bottle support
Fix #55
1 parent 998dd19 commit ab9ee6e

File tree

5 files changed

+51
-2
lines changed

5 files changed

+51
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ For most technologies, just import it in your code.
2626
### Technologies supported:
2727

2828
- **aiohttp** (client)
29+
- **bottle**
2930
- **celery**
3031
- **concurrent.futures**
3132
- **confluent_kafka**
3233
- **cx_Oracle**
3334
- **django**
3435
- **fastapi**
3536
- **flask**
36-
- **grpc**
37+
- **grpc** (client)
3738
- **paramiko**
3839
- **pika** (RabbitMQ)
3940
- **psycopg2**

autodynatrace/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"fastapi": True,
5454
"starlette": True,
5555
"aiohttp": True,
56+
"bottle": True,
5657
}
5758

5859

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .wrapper import instrument
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from ...log import logger
2+
from ...sdk import sdk
3+
4+
import os
5+
6+
7+
from bottle import hook, request, response
8+
9+
10+
def instrument():
11+
@hook("before_request")
12+
def instrument_before_request():
13+
try:
14+
# extract host and port from the request
15+
host = request.environ.get("HTTP_HOST", "unknown")
16+
app_name = "{}".format(host)
17+
18+
virtual_host = os.environ.get("AUTODYNATRACE_VIRTUAL_HOST", "{}".format(host))
19+
app_name = os.environ.get("AUTODYNATRACE_APPLICATION_ID", "Bottle ({})".format(app_name))
20+
context_root = os.environ.get("AUTODYNATRACE_CONTEXT_ROOT", "/")
21+
22+
# Create the oneagent web app
23+
web_app_info = sdk.create_web_application_info(virtual_host, app_name, context_root)
24+
25+
with web_app_info:
26+
# Attempt to extract the x-dynatrace header from the request
27+
dynatrace_header = request.headers.get("x-dynatrace")
28+
logger.debug("Bottle - tracing incoming request ({}) with header: {}".format(request.url, dynatrace_header))
29+
tracer = sdk.trace_incoming_web_request(web_app_info, request.url, request.method, headers=request.headers, str_tag=dynatrace_header)
30+
tracer.start()
31+
setattr(request, "__dynatrace_tracer", tracer)
32+
except Exception as e:
33+
logger.warning("Bottle - failed to instrument request: {}".format(e))
34+
35+
@hook("after_request")
36+
def instrument_after_request():
37+
try:
38+
# check if the request was instrumented
39+
tracer = getattr(request, "__dynatrace_tracer", None)
40+
if tracer:
41+
tracer.set_status_code(response.status_code)
42+
tracer.add_response_headers(response.headers)
43+
logger.debug("Bottle - ending incoming request ({}) with status: {}".format(request.url, response.status_code))
44+
tracer.end()
45+
except Exception as e:
46+
logger.warning("Bottle - failed to instrument response: {}".format(e))

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="autodynatrace",
5-
version="1.0.83",
5+
version="1.0.84",
66
packages=find_packages(),
77
package_data={"autodynatrace": ["wrappers/*"]},
88
install_requires=["wrapt>=1.11.2", "oneagent-sdk>=1.3.0", "six>=1.10.0", "autowrapt>=1.0"],

0 commit comments

Comments
 (0)