forked from stackimpact/stackimpact-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
253 lines (181 loc) · 6.78 KB
/
Copy pathagent.py
File metadata and controls
253 lines (181 loc) · 6.78 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
from __future__ import division, print_function, absolute_import
import time
import datetime
import sys
import traceback
import socket
import threading
import os
import signal
import atexit
import platform
from .runtime import min_version, runtime_info, register_signal
from .utils import timestamp, generate_uuid
from .config import Config
from .config_loader import ConfigLoader
from .message_queue import MessageQueue
from .frame_selector import FrameSelector
from .reporters.process_reporter import ProcessReporter
from .reporters.cpu_reporter import CPUReporter
from .reporters.allocation_reporter import AllocationReporter
from .reporters.block_reporter import BlockReporter
from .reporters.error_reporter import ErrorReporter
class Agent:
AGENT_VERSION = "1.0.2"
SAAS_DASHBOARD_ADDRESS = "https://agent-api.stackimpact.com"
def __init__(self, **kwargs):
self.agent_started = False
self.agent_destroyed = False
self.profiler_lock = threading.Lock()
self.main_thread_func = None
self.run_ts = None
self.run_id = None
self.config = Config(self)
self.config_loader = ConfigLoader(self)
self.message_queue = MessageQueue(self)
self.frame_selector = FrameSelector(self)
self.process_reporter = ProcessReporter(self)
self.cpu_reporter = CPUReporter(self)
self.allocation_reporter = AllocationReporter(self)
self.block_reporter = BlockReporter(self)
self.error_reporter = ErrorReporter(self)
self.options = None
def get_option(self, name, default_val = None):
if name not in self.options:
return default_val
else:
return self.options[name]
def start(self, **kwargs):
if not min_version(2, 7) and not min_version(3, 4):
raise Exception('Supported Python versions 2.6 or highter and 3.4 or higher')
if platform.python_implementation() != 'CPython':
raise Exception('Supported Python interpreter is CPython')
if self.agent_destroyed:
self.log('Destroyed agent cannot be started')
return
if self.agent_started:
return
self.options = kwargs
if 'dashboard_address' not in self.options:
self.options['dashboard_address'] = self.SAAS_DASHBOARD_ADDRESS
if 'agent_key' not in self.options:
raise Exception('missing option: agent_key')
if 'app_name' not in self.options:
raise Exception('missing option: app_name')
if 'host_name' not in self.options:
self.options['host_name'] = socket.gethostname()
self.run_id = generate_uuid()
self.run_ts = timestamp()
self.config_loader.start()
self.message_queue.start()
self.frame_selector.start()
self.process_reporter.start()
self.cpu_reporter.start()
self.allocation_reporter.start()
self.block_reporter.start()
self.error_reporter.start()
# execute main_thread_func in main thread on signal
def _signal_handler(signum, frame):
if(self.main_thread_func):
func = self.main_thread_func
self.main_thread_func = None
try:
func()
except Exception:
self.exception()
return True
register_signal(signal.SIGUSR2, _signal_handler)
# destroy agent on exit
def _exit_handler(*arg):
if not self.agent_started or self.agent_destroyed:
return
try:
self.message_queue.flush()
self.destroy()
except Exception:
self.exception()
atexit.register(_exit_handler)
register_signal(signal.SIGQUIT, _exit_handler, ignore_default = False)
register_signal(signal.SIGINT, _exit_handler, ignore_default = False)
register_signal(signal.SIGTERM, _exit_handler, ignore_default = False)
register_signal(signal.SIGHUP, _exit_handler, ignore_default = False)
self.agent_started = True
self.log('Agent started')
def destroy(self):
if not self.agent_started:
self.log('Agent has not been started')
return
if self.agent_destroyed:
return
self.config_loader.destroy()
self.message_queue.destroy()
self.frame_selector.destroy()
self.process_reporter.destroy()
self.cpu_reporter.destroy()
self.allocation_reporter.destroy()
self.block_reporter.destroy()
self.error_reporter.destroy()
self.agent_destroyed = True
self.log('Agent destroyed')
def log_prefix(self):
return '[' + datetime.datetime.now().strftime('%H:%M:%S.%f') + '] StackImpact ' + self.AGENT_VERSION + ':'
def log(self, message):
if self.get_option('debug'):
print(self.log_prefix(), message)
def print_err(self, *args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def error(self, message):
if self.get_option('debug'):
self.print_err(self.log_prefix(), message)
def exception(self):
if self.get_option('debug'):
self.print_err(sys.exc_info()[0])
traceback.print_exc()
def delay(self, timeout, func):
def func_wrapper():
try:
func()
except Exception:
self.exception()
t = threading.Timer(timeout, func_wrapper, ())
t.start()
return t
def schedule(self, timeout, interval, func):
tw = TimerWraper()
def func_wrapper():
start = time.time()
try:
func()
except Exception:
self.exception()
with tw.cancel_lock:
if not tw.canceled:
tw.timer = threading.Timer(abs(interval - (time.time() - start)), func_wrapper, ())
tw.timer.start()
tw.timer = threading.Timer(timeout, func_wrapper, ())
tw.timer.start()
return tw
def run_in_thread(self, func):
def func_wrapper():
try:
func()
except Exception:
self.exception()
t = threading.Thread(target=func_wrapper)
t.start()
return t
def run_in_main_thread(self, func):
if self.main_thread_func:
return False
self.main_thread_func = func
os.kill(os.getpid(), signal.SIGUSR2)
return True
class TimerWraper():
def __init__(self):
self.timer = None
self.cancel_lock = threading.Lock()
self.canceled = False
def cancel(self):
with self.cancel_lock:
self.canceled = True
self.timer.cancel()