in my agent, I have set up an integration with lan...
# ask-ai
n
in my agent, I have set up an integration with langfuse through opentelemetry to log traces. i want to add some resilience so that the agent would still work even when something happens to the langfuse host. how can i make it work so that errors are logged when the trace export fails but the main logic of the agent wouldn't be affected? This is the the code i use to set up the connection:
Copy code
os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = f"{host.rstrip('/')}/api/public/otel"
os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = f"Authorization=Basic {langfuse_auth}"

trace_provider = TracerProvider()
trace_provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
set_tracer_provider(trace_provider)
t
Thanks for reaching out, Karl. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I’m responding to your question about making the OpenTelemetry -> Langfuse export resilient so trace export failures are logged but don’t break your agent’s main logic. You can handle exporter failures by attaching error handling to the span processor. Use a
BatchSpanProcessor
with an error callback or wrap the exporter so export exceptions are caught and logged. Example change to your setup:
Copy code
import logging
import os
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.trace import set_tracer_provider

# envs you already set
os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = f"{host.rstrip('/')}/api/public/otel"
os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = f"Authorization=Basic {langfuse_auth}"

logging.basicConfig(level=logging.WARNING)
logger = logging.getLogger(__name__)

def on_export_error(exc: Exception):
    logger.warning("Trace export to Langfuse failed; continuing without blocking agent", exc_info=exc)

trace_provider = TracerProvider()
exporter = OTLPSpanExporter()

# BatchSpanProcessor accepts an on_error_callback in many OTLP SDKs;
# if your SDK version differs, see the note below about wrapping export.
span_processor = BatchSpanProcessor(exporter, on_error_callback=on_export_error)
trace_provider.add_span_processor(span_processor)
set_tracer_provider(trace_provider)
If your OpenTelemetry SDK version does not support
on_error_callback
, wrap the exporter to catch errors:
Copy code
from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult

class SafeExporter(SpanExporter):
    def __init__(self, inner):
        self._inner = inner

    def export(self, spans):
        try:
            return self._inner.export(spans)
        except Exception as e:
            logger.warning("Trace export failed; continuing agent", exc_info=e)
            return SpanExportResult.FAILURE

    def shutdown(self):
        try:
            return self._inner.shutdown()
        except Exception:
            logger.warning("Exporter shutdown error", exc_info=True)
Then use
SafeExporter(exporter)
when creating the
BatchSpanProcessor
. Notes and suggestions: • BatchSpanProcessor already performs batching and retries; the above ensures exporter exceptions are observed and logged without raising to your app threads. • Keep logging at warning or error level so you can review intermittent Langfuse connectivity issues without flooding logs. • If you want, I can provide a version-specific snippet (which OpenTelemetry Python SDK version are you using?) or help tune retry/backoff settings. Would you like that? Sources: Observability data hooks | LiveKit docs