-
Notifications
You must be signed in to change notification settings - Fork 110
/
environment.py
268 lines (206 loc) · 9.74 KB
/
environment.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
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
# Copyright 2010 New Relic, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This module provides functions to collect information about the operating
system, Python and hosting environment.
"""
import logging
import os
import platform
import sys
import newrelic
from newrelic.common.package_version_utils import get_package_version
from newrelic.common.system_info import (
logical_processor_count,
physical_processor_count,
total_physical_memory,
)
from newrelic.packages.isort import stdlibs as isort_stdlibs
try:
import newrelic.core._thread_utilization
except ImportError:
pass
_logger = logging.getLogger(__name__)
def environment_settings():
"""Returns an array of arrays of environment settings"""
env = []
# Agent information.
env.append(("Agent Version", ".".join(map(str, newrelic.version_info))))
# System information.
env.append(("Arch", platform.machine()))
env.append(("OS", platform.system()))
env.append(("OS version", platform.release()))
env.append(("Total Physical Memory (MB)", total_physical_memory()))
env.append(("Logical Processors", logical_processor_count()))
physical_processor_packages, physical_cores = physical_processor_count()
# Report this attribute only if it has a valid value.
if physical_processor_packages:
env.append(("Physical Processor Packages", physical_processor_packages))
# Report this attribute only if it has a valid value.
if physical_cores:
env.append(("Physical Cores", physical_cores))
# Python information.
env.append(("Python Program Name", sys.argv[0]))
env.append(("Python Executable", sys.executable))
env.append(("Python Home", os.environ.get("PYTHONHOME", "")))
env.append(("Python Path", os.environ.get("PYTHONPATH", "")))
env.append(("Python Prefix", sys.prefix))
env.append(("Python Exec Prefix", sys.exec_prefix))
env.append(("Python Runtime", ".".join(platform.python_version_tuple())))
env.append(("Python Implementation", platform.python_implementation()))
env.append(("Python Version", sys.version))
env.append(("Python Platform", sys.platform))
env.append(("Python Max Unicode", sys.maxunicode))
# Extensions information.
extensions = []
if "newrelic.core._thread_utilization" in sys.modules:
extensions.append("newrelic.core._thread_utilization")
env.append(("Compiled Extensions", ", ".join(extensions)))
# Dispatcher information.
dispatcher = []
# Find the first dispatcher module that's been loaded and report that as the dispatcher.
# If possible, also report the dispatcher's version and any other environment information.
if not dispatcher and "mod_wsgi" in sys.modules:
mod_wsgi = sys.modules["mod_wsgi"]
if hasattr(mod_wsgi, "process_group"):
if mod_wsgi.process_group == "":
dispatcher.append(("Dispatcher", "Apache/mod_wsgi (embedded)"))
else:
dispatcher.append(("Dispatcher", "Apache/mod_wsgi (daemon)"))
env.append(("Apache/mod_wsgi Process Group", mod_wsgi.process_group))
else:
dispatcher.append(("Dispatcher", "Apache/mod_wsgi"))
if hasattr(mod_wsgi, "version"):
dispatcher.append(("Dispatcher Version", str(mod_wsgi.version)))
if hasattr(mod_wsgi, "application_group"):
env.append(("Apache/mod_wsgi Application Group", mod_wsgi.application_group))
if not dispatcher and "uwsgi" in sys.modules:
dispatcher.append(("Dispatcher", "uWSGI"))
uwsgi = sys.modules["uwsgi"]
if hasattr(uwsgi, "version"):
dispatcher.append(("Dispatcher Version", uwsgi.version))
if not dispatcher and "flup.server.fcgi" in sys.modules:
dispatcher.append(("Dispatcher", "flup/fastcgi (threaded)"))
if not dispatcher and "flup.server.fcgi_fork" in sys.modules:
dispatcher.append(("Dispatcher", "flup/fastcgi (prefork)"))
if not dispatcher and "flup.server.scgi" in sys.modules:
dispatcher.append(("Dispatcher", "flup/scgi (threaded)"))
if not dispatcher and "flup.server.scgi_fork" in sys.modules:
dispatcher.append(("Dispatcher", "flup/scgi (prefork)"))
if not dispatcher and "flup.server.ajp" in sys.modules:
dispatcher.append(("Dispatcher", "flup/ajp (threaded)"))
if not dispatcher and "flup.server.ajp_fork" in sys.modules:
dispatcher.append(("Dispatcher", "flup/ajp (forking)"))
if not dispatcher and "flup.server.cgi" in sys.modules:
dispatcher.append(("Dispatcher", "flup/cgi"))
if not dispatcher and "gunicorn" in sys.modules:
if "gunicorn.workers.ggevent" in sys.modules:
dispatcher.append(("Dispatcher", "gunicorn (gevent)"))
elif "gunicorn.workers.geventlet" in sys.modules:
dispatcher.append(("Dispatcher", "gunicorn (eventlet)"))
elif "uvicorn.workers" in sys.modules or "uvicorn_worker" in sys.modules:
dispatcher.append(("Dispatcher", "gunicorn (uvicorn)"))
uvicorn = sys.modules.get("uvicorn")
if hasattr(uvicorn, "__version__"):
dispatcher.append(("Worker Version", uvicorn.__version__))
else:
dispatcher.append(("Dispatcher", "gunicorn"))
gunicorn = sys.modules["gunicorn"]
if hasattr(gunicorn, "__version__"):
dispatcher.append(("Dispatcher Version", gunicorn.__version__))
if not dispatcher and "uvicorn" in sys.modules:
dispatcher.append(("Dispatcher", "uvicorn"))
uvicorn = sys.modules["uvicorn"]
if hasattr(uvicorn, "__version__"):
dispatcher.append(("Dispatcher Version", uvicorn.__version__))
if not dispatcher and "hypercorn" in sys.modules:
dispatcher.append(("Dispatcher", "hypercorn"))
hypercorn = sys.modules["hypercorn"]
if hasattr(hypercorn, "__version__"):
dispatcher.append(("Dispatcher Version", hypercorn.__version__))
else:
try:
dispatcher.append(("Dispatcher Version", get_package_version("hypercorn")))
except Exception:
pass
if not dispatcher and "daphne" in sys.modules:
dispatcher.append(("Dispatcher", "daphne"))
daphne = sys.modules["daphne"]
if hasattr(daphne, "__version__"):
dispatcher.append(("Dispatcher Version", daphne.__version__))
if not dispatcher and "tornado" in sys.modules:
dispatcher.append(("Dispatcher", "tornado"))
tornado = sys.modules["tornado"]
if hasattr(tornado, "version_info"):
dispatcher.append(("Dispatcher Version", str(tornado.version_info)))
env.extend(dispatcher)
return env
def plugins():
# Module information.
stdlib_builtin_module_names = _get_stdlib_builtin_module_names()
# Using any iterable to create a snapshot of sys.modules can occassionally
# fail in a rare case when modules are imported in parallel by different
# threads.
#
# TL;DR: Do NOT use an iterable on the original sys.modules to generate the
# list
for name, module in sys.modules.copy().items():
# Exclude lib.sub_paths as independent modules except for newrelic.hooks.
nr_hook = name.startswith("newrelic.hooks.")
if "." in name and not nr_hook or name.startswith("_"):
continue
# If the module isn't actually loaded (such as failed relative imports
# in Python 2.7), the module will be None and should not be reported.
try:
if not module:
continue
except Exception: # nosec B112
# if the application uses generalimport to manage optional depedencies,
# it's possible that generalimport.MissingOptionalDependency is raised.
# In this case, we should not report the module as it is not actually loaded and
# is not a runtime dependency of the application.
#
continue
# Exclude standard library/built-in modules.
if name in stdlib_builtin_module_names:
continue
# Don't attempt to look up version information for our hooks
version = None
if not nr_hook:
try:
version = get_package_version(name)
except Exception:
pass
# If it has no version it's likely not a real package so don't report it unless
# it's a new relic hook.
if nr_hook or version:
yield [name, version, {}] if version else [name, " ", {}]
def _get_stdlib_builtin_module_names():
builtins = set(sys.builtin_module_names)
# Since sys.stdlib_module_names is not available in versions of python below 3.10,
# use isort's hardcoded stdlibs instead.
python_version = sys.version_info[0:2]
if python_version < (3,):
stdlibs = isort_stdlibs.py27.stdlib
elif (3, 7) <= python_version < (3, 8):
stdlibs = isort_stdlibs.py37.stdlib
elif python_version < (3, 9):
stdlibs = isort_stdlibs.py38.stdlib
elif python_version < (3, 10):
stdlibs = isort_stdlibs.py39.stdlib
elif python_version >= (3, 10):
stdlibs = sys.stdlib_module_names
else:
_logger.warn("Unsupported Python version. Unable to determine stdlibs.")
return builtins
return builtins | stdlibs