|
6 | 6 | ORIGINAL_HANDLER_KEY = "LUMIGO_ORIGINAL_HANDLER" |
7 | 7 |
|
8 | 8 |
|
| 9 | +def parse_handler(): |
| 10 | + try: |
| 11 | + module_name, unit_name = os.environ[ORIGINAL_HANDLER_KEY].rsplit(".", 1) |
| 12 | + except KeyError: |
| 13 | + raise ValueError( |
| 14 | + "Could not find the original handler. Please contact Lumigo for more information." |
| 15 | + ) |
| 16 | + except ValueError: |
| 17 | + raise RuntimeError( |
| 18 | + f"Invalid handler format: Bad handler '{os.environ[ORIGINAL_HANDLER_KEY]}': not enough values to unpack (expected 2, got 1)" |
| 19 | + ) |
| 20 | + importable_name = module_name.replace("/", ".") |
| 21 | + return importable_name, unit_name |
| 22 | + |
| 23 | + |
9 | 24 | @lumigo_tracer() |
10 | 25 | def _handler(*args, **kwargs): |
| 26 | + handler_module = "" |
11 | 27 | try: |
12 | | - module_name, unit_name = os.environ[ORIGINAL_HANDLER_KEY].rsplit(".", 1) |
13 | | - importable_name = module_name.replace("/", ".") |
14 | | - original_handler = getattr(importlib.import_module(importable_name), unit_name) |
| 28 | + handler_module, unit_name = parse_handler() |
| 29 | + original_handler = getattr(importlib.import_module(handler_module), unit_name) |
15 | 30 | except (ImportError, AttributeError): |
16 | 31 | raise ImportError( |
17 | | - "Could not load the original handler. Are you sure that the import is ok?" |
18 | | - ) |
19 | | - except KeyError: |
20 | | - raise ValueError( |
21 | | - "Could not find the original handler. Please follow lumigo's docs: https://docs.lumigo.io/" |
| 32 | + f"Unable to import module '{handler_module}': No module named '{handler_module}'" |
22 | 33 | ) |
23 | 34 | return original_handler(*args, **kwargs) |
| 35 | + |
| 36 | + |
| 37 | +try: |
| 38 | + # import handler during runtime initialization, as usual. |
| 39 | + importlib.import_module(parse_handler()[0]) |
| 40 | +except Exception: |
| 41 | + pass |
0 commit comments