-
Notifications
You must be signed in to change notification settings - Fork 674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AttributeError: 'NoneType' object has no attribute 'get_current' #2288
Comments
I spent sometime looking into this and found that there was an import error. |
I ran into this on python 3.10.0 and opentelemetry-sdk 1.10.0, upgrading to 1.11.0 seems to have resolved the issue. |
@wbzdssm can you please confirm? |
workaround the issue with open-telemetry/opentelemetry-python#2288
* Use 'go install' in Github Action setup As of Go 1.18, "go get no longer builds or installs packages in module-aware mode." * opentelemetry: pin dependencies for the otel as stated in open-telemetry/opentelemetry-python#2288 * action: force python version 3.9 workaround the issue with open-telemetry/opentelemetry-python#2288 Co-authored-by: Victor Martinez <[email protected]>
* Use 'go install' in Github Action setup As of Go 1.18, "go get no longer builds or installs packages in module-aware mode." * opentelemetry: pin dependencies for the otel as stated in open-telemetry/opentelemetry-python#2288 * action: force python version 3.9 workaround the issue with open-telemetry/opentelemetry-python#2288 Co-authored-by: Victor Martinez <[email protected]> (cherry picked from commit 6413e46) # Conflicts: # libbeat/tests/system/requirements_aix.txt
* Use 'go install' in Github Action setup As of Go 1.18, "go get no longer builds or installs packages in module-aware mode." * opentelemetry: pin dependencies for the otel as stated in open-telemetry/opentelemetry-python#2288 * action: force python version 3.9 workaround the issue with open-telemetry/opentelemetry-python#2288 Co-authored-by: Victor Martinez <[email protected]> (cherry picked from commit 6413e46) # Conflicts: # libbeat/tests/system/requirements_aix.txt Co-authored-by: Andrew Kroh <[email protected]>
* Use 'go install' in Github Action setup As of Go 1.18, "go get no longer builds or installs packages in module-aware mode." * opentelemetry: pin dependencies for the otel as stated in open-telemetry/opentelemetry-python#2288 * action: force python version 3.9 workaround the issue with open-telemetry/opentelemetry-python#2288 Co-authored-by: Victor Martinez <[email protected]>
I am seeing this issue using Python 3.8 and opentelemetry 1.18.0 |
Same with me. Python 3.8 and opentelemetry 1.18.0 |
I was able to track this down to the fact that the |
Could those experiencing this issue in 3.8 and the latest OpenTelemetry provide a detailed description of the steps that you have taken to reproduce this? Looks like @srikanthccv 's suggestion is a bit outdated so providing an updated repro would be helpful in debugging. |
@kwrobert, any chance you are using some application server like gunicorn/uwsgi in your code? And what is the worker class used? @lzchen, one way to (non-)deterministically reproduce this is to use |
I am facing the same issue on Python 3.10, opentelemetry-sdk==1.20.0 on a python wsgi service. What to set as the value for OTEL_PYTHON_CONTEXT env var ? |
Interesting. Since we already assume the default context is contextvars_context in the api and this is the value we attempt to load, should we simply load the default if there is any exception thrown, since we do rely on the context existing downstream. @ocelotl @aabmass @srikanthccv wdyt |
I think this will be an undesirable thing to do if the given context provider has a different implementation and can't be substituted by default? From what I understood, the |
@srikanthccv there is something weird here, this is our current code: default_context = "contextvars_context"
configured_context = environ.get(
OTEL_PYTHON_CONTEXT, default_context
) # type: str
try:
_RUNTIME_CONTEXT = next( # type: ignore
iter( # type: ignore
entry_points( # type: ignore
group="opentelemetry_context",
name=configured_context,
)
)
).load()()
except Exception: # pylint: disable=broad-except
logger.exception(
"Failed to load context: %s", configured_context
)
return func(*args, **kwargs) # type: ignore[misc]
return typing.cast(_F, wrapper) # type: ignore[misc] It looks to me that for this error to happen, BTW, for anyone who can reproduce this issue, please paste the complete error output, not just the traceback for the |
Hello there, I'm also having the same issue when packaging $ dist/main/main.exe
Python Graph Tutorial
Failed to load context: contextvars_context
Traceback (most recent call last):
File "opentelemetry\context\__init__.py", line 60, in wrapper
StopIteration
Traceback (most recent call last):
File "main.py", line 112, in <module>
asyncio.run(main())
File "asyncio\runners.py", line 190, in run
File "asyncio\runners.py", line 118, in run
File "asyncio\base_events.py", line 653, in run_until_complete
File "main.py", line 20, in main
await greet_user(graph)
File "main.py", line 58, in greet_user
user = await graph.get_user()
^^^^^^^^^^^^^^^^^^^^^^
File "graph.py", line 52, in get_user
user = await self.user_client.me.get(request_configuration=request_config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "msgraph\generated\users\item\user_item_request_builder.py", line 157, in get
File "kiota_http\httpx_request_adapter.py", line 167, in send_async
File "kiota_http\httpx_request_adapter.py", line 494, in get_http_response_message
File "kiota_http\httpx_request_adapter.py", line 139, in _start_local_tracing_span
File "opentelemetry\trace\propagation\__init__.py", line 34, in set_span_in_context
File "opentelemetry\context\__init__.py", line 122, in set_value
File "opentelemetry\context\__init__.py", line 73, in wrapper
File "opentelemetry\context\__init__.py", line 137, in get_current
AttributeError: 'NoneType' object has no attribute 'get_current'
[28728] Failed to execute script 'main' due to unhandled exception! In pure python (eg. $ python main.py
Python Graph Tutorial
To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code ABCD1234 to authenticate.
Hello, Francois XXX
Email: [email protected] So what options do I have? Thanks for the help! |
@ocelotl Sorry to bug you directly but I would really appreciate some insight on how to solve this bug as I cannot package my application and have not found any workaround 😬 |
@eeintech sorry for the late reply, investigating... |
@ocelotl I tried it and running it pure python (no packaging) I now get the same error, here's the full trace: Windows
sys.version_info(major=3, minor=11, micro=6, releaselevel='final', serial=0)
EntryPoints((EntryPoint(name='contextvars_context', value='opentelemetry.context.contextvars_context:ContextVarsRuntimeContext', group='opentelemetry_context'),))
Failed to load context: contextvars_context
Traceback (most recent call last):
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\.env\Lib\site-packages\opentelemetry\context\__init__.py", line 76, in wrapper
raise Exception(the_entry_points)
Exception: EntryPoints((EntryPoint(name='contextvars_context', value='opentelemetry.context.contextvars_context:ContextVarsRuntimeContext', group='opentelemetry_context'),))
Traceback (most recent call last):
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\main.py", line 112, in <module>
asyncio.run(main())
File "C:\Python311\Lib\asyncio\runners.py", line 190, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\asyncio\runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\main.py", line 20, in main
await greet_user(graph)
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\main.py", line 58, in greet_user
user = await graph.get_user()
^^^^^^^^^^^^^^^^^^^^^^
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\graph.py", line 52, in get_user
user = await self.user_client.me.get(request_configuration=request_config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\.env\Lib\site-packages\msgraph\generated\users\item\user_item_request_builder.py", line 157, in get
return await self.request_adapter.send_async(request_info, User, error_mapping)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\.env\Lib\site-packages\kiota_http\httpx_request_adapter.py", line 178, in send_async
response = await self.get_http_response_message(request_info, parent_span)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\.env\Lib\site-packages\kiota_http\httpx_request_adapter.py", line 513, in get_http_response_message
_get_http_resp_span = self._start_local_tracing_span(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\.env\Lib\site-packages\kiota_http\httpx_request_adapter.py", line 150, in _start_local_tracing_span
_context = trace.set_span_in_context(parent_span)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\.env\Lib\site-packages\opentelemetry\trace\propagation\__init__.py", line 34, in set_span_in_context
ctx = set_value(_SPAN_KEY, span, context=context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\.env\Lib\site-packages\opentelemetry\context\__init__.py", line 140, in set_value
context = get_current()
^^^^^^^^^^^^^
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\.env\Lib\site-packages\opentelemetry\context\__init__.py", line 91, in wrapper
return func(*args, **kwargs) # type: ignore[misc]
^^^^^^^^^^^^^^^^^^^^^
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\.env\Lib\site-packages\opentelemetry\context\__init__.py", line 155, in get_current
return _RUNTIME_CONTEXT.get_current() # type:ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'get_current' EDIT: This is the traceback when packaging the app. Windows
sys.version_info(major=3, minor=11, micro=6, releaselevel='final', serial=0)
EntryPoints(())
Failed to load context: contextvars_context
Traceback (most recent call last):
File "opentelemetry\context\__init__.py", line 76, in wrapper
Exception: EntryPoints(())
Traceback (most recent call last):
File "main.py", line 112, in <module>
asyncio.run(main())
File "asyncio\runners.py", line 190, in run
File "asyncio\runners.py", line 118, in run
File "asyncio\base_events.py", line 653, in run_until_complete
File "main.py", line 20, in main
await greet_user(graph)
File "main.py", line 58, in greet_user
user = await graph.get_user()
^^^^^^^^^^^^^^^^^^^^^^
File "graph.py", line 52, in get_user
user = await self.user_client.me.get(request_configuration=request_config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "msgraph\generated\users\item\user_item_request_builder.py", line 157, in get
File "kiota_http\httpx_request_adapter.py", line 178, in send_async
File "kiota_http\httpx_request_adapter.py", line 513, in get_http_response_message
File "kiota_http\httpx_request_adapter.py", line 150, in _start_local_tracing_span
File "opentelemetry\trace\propagation\__init__.py", line 34, in set_span_in_context
File "opentelemetry\context\__init__.py", line 140, in set_value
File "opentelemetry\context\__init__.py", line 91, in wrapper
File "opentelemetry\context\__init__.py", line 155, in get_current
AttributeError: 'NoneType' object has no attribute 'get_current'
[21844] Failed to execute script 'main' due to unhandled exception! |
This is the problem:
For some reason, in your packaging environment there is no Notice how this is different from the pure python:
With my changes both should fail because I am intentionally raising an error, but I see that the problem is that the packaging environment does not have any entry points loaded. |
Ha yes I was aware of the missing entry point but have no idea how to solve it... Again with raw python my application works but not after being packaged. |
I wonder if this is only happening in Windows. @wbzdssm Can you let us know in which OS did this error happen to you? |
Also @eeintech can you give us more details about what are you doing when packaging? |
I use Git Bash on Windows 11. My main script Running with I use PyInstaller 6.4.0 (https://pyinstaller.org/en/stable/) as follow: $ pyinstaller main.py --clean --onefile
480 INFO: PyInstaller: 6.4.0, contrib hooks: 2024.2
480 INFO: Python: 3.11.6
542 INFO: Platform: Windows-10-10.0.22631-SP0
[ ... ]
130831 INFO: Building EXE from EXE-00.toc completed successfully. Then once pyinstaller has finished the packaging, I run it as follow: $ dist/main.exe
Python Graph Tutorial
Windows
sys.version_info(major=3, minor=11, micro=6, releaselevel='final', serial=0)
EntryPoints(())
Failed to load context: contextvars_context
Traceback (most recent call last):
File "opentelemetry\context\__init__.py", line 76, in wrapper
Exception: EntryPoints(())
Traceback (most recent call last):
File "main.py", line 112, in <module>
asyncio.run(main())
File "asyncio\runners.py", line 190, in run
File "asyncio\runners.py", line 118, in run
File "asyncio\base_events.py", line 653, in run_until_complete
File "main.py", line 20, in main
await greet_user(graph)
File "main.py", line 58, in greet_user
user = await graph.get_user()
^^^^^^^^^^^^^^^^^^^^^^
File "graph.py", line 52, in get_user
user = await self.user_client.me.get(request_configuration=request_config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "msgraph\generated\users\item\user_item_request_builder.py", line 157, in get
File "kiota_http\httpx_request_adapter.py", line 178, in send_async
File "kiota_http\httpx_request_adapter.py", line 513, in get_http_response_message
File "kiota_http\httpx_request_adapter.py", line 150, in _start_local_tracing_span
File "opentelemetry\trace\propagation\__init__.py", line 34, in set_span_in_context
File "opentelemetry\context\__init__.py", line 140, in set_value
File "opentelemetry\context\__init__.py", line 91, in wrapper
File "opentelemetry\context\__init__.py", line 155, in get_current
AttributeError: 'NoneType' object has no attribute 'get_current'
[14624] Failed to execute script 'main' due to unhandled exception! while running raw python gives: $ python main.py
Python Graph Tutorial
Windows
sys.version_info(major=3, minor=11, micro=6, releaselevel='final', serial=0)
EntryPoints((EntryPoint(name='contextvars_context', value='opentelemetry.context.contextvars_context:ContextVarsRuntimeContext', group='opentelemetry_context'),))
Failed to load context: contextvars_context
Traceback (most recent call last):
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\.env\Lib\site-packages\opentelemetry\context\__init__.py", line 76, in wrapper
raise Exception(the_entry_points)
Exception: EntryPoints((EntryPoint(name='contextvars_context', value='opentelemetry.context.contextvars_context:ContextVarsRuntimeContext', group='opentelemetry_context'),))
Traceback (most recent call last):
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\main.py", line 112, in <module>
asyncio.run(main())
File "C:\Python311\Lib\asyncio\runners.py", line 190, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\asyncio\runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\main.py", line 20, in main
await greet_user(graph)
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\main.py", line 58, in greet_user
user = await graph.get_user()
^^^^^^^^^^^^^^^^^^^^^^
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\graph.py", line 52, in get_user
user = await self.user_client.me.get(request_configuration=request_config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\.env\Lib\site-packages\msgraph\generated\users\item\user_item_request_builder.py", line 157, in get
return await self.request_adapter.send_async(request_info, User, error_mapping)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\.env\Lib\site-packages\kiota_http\httpx_request_adapter.py", line 178, in send_async
response = await self.get_http_response_message(request_info, parent_span)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\.env\Lib\site-packages\kiota_http\httpx_request_adapter.py", line 513, in get_http_response_message
_get_http_resp_span = self._start_local_tracing_span(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\.env\Lib\site-packages\kiota_http\httpx_request_adapter.py", line 150, in _start_local_tracing_span
_context = trace.set_span_in_context(parent_span)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\.env\Lib\site-packages\opentelemetry\trace\propagation\__init__.py", line 34, in set_span_in_context
ctx = set_value(_SPAN_KEY, span, context=context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\.env\Lib\site-packages\opentelemetry\context\__init__.py", line 140, in set_value
context = get_current()
^^^^^^^^^^^^^
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\.env\Lib\site-packages\opentelemetry\context\__init__.py", line 91, in wrapper
return func(*args, **kwargs) # type: ignore[misc]
^^^^^^^^^^^^^^^^^^^^^
File "C:\development\tests\msgraph-training-python\user-auth\graphtutorial\.env\Lib\site-packages\opentelemetry\context\__init__.py", line 155, in get_current
return _RUNTIME_CONTEXT.get_current() # type:ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'get_current'
|
I'm guessing that PyInstaller is the issue, does it support entry points from dists you install? If this continue to be an issue, we should consider using the contextvars implementation with a direct import instead of loading it as an entrypoint in the default case (when The |
I am encountering the same issue. My environment is Python 3.11.0 and opentelemetry-sdk 1.23.0. When packaging my application using PyInstaller on Windows, I am getting the same issue: |
Another thing I tried is packaging calling Pyinstaller on Powershell but I still had the same error... Now my "working patch" is as follow:
def _load_runtime_context(func: _F) -> _F:
"""A decorator used to initialize the global RuntimeContext
Returns:
A wrapper of the decorated method.
"""
@wraps(func) # type: ignore[misc]
def wrapper( # type: ignore[misc]
*args: typing.Tuple[typing.Any, typing.Any],
**kwargs: typing.Dict[typing.Any, typing.Any],
) -> typing.Optional[typing.Any]:
global _RUNTIME_CONTEXT # pylint: disable=global-statement
return
def set_value(
key: str, value: "object", context: typing.Optional[Context] = None
) -> Context:
"""To record the local state of a cross-cutting concern, the
RuntimeContext API provides a function which takes a context, a
key, and a value as input, and returns an updated context
which contains the new value.
Args:
key: The key of the entry to set.
value: The value of the entry to set.
context: The context to copy, if None, the current context is used.
Returns:
A new `Context` containing the value set.
"""
# if context is None:
# context = get_current()
# new_values = context.copy()
# new_values[key] = value
new_values = {}
return Context(new_values) It works fine in raw python and also works after packaging using Pyinstaller. |
FYI, this PR in PyInstaller addresses this issue: pyinstaller/pyinstaller-hooks-contrib#725 |
Seems to be a Pyinstaller issue, closing. Please reopen if this issue persists after @uellue fix has been merged in Pyinstaller. |
Hi guys, Traceback (most recent call last): Could anyone help me if possible? Thank you! |
@Aybfr If you're using Pyinstaller v6.6 it won't work yet, the PR will most likely make it to the next version. |
@Aybfr Hi! Can you try this for your error? # pyinstaller options
# ...
f"--copy-metadata=opentelemetry_sdk", |
FYI I believe that with |
Describe your environment runtime: python3.6.1; sdk version: 1.7.1
Steps to reproduce
Describe exactly how to reproduce the error. Include a code sample if applicable.
What is the expected behavior?
What did you expect to see?
I use python3.6.1 runtime; But i have a problem,
my code is:
What is the actual behavior?
What did you see instead?
Additional context
Add any other context about the problem here.
https://opentelemetry-python.readthedocs.io/en/stable/getting-started.html
The text was updated successfully, but these errors were encountered: