elastic / elasticsearch-py

Official Python client for Elasticsearch
https://ela.st/es-python
Apache License 2.0
35 stars 1.18k forks source link

Fix import when trace is missing from opentelemetry #2694

Closed nicoloboschi closed 2 weeks ago

nicoloboschi commented 2 weeks ago

otel dependency is optional but if I try to use latest elastic client it fails to bootstrap due to

../../../vdp/config_verification/elastic_sink/elastic_config_verification.py:21: in <module>
    from elasticsearch import Elasticsearch
../../../../.venv/lib/python3.11/site-packages/elasticsearch/__init__.py:45: in <module>
    from ._async.client import AsyncElasticsearch as AsyncElasticsearch
../../../../.venv/lib/python3.11/site-packages/elasticsearch/_async/client/__init__.py:38: in <module>
    from ._base import (
../../../../.venv/lib/python3.11/site-packages/elasticsearch/_async/client/_base.py:48: in <module>
    from ..._otel import OpenTelemetry
../../../../.venv/lib/python3.11/site-packages/elasticsearch/_otel.py:25: in <module>
    from opentelemetry import trace
E   ImportError: cannot import name 'trace' from 'opentelemetry' (unknown location)

Fix

While is usually correct to catch ModuleNotFoundError, in case of opentelemetry this is a bit different since the actual "opentelemetry" package doesn't exist at all (we got opentelemetry-api and opentelemetry-sdk) and in that case Python (3.11 in my case) throws a more generic ImportError

cla-checker-service[bot] commented 2 weeks ago

💚 CLA has been signed

github-actions[bot] commented 2 weeks ago

A documentation preview will be available soon.

Request a new doc build by commenting * Rebuild this PR: `run docs-build` * Rebuild this PR and all Elastic docs: `run docs-build rebuild` `run docs-build` is much faster than `run docs-build rebuild`. A `rebuild` should only be needed in rare situations. If your PR continues to fail for an unknown reason, the doc build pipeline may be broken. Elastic employees can check the pipeline status [here](https://buildkite.com/elastic/docs-build).
pquentin commented 2 weeks ago

buildkite test this please

pquentin commented 1 week ago

Thank you for your contribution, it is now released as part of 8.16.0: https://github.com/elastic/elasticsearch-py/releases/tag/v8.16.0