Skip to content

Circular Import Issue Caused by AwsLambdaInstrumentor Manual Instrumentation #2056

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

Open
sam-mosleh opened this issue Nov 12, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@sam-mosleh
Copy link
Contributor

Environment:

  • AWS Lambda runtime: Python 3.11
  • Library version: opentelemetry-instrumentation-aws-lambda==0.42b0

Steps to reproduce

from opentelemetry.instrumentation.aws_lambda import AwsLambdaInstrumentor

AwsLambdaInstrumentor().instrument()

def lambda_handler(event, context):
    return "OK"

What is the expected behavior?
The AwsLambdaInstrumentor should allow manual instrumentation without causing circular imports, ensuring the proper functioning of the project.

What is the actual behavior?
Circular import is triggered

Additional context

{
  "errorMessage": "partially initialized module 'test' has no attribute 'lambda_handler' (most likely due to a circular import)",
  "errorType": "AttributeError",
  "requestId": "",
  "stackTrace": [
    "  File \"/var/lang/lib/python3.11/importlib/init.py\", line 126, in import_module\n    return _bootstrap._gcd_import(name[level:], package, level)\n",
    "  File \"<frozen importlib._bootstrap>\", line 1204, in _gcd_import\n",
    "  File \"<frozen importlib._bootstrap>\", line 1176, in _find_and_load\n",
    "  File \"<frozen importlib._bootstrap>\", line 1147, in _find_and_load_unlocked\n",
    "  File \"<frozen importlib._bootstrap>\", line 690, in _load_unlocked\n",
    "  File \"<frozen importlib._bootstrap_external>\", line 940, in exec_module\n",
    "  File \"<frozen importlib._bootstrap>\", line 241, in _call_with_frames_removed\n",
    "  File \"/var/task/test.py\", line 3, in <module>\n    AwsLambdaInstrumentor().instrument()\n",
    "  File \"/opt/python/opentelemetry/instrumentation/instrumentor.py\", line 108, in instrument\n    result = self._instrument(  # pylint: disable=assignment-from-no-return\n",
    "  File \"/opt/python/opentelemetry/instrumentation/aws_lambda/init.py\", line 462, in _instrument\n    _instrument(\n",
    "  File \"/opt/python/opentelemetry/instrumentation/aws_lambda/init.py\", line 403, in _instrument\n    wrap_function_wrapper(\n",
    "  File \"/opt/python/wrapt/wrappers.py\", line 895, in wrap_function_wrapper\n    return wrap_object(module, name, FunctionWrapper, (wrapper,))\n",
    "  File \"/opt/python/wrapt/wrappers.py\", line 841, in wrap_object\n    (parent, attribute, original) = resolve_path(module, name)\n",
    "  File \"/opt/python/wrapt/wrappers.py\", line 829, in resolve_path\n    original = lookup_attribute(parent, attribute)\n",
    "  File \"/opt/python/wrapt/wrappers.py\", line 827, in lookup_attribute\n    return getattr(parent, attribute)\n"
  ]
}
@Kludex
Copy link
Contributor

Kludex commented Dec 3, 2024

I don't understand. This is the first step.

I see 30k downloads a day on https://pypistats.org/packages/opentelemetry-instrumentation-aws-lambda.

Is it all fake? Or are people not using the Python runtime?

@Minister944
Copy link

Hi
There is a problem with the docs. The line of code that instruments the code should be placed below the function. This is because you cannot instrument code that does not exist yet.
Here’s an example:

from opentelemetry.instrumentation.aws_lambda import AwsLambdaInstrumentor

def lambda_handler(event, context):
    return "OK"

AwsLambdaInstrumentor().instrument()

I wanted to fix this in the guide, but I couldn’t run the tests/build the docs locally.

@xrmx
Copy link
Contributor

xrmx commented Dec 10, 2024

Hi There is a problem with the docs. The line of code that instruments the code should be placed below the function. This is because you cannot instrument code that does not exist yet. Here’s an example:

from opentelemetry.instrumentation.aws_lambda import AwsLambdaInstrumentor

def lambda_handler(event, context):
    return "OK"

AwsLambdaInstrumentor().instrument()

I wanted to fix this in the guide, but I couldn’t run the tests/build the docs locally.

tox -e docs should do it but if you can sign the CLA please open a PR anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants