-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
150 lines (116 loc) · 3.82 KB
/
Copy pathserver.py
File metadata and controls
150 lines (116 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# patch before importing tornado
from ddtrace import Pin, patch, tracer
patch(tornado=True)
import os
import time
import random
import redis
import tornado.httpserver
import tornado.httpclient
from tornado.gen import coroutine, sleep as gen_sleep
from tornado.concurrent import run_on_executor
from tornado.web import (
Application, RequestHandler, RedirectHandler, StaticFileHandler,
)
from concurrent.futures import ThreadPoolExecutor
# env vars for deploying purpose
DATADOG_TRACER = os.getenv('DATADOG_TRACER', 'localhost')
PORT = int(os.getenv('APP_PORT', '8000'))
BASE_DIR = os.path.dirname(os.path.realpath(__file__))
STATIC_DIR = os.path.join(BASE_DIR, 'statics')
# configure the tracer
tracer.configure(hostname=DATADOG_TRACER)
# patch redis with Pin
patch(redis=True)
url = os.getenv('REDIS_URL', 'redis://localhost:6379')
client = redis.StrictRedis.from_url(url, db=0)
Pin.override(client, service='tornado-redis')
# trace a synchronous function
@tracer.wrap('tornado.unit_counter')
def count_unit():
# this call is automatically traced
client.incr('tornado:unit_counter')
class MainHandler(RequestHandler):
@coroutine
def get(self):
yield gen_sleep(0.05)
yield self.delayed_work()
# external interaction that is automatically traced
count_unit()
self.write('OK')
# trace a coroutine
@tracer.wrap()
@coroutine
def delayed_work(self):
# fake some yielding
yield gen_sleep(0.15)
class BrokenHandler(RequestHandler):
@coroutine
def get(self):
initial_value = random.randint(0, 1000)
yield gen_sleep(0.01)
# trace more work
with tracer.trace('tornado.broken_queue', span_type='http') as span:
span.set_tag('tornado.initial_value', initial_value)
yield gen_sleep(0.015)
# but something bad happen
raise Exception('Ouch!')
class ExecutorHandler(RequestHandler):
executor = ThreadPoolExecutor(max_workers=10)
@tracer.wrap('tornado.workload_1')
@run_on_executor
def workload_1(self):
time.sleep(0.5)
return 'workload_1'
@run_on_executor
@tracer.wrap('tornado.workload_2')
def workload_2(self):
time.sleep(0.5)
return 'workload_2'
@tracer.wrap('tornado.do_access')
@run_on_executor
def do_access(self):
time.sleep(0.5)
count_unit()
return 'redis_access'
@run_on_executor
def do_sleep(self):
with tracer.trace('sleep'):
time.sleep(random.uniform(0.1, 0.5))
return 'sleeping'
@coroutine
def get(self):
yield gen_sleep(0.5)
result_1 = yield self.workload_1()
result_2 = yield self.workload_2()
result_3 = yield self.do_access()
results = []
results.append(result_1)
results.append(result_2)
results.append(result_3)
# This span will have the right parent and duration
with tracer.trace('tornado.executors.sleeps'):
sleeps = [self.do_sleep() for i in range(8)]
with tracer.trace('number_crunching'):
results.append(42 * 42)
sleeps = yield sleeps
results += sleeps
self.write('Results: {}'.format(results))
def make_app(settings={}):
return Application([
(r'/count/', MainHandler),
(r'/broken/', BrokenHandler),
(r'/executor/', ExecutorHandler),
(r'/redirect/', RedirectHandler, {'url': '/count/'}),
(r'/statics/(.*)', StaticFileHandler, {'path': STATIC_DIR}),
], **settings)
if __name__ == "__main__":
settings = {
'datadog_trace': {
'default_service': 'tornado-website',
}
}
app = make_app(settings=settings)
app.listen(PORT)
print('-- Starting the server --')
tornado.ioloop.IOLoop.current().start()