{"record":{"id":"312d7b06c6fc274c","repo":"redis/redis-py","slug":"opentelemetry-api-is-not-installed-install-it-wit","errorCode":null,"errorMessage":"OpenTelemetry API is not installed. Install it with: pip install opentelemetry-api","messagePattern":"OpenTelemetry API is not installed\\. Install it with: pip install opentelemetry-api","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"redis/observability/metrics.py","lineNumber":84,"sourceCode":"class RedisMetricsCollector:\n    \"\"\"\n    Collects and records OpenTelemetry metrics for Redis operations.\n\n    This class manages all metric instruments and provides methods to record\n    various Redis operations including connection pool events, command execution,\n    and cluster-specific operations.\n\n    Args:\n        meter: OpenTelemetry Meter instance\n        config: OTel configuration object\n    \"\"\"\n\n    METER_NAME = \"redis-py\"\n    METER_VERSION = \"1.0.0\"\n\n    def __init__(self, meter: Meter, config: OTelConfig):\n        if not OTEL_AVAILABLE:\n            raise ImportError(\n                \"OpenTelemetry API is not installed. \"\n                \"Install it with: pip install opentelemetry-api\"\n            )\n\n        self.meter = meter\n        self.config = config\n        self.attr_builder = AttributeBuilder()\n\n        # Initialize enabled metric instruments\n\n        if MetricGroup.RESILIENCY in self.config.metric_groups:\n            self._init_resiliency_metrics()\n\n        if MetricGroup.COMMAND in self.config.metric_groups:\n            self._init_command_metrics()\n\n        if MetricGroup.CONNECTION_BASIC in self.config.metric_groups:\n            self._init_connection_basic_metrics()","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/redis/redis-py/blob/6a6b581b48225afa0b76912d1028c6035baee932/redis/observability/metrics.py#L66-L102","documentation":"Raised as ImportError by RedisMetricsCollector.__init__ (redis/observability/metrics.py:84) when the module-level import `from opentelemetry.metrics import Meter` failed at load time, leaving OTEL_AVAILABLE=False. The collector cannot create any instruments (Counter/Histogram/UpDownCounter) without the opentelemetry-api package, so construction aborts immediately with an actionable install hint.","triggerScenarios":"Instantiating RedisMetricsCollector(meter, config) directly, or calling get_observability_instance().init(OTelConfig(enable_metrics=True)) which builds the provider manager and collector, in an environment where `pip install opentelemetry-api` was never run. Also triggered when the `otel` extra (redis[otel]) was not requested at install time.","commonSituations":"Enabling the observability feature in code but forgetting to add the otel extra to requirements.txt; CI image that lacks the wheel; a downgraded opentelemetry-api that uninstalled itself due to a dependency conflict; production env built from a slim base image that never included opentelemetry-api.","solutions":["Install the dependency: `pip install opentelemetry-api` (or reinstall redis with the extra: `pip install redis[otel]`).","Verify the import resolves in the running interpreter: `python -c \"from opentelemetry.metrics import Meter\"`.","If metrics are optional for your app, guard initialization so a missing dependency disables observability rather than crashing startup.","Pin compatible opentelemetry versions in requirements to avoid a resolver silently removing opentelemetry-api."],"exampleFix":"# before\nfrom redis.observability import get_observability_instance, OTelConfig\notel = get_observability_instance()\notel.init(OTelConfig(enable_metrics=True))  # ImportError if opentelemetry-api missing\n\n# after - install dependency, then optionally guard\npip install opentelemetry-api\n\n# or make observability optional in code\ntry:\n    from redis.observability import get_observability_instance, OTelConfig\n    get_observability_instance().init(OTelConfig(enable_metrics=True))\nexcept ImportError:\n    logging.warning(\"OpenTelemetry not installed; metrics disabled\")","handlingStrategy":"validation","validationCode":"def metrics_available():\n    try:\n        from opentelemetry.metrics import Meter  # noqa: F401\n        return True\n    except ImportError:\n        return False\n\nif metrics_available():\n    from redis.observability import get_observability_instance, OTelConfig\n    get_observability_instance().init(OTelConfig(enable_metrics=True))","typeGuard":null,"tryCatchPattern":"try:\n    from redis.observability import get_observability_instance, OTelConfig\n    get_observability_instance().init(OTelConfig(enable_metrics=True))\nexcept ImportError as e:\n    if 'opentelemetry-api' in str(e):\n        logging.warning('OpenTelemetry API missing; metrics disabled')\n    else:\n        raise","preventionTips":["Install redis with the otel extra (`pip install redis[otel]`) in any environment that enables metrics.","Add a smoke check at startup that imports opentelemetry.metrics before initializing observability.","Make observability opt-in / guarded by try-except so a missing dependency degrades gracefully instead of crashing.","Pin opentelemetry-api in your lockfile to prevent a resolver from removing it."],"tags":["observability","opentelemetry","metrics","import","dependency","configuration"],"backgroundTag":null,"analyzedSha":"6a6b581b48225afa0b76912d1028c6035baee932","analyzedAt":"2026-08-10T12:52:44.840Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}