-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
constants.py
111 lines (88 loc) · 2.59 KB
/
constants.py
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
import sys
import sysconfig
from blackfire.utils import get_logger
log = get_logger(__name__)
def _get_sys_config_params(*args):
result = []
for arg in args:
v = sysconfig.get_config_var(arg)
if v:
result += [v.strip()]
return " ".join(result)
def _on_except(func=None, return_val=None):
def inner_func(func):
def _wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except:
return return_val
return _wrapper
return inner_func
class BlackfireConstants(object):
'''
This constants are sent back to the Agent in `Constants` headers and they appear
as runtime.constants metric defs./scenarios...etc
'''
@classmethod
def get(cls, val):
fn = getattr(cls, val.lower(), None)
if fn is None:
log.error("Unsupported Blackfire-Const value. [%s]", val)
return None
return fn()
# Constant definitions
@classmethod
@_on_except(return_val="0.0.0")
def python_version(cls):
return "%d.%d.%d" % (
sys.version_info.major, sys.version_info.minor,
sys.version_info.micro
)
@classmethod
@_on_except()
def python_debug_flag(cls):
return bool(sysconfig.get_config_var('Py_DEBUG'))
@classmethod
@_on_except()
def python_pgo_flag(cls):
return '-fprofile-use' in _get_sys_config_params(
'PY_CFLAGS', 'PY_CFLAGS_NODIST'
)
@classmethod
@_on_except()
def django_version(cls):
import django
return django.get_version()
@classmethod
@_on_except()
def flask_version(cls):
import flask
return flask.__version__
@classmethod
@_on_except()
def django_debug_flag(cls):
from django.conf import settings
return settings.DEBUG
@classmethod
@_on_except()
def django_db_conn_max_age(cls):
from django.db import connection
return connection.settings_dict['CONN_MAX_AGE']
@classmethod
@_on_except()
def flask_debug_flag(cls):
from flask import current_app
return current_app.debug
@classmethod
@_on_except()
def is_flask_app(cls):
# current_app will throw an error if not called from a context a request
from flask import current_app
return True
@classmethod
@_on_except()
def is_django_app(cls):
from django.conf import settings
# configure() will throw error if Django app is configured properly
settings.configure()
return True