WIPACrepo / wipac-telemetry

WIPAC Telemetry: Monitoring/Tracing Applications, Supporting Infrastructures, and Services
MIT License
0 stars 0 forks source link

import from repl #38

Closed dsschult closed 3 years ago

dsschult commented 3 years ago

For any project with telemetry, it is an error to import anything from the repl:

RuntimeError: WIPAC Telemetry service started up before '__main__' was set. Do you have imports in your package's base '__init__.py'? If so, remove them; one of these likely prematurely called this library before '__main__.py' was executed.

dsschult commented 3 years ago

Full trace:

>>> from rest_tools.client import Client
[wipac-telemetry-setup] Setting Tracer Provider...
Traceback (most recent call last):
  File "/home/dschultz/Documents/github/rest-tools/env/lib/python3.9/site-packages/wipac_telemetry/tracing_tools/__init__.py", line 67, in get_service_name
    main_mod_abspath = os.path.abspath(sys.modules["__main__"].__file__)
AttributeError: module '__main__' has no attribute '__file__'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/dschultz/Documents/github/rest-tools/rest_tools/client/__init__.py", line 1, in <module>
    from .client import OpenIDRestClient, RestClient
  File "/home/dschultz/Documents/github/rest-tools/rest_tools/client/client.py", line 19, in <module>
    import wipac_telemetry.tracing_tools as wtt
  File "/home/dschultz/Documents/github/rest-tools/env/lib/python3.9/site-packages/wipac_telemetry/__init__.py", line 3, in <module>
    from . import tracing_tools  # noqa
  File "/home/dschultz/Documents/github/rest-tools/env/lib/python3.9/site-packages/wipac_telemetry/tracing_tools/__init__.py", line 107, in <module>
    TracerProvider(resource=Resource.create({SERVICE_NAME: get_service_name()}))
  File "/home/dschultz/Documents/github/rest-tools/env/lib/python3.9/site-packages/wipac_telemetry/tracing_tools/__init__.py", line 69, in get_service_name
    raise RuntimeError(
RuntimeError: WIPAC Telemetry service started up before '__main__' was set. Do you have imports in your package's base '__init__.py'? If so, remove them; one of these likely prematurely called this library before '__main__.py' was executed.
ric-evans commented 3 years ago

@dsschult what's your use case?

ric-evans commented 3 years ago

is this ipython?

dsschult commented 3 years ago

ipython or just regular python. I was testing imports and how things like __package__ looked, and noticed the error.

ric-evans commented 3 years ago

ipython:

In [1]: from rest_tools.client import RestClient                                                                                             
[wipac-telemetry-setup] Setting Tracer Provider...
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~/.local/lib/python3.8/site-packages/wipac_telemetry/tracing_tools/__init__.py in get_service_name()
     66     try:
---> 67         main_mod_abspath = os.path.abspath(sys.modules["__main__"].__file__)
     68     except AttributeError as e:

AttributeError: module '__main__' has no attribute '__file__'

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
<ipython-input-1-6355ed8ba6ba> in <module>
----> 1 from rest_tools.client import RestClient

~/.local/lib/python3.8/site-packages/rest_tools/client/__init__.py in <module>
----> 1 from .client import OpenIDRestClient, RestClient
      2 from .session import AsyncSession, Session

~/.local/lib/python3.8/site-packages/rest_tools/client/client.py in <module>
     17 import jwt
     18 import requests
---> 19 import wipac_telemetry.tracing_tools as wtt
     20 
     21 from ..server import OpenIDAuth

~/.local/lib/python3.8/site-packages/wipac_telemetry/__init__.py in <module>
      1 """Top-Level Init."""
      2 
----> 3 from . import tracing_tools  # noqa
      4 
      5 __all__ = ["tracing_tools"]

~/.local/lib/python3.8/site-packages/wipac_telemetry/tracing_tools/__init__.py in <module>
    105 _pseudo_log("Setting Tracer Provider...")
    106 set_tracer_provider(
--> 107     TracerProvider(resource=Resource.create({SERVICE_NAME: get_service_name()}))
    108 )
    109 

~/.local/lib/python3.8/site-packages/wipac_telemetry/tracing_tools/__init__.py in get_service_name()
     67         main_mod_abspath = os.path.abspath(sys.modules["__main__"].__file__)
     68     except AttributeError as e:
---> 69         raise RuntimeError(
     70             "WIPAC Telemetry service started up before '__main__' was set. "
     71             "Do you have imports in your package's base '__init__.py'? "

RuntimeError: WIPAC Telemetry service started up before '__main__' was set. Do you have imports in your package's base '__init__.py'? If so, remove them; one of these likely prematurely called this library before '__main__.py' was executed.
ric-evans commented 3 years ago

The simplest solution is to check if OTEL_EXPORTER_OTLP_ENDPOINT is set before setting up the service name, since it probably wouldn't be set in REPLs. REPLs don't have a file