Skip to content

Commit 1b0a7e8

Browse files
pvitalGSVarsha
authored andcommitted
refactor: Instana module init.
Signed-off-by: Paulo Vital <[email protected]>
1 parent e9820b9 commit 1b0a7e8

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

src/instana/__init__.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"""
1212

1313
import importlib
14+
import importlib.util
1415
import os
1516
import sys
1617
from typing import Tuple
@@ -70,7 +71,7 @@ def load(_: object) -> None:
7071
def apply_gevent_monkey_patch() -> None:
7172
from gevent import monkey
7273

73-
if os.environ.get("INSTANA_GEVENT_MONKEY_OPTIONS"):
74+
if provided_options := os.environ.get("INSTANA_GEVENT_MONKEY_OPTIONS"):
7475

7576
def short_key(k: str) -> str:
7677
return k[3:] if k.startswith("no-") else k
@@ -81,12 +82,8 @@ def key_to_bool(k: str) -> bool:
8182
import inspect
8283

8384
all_accepted_patch_all_args = inspect.getfullargspec(monkey.patch_all)[0]
84-
provided_options = (
85-
os.environ.get("INSTANA_GEVENT_MONKEY_OPTIONS")
86-
.replace(" ", "")
87-
.replace("--", "")
88-
.split(",")
89-
)
85+
provided_options.replace(" ", "").replace("--", "").split(",")
86+
9087
provided_options = [
9188
k for k in provided_options if short_key(k) in all_accepted_patch_all_args
9289
]
@@ -115,9 +112,7 @@ def get_aws_lambda_handler() -> Tuple[str, str]:
115112
handler_function = "lambda_handler"
116113

117114
try:
118-
handler = os.environ.get("LAMBDA_HANDLER", False)
119-
120-
if handler:
115+
if handler := os.environ.get("LAMBDA_HANDLER", None):
121116
parts = handler.split(".")
122117
handler_function = parts.pop().strip()
123118
handler_module = ".".join(parts).strip()
@@ -159,21 +154,17 @@ def boot_agent() -> None:
159154

160155
import instana.singletons # noqa: F401
161156

162-
# Instrumentation
157+
# Import & initialize instrumentation
163158
if "INSTANA_DISABLE_AUTO_INSTR" not in os.environ:
164-
# TODO: remove the following entries as the migration of the
165-
# instrumentation codes are finalised.
166-
167-
# Import & initialize instrumentation
168159
from instana.instrumentation import (
160+
aio_pika, # noqa: F401
169161
aioamqp, # noqa: F401
170162
asyncio, # noqa: F401
171163
cassandra, # noqa: F401
172164
celery, # noqa: F401
173165
couchbase, # noqa: F401
174166
fastapi, # noqa: F401
175167
flask, # noqa: F401
176-
# gevent_inst, # noqa: F401
177168
grpcio, # noqa: F401
178169
httpx, # noqa: F401
179170
logging, # noqa: F401
@@ -186,11 +177,10 @@ def boot_agent() -> None:
186177
pyramid, # noqa: F401
187178
redis, # noqa: F401
188179
sanic, # noqa: F401
180+
spyne, # noqa: F401
189181
sqlalchemy, # noqa: F401
190182
starlette, # noqa: F401
191183
urllib3, # noqa: F401
192-
spyne, # noqa: F401
193-
aio_pika, # noqa: F401
194184
)
195185
from instana.instrumentation.aiohttp import (
196186
client as aiohttp_client, # noqa: F401
@@ -218,13 +208,23 @@ def boot_agent() -> None:
218208
server as tornado_server, # noqa: F401
219209
)
220210

211+
# from instana.instrumentation import gevent_inst # noqa: F401
212+
221213
# Hooks
222214
from instana.hooks import (
223215
hook_gunicorn, # noqa: F401
224216
hook_uwsgi, # noqa: F401
225217
)
226218

227219

220+
def _start_profiler() -> None:
221+
"""Start the Instana Auto Profile."""
222+
from instana.singletons import get_profiler
223+
224+
if profiler := get_profiler():
225+
profiler.start()
226+
227+
228228
if "INSTANA_DISABLE" not in os.environ:
229229
# There are cases when sys.argv may not be defined at load time. Seems to happen in embedded Python,
230230
# and some Pipenv installs. If this is the case, it's best effort.
@@ -246,12 +246,9 @@ def boot_agent() -> None:
246246
and importlib.util.find_spec("gevent")
247247
):
248248
apply_gevent_monkey_patch()
249+
249250
# AutoProfile
250251
if "INSTANA_AUTOPROFILE" in os.environ:
251-
from instana.singletons import get_profiler
252-
253-
profiler = get_profiler()
254-
if profiler:
255-
profiler.start()
252+
_start_profiler()
256253

257254
boot_agent()

0 commit comments

Comments
 (0)