Troubleshooting
This article helps you resolve issues you may meet using Cognite Functions.
Incorrect folder structure when deploying a function as a zip file
Cognite Functions expects handler.py
to be at the root of the zipped folder or at a path relative to the root as pointed to by the function_path
attribute.
The following errors occur when handler.py
isn't found at either of these locations.
Error messages:
When using the API to publish the function, you may see this error message in Cognite Data Fusion (CDF) if the function deployment fails:
No Python module found at location "handler.py" in zip-file.
If you use the Cognite SDK to publish the function, you may see this error message if the function deployment fails:
No file found at location.
This typically occurs when the root folder is zipped and not the folder content. Here's an example of a wrong folder structure:
func/
├── handler.py
└── requirements.txt
If you run the zip command outside the func/
folder, the folder structure becomes wrong.
# pwd: ~/tmp
> zip -r func.zip func/
adding: func/ (stored 0%)
adding: func/handler.py (stored 0%)
adding: func/requirements.txt (stored 0%)
The requirements.txt
must be at the root of the zip-file.
Solution:
cd
into the func
folder before you zip the contents:
# pwd: ~/tmp/func
❯ zip -r func.zip *
adding: handler.py (stored 0%)
adding: requirements.txt (stored 0%)
Failed calls without logs
A function call returns with the status Failed
, and there are no logs to explain why it Failed
.
Cause:
Cognite Functions doesn't support the Python logging
module, and importing it can sometimes hide the stack trace of failed calls.
Solution
Rerun the function after replacing logging statements with print statements. Remove all imports of the logging module.
Multithreading isn't behaving as expected
Avoid using multithreading from within the function as this may break the internal structure of the function such as function status reporting and error detection.
Faulty requirements.txt
A faulty requirements.txt
usually results in error messages from pip. These occur in the error
attribute of the object returned by the function create
operation. If the function deployment fails, the error also occurs in the CDF user interface.
We've experienced cases where the pip installation failed, but no error messages occurred in the pip log.
Solution:
Test the installation locally: pip install -r requirements.txt
For Cognite Functions on Azure, the requirements.txt
-file must be compatible with:
azure-functions
azure-identity
azure-keyvault-keys
azure-storage-blob
cryptography
Incorrectly named requirements.txt
Your function call fails with the following message in the logs.
Error message:
ModuleNotFoundError: No module named ...
Solution:
Make sure the file requirements.txt
is correctly named. If the file name is misspelled, the packages listed in the file will not be installed.
Default version of cognite-sdk
By default, the function is deployed with the latest version of cognite-sdk. If a specific version is desired, it can be specified in the requirements.txt file uploaded with the function source code.
Absolute versus relative imports
If the following message occurs in the function call logs, a relative import has failed.
Error message:
ImportError: attempted relative import with no known parent package
Cause:
The user is attempting to use relative imports within their handler.py.
Solution:
Use absolute imports instead of relative imports to avoid ambiguity about where the package should be imported from. See RealPython: Absolute vs Relative Python Imports.
Scheduled function call fails/times out
The following error message in the function call logs can indicate a problem with the session used to instantiate CogniteClient
:
Error message:
Could not fetch a valid token or a valid API key. Unable to instantiate a CogniteClient. Please verify your credentials.
Solution:
Check CDF to see if the OpenID Connect (OIDC) settings have been updated. This revokes all existing sessions, and all schedules will fail. Recreate the function schedules to solve this problem.