forked from blackfireio/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprobe.py
More file actions
388 lines (301 loc) · 11 KB
/
Copy pathprobe.py
File metadata and controls
388 lines (301 loc) · 11 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import re
import os
import sys
import time
import atexit
import platform
import traceback
import logging
import base64
import random
from contextlib import contextmanager
from blackfire import profiler, VERSION, agent, generate_config, DEFAULT_CONFIG_FILE
from blackfire.utils import IS_PY3, get_home_dir, ConfigParser, \
urlparse, urljoin, urlencode, get_load_avg, get_logger, quote, \
parse_qsl, Request, urlopen, json_prettify, get_probed_runtime
from blackfire.exceptions import BlackfireApiException
from blackfire import BlackfireConfiguration
log = get_logger(__name__)
# globals
_config = None
_probe = None
_DEFAULT_OMIT_SYS_PATH = True
_DEFAULT_PROFILE_TITLE = 'unnamed profile'
__all__ = [
'get_traces', 'clear_traces', 'is_enabled', 'enable', 'end', 'reset',
'disable', 'run', 'initialize'
]
class Probe(object):
def __init__(self, config):
self._config = config
self._agent_conn = None
self._enabled = False
def is_enabled(self):
return self._enabled
def get_agent_prolog_response(self):
'''Returns the first response of the Agent in prolog dialogue'''
assert self._agent_conn is not None
return self._agent_conn.agent_response
def enable(self):
if self._enabled:
raise BlackfireApiException('Another probe is already profiling')
self._enabled = True
# connect agent
if not self._agent_conn:
try:
self._agent_conn = agent.Connection(
self._config.agent_socket, self._config.agent_timeout
)
self._agent_conn.connect(config=self._config)
except Exception as e:
self._enabled = False
self._agent_conn = None
raise e
self._req_start = time.time()
# pass start options from _config.args, set defaults as necessary
builtins = not bool(int(self._config.args.get('flag_no_builtins', '0')))
profile_cpu = bool(int(self._config.args.get('flag_cpu', '0')))
profile_memory = bool(int(self._config.args.get('flag_memory', '0')))
fn_args_enabled = bool(int(self._config.args.get('flag_fn_args', '0')))
# only enable timespan if this is the last profile of multiple sample profiles.
# we look at 'continue': 'false' from the agent response
profile_timespan = False
timespan_threshold = profiler.MAX_TIMESPAN_THRESHOLD # not probable number
if self._agent_conn.agent_response.status_val_dict.get(
'first_sample'
) == 'true':
profile_timespan = bool(
int(self._config.args.get('flag_timespan', '0'))
)
timespan_threshold = int(
self._config.args.get('timespan_threshold', 10)
)
# timespan_selectors is a dict of set of prefix/equal regex selectors.
timespan_selectors = {'^': set(), '=': set()}
if profile_timespan:
ts_selectors = self._agent_conn.agent_response.args.get(
'Blackfire-Timespan', []
)
for ts_sel in ts_selectors:
if ts_sel[0] not in ['^', '=']:
log.warning(
"Ignoring invalid timespan selector '%s'.", ts_sel
)
continue
timespan_selectors[ts_sel[0]].add(ts_sel[1:])
# instrumented_funcs is a dict of {func_name:[list of argument IDs]}
instrumented_funcs = {}
if fn_args_enabled:
# convert the fn-args string to dict for faster lookups on C side
fn_args = self._agent_conn.agent_response.args.get(
'Blackfire-Fn-Args', []
)
for fn_arg in fn_args:
fn_name, arg_ids_s = fn_arg.split()
fn_name = fn_name.strip()
if fn_name in instrumented_funcs:
log.warning(
"Function '%s' is already instrumented. Ignoring fn-args directive %s.",
fn_name, fn_arg
)
continue
arg_ids = []
for arg_id in arg_ids_s.strip().split(','):
if arg_id.isdigit():
arg_ids.append(int(arg_id))
else:
arg_ids.append(arg_id)
instrumented_funcs[fn_name] = arg_ids
profiler.start(
builtins=builtins,
profile_cpu=profile_cpu,
profile_memory=profile_memory,
profile_timespan=profile_timespan,
instrumented_funcs=instrumented_funcs,
timespan_selectors=timespan_selectors,
timespan_threshold=timespan_threshold,
)
# TODO: 'Blackfire-Error: 103 Samples quota is out'
log.debug(
"profiler started. [instrumented_funcs:%s, timespan_selectors:%s]",
json_prettify(instrumented_funcs),
json_prettify(timespan_selectors),
)
def disable(self):
self._enabled = False
profiler.stop()
def clear_traces(self):
profiler.clear_traces()
def end(self, headers={}, omit_sys_path_dirs=_DEFAULT_OMIT_SYS_PATH):
if not self._agent_conn:
return
log.debug("probe.end() called.")
self.disable()
traces = get_traces(omit_sys_path_dirs=omit_sys_path_dirs)
self.clear_traces()
# write main prolog
profile_title = self._config.args.get(
'profile_title', _DEFAULT_PROFILE_TITLE
)
end_headers = {
'file-format': 'BlackfireProbe',
'Probed-Runtime': get_probed_runtime(),
'Probed-Language': 'python',
'Probed-Os': platform.platform(),
'Probe-version': VERSION,
'Probed-Features': self._config.args_raw,
'Request-Start': self._req_start,
'Request-End': time.time(),
'Profile-Title': profile_title,
}
load_avg = get_load_avg()
if load_avg:
end_headers['Request-Sys-Load-Avg'] = load_avg
end_headers.update(headers)
context_dict = {'script': sys.executable, 'argv[]': sys.argv}
# middlewares populate the Context dict?
if 'Context' in end_headers:
context_dict.update(end_headers['Context'])
end_headers['Context'] = urlencode(context_dict, doseq=True)
profile_data_req = agent.BlackfireRequest(
headers=end_headers, data=traces
)
self._agent_conn.send(profile_data_req.to_bytes())
self._agent_conn.close()
self._agent_conn = None
return traces
def get_traces(self, omit_sys_path_dirs=_DEFAULT_OMIT_SYS_PATH):
return profiler.get_traces(omit_sys_path_dirs=omit_sys_path_dirs)
def get_traces(omit_sys_path_dirs=_DEFAULT_OMIT_SYS_PATH):
return profiler.get_traces(omit_sys_path_dirs=omit_sys_path_dirs)
def clear_traces():
profiler.clear_traces()
# used from testing to set Probe state to a consistent state
def reset():
global _config, _probe
_config = None
_probe = None
def add_marker(label=''):
pass
def generate_subprofile_query():
global _config
if not _config:
raise BlackfireApiException(
'Unable to create a subprofile query as profiling is not enabled.'
)
args_copy = _config.args.copy()
parent_sid = ''
if 'sub_profile' in args_copy:
parent_sid = args_copy['sub_profile'].split(':')[1]
args_copy.pop('aggreg_samples')
s = ''.join(chr(random.randint(0, 255)) for _ in range(7))
if IS_PY3:
s = bytes(s, agent.Protocol.ENCODING)
sid = base64.b64encode(s)
sid = sid.decode("ascii")
sid = sid.rstrip('=')
sid = sid.replace('+', 'A')
sid = sid.replace('/', 'B')
sid = sid[:9]
args_copy['sub_profile'] = '%s:%s' % (parent_sid, sid)
result = "%s&signature=%s&%s" % (
_config.challenge,
_config.signature,
urlencode(args_copy),
)
return result
def initialize(
query=None,
client_id=None,
client_token=None,
agent_socket=None,
agent_timeout=None,
endpoint=None,
log_file=None,
log_level=None,
config_file=DEFAULT_CONFIG_FILE,
_method="manual",
):
global _config, log, _probe
if log_file or log_level:
log = get_logger(__name__, log_file=log_file, log_level=log_level)
log.warning(
"DeprecationWarning: 'LOG_FILE' and 'LOG_LEVEL' params are no longer used from 'probe.initialize' API. "
"Please use 'BLACKFIRE_LOG_FILE'/'BLACKFIRE_LOG_LEVEL' environment variables."
"These settings will be removed in the next version."
)
log.debug("probe.initialize called. [method:'%s']", _method)
_config = generate_config(
query,
client_id,
client_token,
agent_socket,
agent_timeout,
endpoint,
log_file,
log_level,
config_file,
)
log.debug(
"Probe Configuration initialized. [%s]",
json_prettify(_config.__dict__)
)
_probe = Probe(_config)
def is_enabled():
global _probe
if not _probe:
return False
return _probe.is_enabled()
def enable(end_at_exit=False):
global _config, _probe
if not _config:
raise BlackfireApiException(
'No configuration set. initialize should be called first.'
)
log.debug("probe.enable() called.")
_probe.enable()
if end_at_exit: # used for profiling CLI scripts
# patch sys module to get the exit code/stdout/stderr output lengths
from blackfire.hooks.sys.patch import patch
from blackfire.hooks.sys import SysHooks
patch()
def _deinitialize():
headers = {}
headers['Response-Code'] = SysHooks.exit_code
headers['Response-Bytes'
] = SysHooks.stdout_len + SysHooks.stderr_len
try:
end(headers=headers)
except:
# we do not need to return if any error happens inside end()
# but it would be nice to see the traceback
log.warn(traceback.format_exc())
logging.shutdown()
# Note: The functions registered via this module are not called when the
# program is killed by a signal not handled by Python, when a Python fatal
# internal error is detected, or when os._exit() is called.
atexit.register(_deinitialize)
def disable():
global _probe
if not _probe:
return
_probe.disable()
log.debug("probe.disable() called.")
def end(headers={}, omit_sys_path_dirs=_DEFAULT_OMIT_SYS_PATH):
'''
headers: additional headers to send along with the final profile data.
'''
global _probe
if not _probe:
return
log.debug("probe.end() called.")
return _probe.end()
@contextmanager
def run(call_end=True):
enable()
try:
yield
finally:
disable()
if call_end:
end()