opentelemetry-python-contrib icon indicating copy to clipboard operation
opentelemetry-python-contrib copied to clipboard

opentelemetry-instrumentation-aws-lambda: code does not handle lack of expected environment variables

Open Kyle-sandeman-mrdfood opened this issue 1 year ago • 1 comments

Describe your environment

OS: Docker (python3.12) Python version: (e.g., Python 3.12.1) Package version: (e.g., 0.48.0)

What happened?

Please note that my application is NOT a Lambda or related in any way. I only have this package because it is a default in the bootstrap.

Instrumenting of aws-lambda failed
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/opentelemetry/instrumentation/auto_instrumentation/_load.py", line 87, in _load_instrumentors
    distro.load_instrumentor(entry_point, skip_dep_check=True)
  File "/usr/local/lib/python3.12/site-packages/opentelemetry/instrumentation/distro.py", line 64, in load_instrumentor
    instrumentor().instrument(**kwargs)
  File "/usr/local/lib/python3.12/site-packages/opentelemetry/instrumentation/instrumentor.py", line 114, in instrument
    result = self._instrument(  # pylint: disable=assignment-from-no-return
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/opentelemetry/instrumentation/aws_lambda/__init__.py", line 429, in _instrument
    ) = lambda_handler.rsplit(".", 1)
        ^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'rsplit'
Failed to auto initialize opentelemetry
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py", line 39, in initialize
    _load_instrumentors(distro)
  File "/usr/local/lib/python3.12/site-packages/opentelemetry/instrumentation/auto_instrumentation/_load.py", line 91, in _load_instrumentors
    raise exc
  File "/usr/local/lib/python3.12/site-packages/opentelemetry/instrumentation/auto_instrumentation/_load.py", line 87, in _load_instrumentors
    distro.load_instrumentor(entry_point, skip_dep_check=True)
  File "/usr/local/lib/python3.12/site-packages/opentelemetry/instrumentation/distro.py", line 64, in load_instrumentor
    instrumentor().instrument(**kwargs)
  File "/usr/local/lib/python3.12/site-packages/opentelemetry/instrumentation/instrumentor.py", line 114, in instrument
    result = self._instrument(  # pylint: disable=assignment-from-no-return
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/opentelemetry/instrumentation/aws_lambda/__init__.py", line 429, in _instrument
    ) = lambda_handler.rsplit(".", 1)
        ^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'rsplit'

Steps to Reproduce

pip install opentelemetry-distro opentelemetry-exporter-otlp-proto-grpc opentelemetry-propagator-jaeger opentelemetry-instrumentation-aws-lambda

One could instead run opentelemetry-bootstrap -a install, I presume.

opentelemetry-instrument python -c "print('test')"

Expected Result

test

Actual Result

<error message from description>
test

Additional context

While this does not affect the application itself, it may affect whether tracing gets set up properly.

Would you like to implement a fix?

None

Kyle-sandeman-mrdfood avatar Jul 26 '24 12:07 Kyle-sandeman-mrdfood

I'd like to work on this and submit a PR later

shijiadong2022 avatar Jul 28 '24 00:07 shijiadong2022

I was seeing the same behaviour even without explicitly installing the opentelemetry-instrumentation-aws-lambda package via pip

It got installed after I ran opentelemetry-bootstrap -a install.

However as I don't have any AWS Lambda dependencies (like Kyle) I was able to remove it using pip uninstall opentelemetry-instrumentation-aws-lambda after the bootstrap step and things worked fine.

It's certainly not the solution, but works as a temporary workaround at least for me.

maeddes avatar Jul 29 '24 10:07 maeddes

I think in general you should just not install the aws-lambda instrumentation, or if you need to use the bootstrap command, simply add aws-lambda to OTEL_PYTHON_DISABLED_INSTRUMENTATIONS environment variable:

https://opentelemetry.io/docs/zero-code/python/configuration/#disabling-specific-instrumentations

Here is the link to the entry point where you can see what name to add to that env var: https://github.com/open-telemetry/opentelemetry-python-contrib/blob/dbfa6818bf856b148824c2ac07b73023a5c1dfaf/instrumentation/opentelemetry-instrumentation-aws-lambda/pyproject.toml#L37

mlorenzana avatar Jul 30 '24 14:07 mlorenzana

Preface: I should've been more clear in the issue that I ran into the problem by using the bootstrap

@mlorenzana While that suffices as a workaround, I don't think it is the right approach.
The AWS lamdba users should opt-in, as opposed to requiring everyone else to opt-out. (I say this as someone that uses AWS Lambda, although unrelated to this issue.)

Additionally, say another default instrumentation gets added for e.g. Google Cloud functions or such, should everyone using opentelemetry be required to add that to the disabled instrumentation list?

Kyle-sandeman-mrdfood avatar Jul 30 '24 15:07 Kyle-sandeman-mrdfood

@Kyle-sandeman-mrdfood I'll bring this discussion to the next SIG so we can vote to remove the aws-lambda from the defaults. Meanwhile, could you please take a look at the PR that will address part of the issue #2750 ?

emdneto avatar Jul 30 '24 17:07 emdneto

@emdneto the PR is merged, but indeed the code is more defensive 👍 Thanks @lzchen!

Kyle-sandeman-mrdfood avatar Jul 31 '24 14:07 Kyle-sandeman-mrdfood