diff --git a/pyproject.toml b/pyproject.toml index a93e5df5..bbae731b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,6 +13,7 @@ dependencies = [ "httpx>=0.28.1", "Jinja2>=3.1.4", "openai>=1.61.1", + "opentelemetry-sdk>=1.38.0", "pillow>=11.0.0", "py-machineid>=0.7.0", "pydantic-settings>=2.9.1", @@ -223,7 +224,6 @@ bedrock = [ otel = [ "prometheus-fastapi-instrumentator>=7.1.0", "opentelemetry-api>=1.38.0", - "opentelemetry-sdk>=1.38.0", "opentelemetry-instrumentation-fastapi>=0.59b0", "opentelemetry-exporter-otlp-proto-http>=1.38.0", "opentelemetry-instrumentation-httpx>=0.59b0", diff --git a/src/askui/telemetry/otel.py b/src/askui/telemetry/otel.py index 563df252..e5ad09fc 100644 --- a/src/askui/telemetry/otel.py +++ b/src/askui/telemetry/otel.py @@ -1,9 +1,5 @@ from fastapi import FastAPI from opentelemetry import trace -from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter -from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor -from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor -from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor from opentelemetry.sdk.resources import Resource from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor @@ -51,6 +47,22 @@ def setup_opentelemetry_tracing(app: FastAPI, settings: OtelSettings) -> None: None """ + try: + from opentelemetry.exporter.otlp.proto.http.trace_exporter import ( # type: ignore[import-not-found] + OTLPSpanExporter, + ) + from opentelemetry.instrumentation.fastapi import ( # type: ignore[import-not-found] + FastAPIInstrumentor, + ) + from opentelemetry.instrumentation.httpx import ( # type: ignore[import-not-found] + HTTPXClientInstrumentor, + ) + from opentelemetry.instrumentation.sqlalchemy import ( # type: ignore[import-not-found] + SQLAlchemyInstrumentor, + ) + except ImportError: + return + resource = Resource.create( { "service.name": settings.service_name,