From d7e0e85e2ccbd3e4abd2f195a37b28c74b9a28ac Mon Sep 17 00:00:00 2001 From: Abhiram Date: Tue, 7 Jul 2026 07:01:05 +0000 Subject: [PATCH] feat(traceloop-sdk): add HTTPX instrumentation support Adds Instruments.HTTPCLIENT to enable httpx client tracing via opentelemetry-instrumentation-httpx. This implements feature request from issue #2283. Changes: - Add HTTPCLIENT = 'httpclient' to Instruments enum - Add init_httpclient_instrumentor() function - Add httpx instrumentation init in _initialize_instrument() - Add opentelemetry-instrumentation-httpx as a dependency Closes traceloop/openllmetry#2283 --- packages/traceloop-sdk/pyproject.toml | 1 + .../traceloop-sdk/traceloop/sdk/instruments.py | 1 + .../traceloop/sdk/tracing/tracing.py | 17 +++++++++++++++++ 3 files changed, 19 insertions(+) diff --git a/packages/traceloop-sdk/pyproject.toml b/packages/traceloop-sdk/pyproject.toml index d31795fd5a..675988ce35 100644 --- a/packages/traceloop-sdk/pyproject.toml +++ b/packages/traceloop-sdk/pyproject.toml @@ -42,6 +42,7 @@ dependencies = [ "opentelemetry-instrumentation-llamaindex", "opentelemetry-instrumentation-milvus", "opentelemetry-instrumentation-haystack", + "opentelemetry-instrumentation-httpx>=0.59b0", "opentelemetry-instrumentation-bedrock", "opentelemetry-instrumentation-sagemaker", "opentelemetry-instrumentation-replicate", diff --git a/packages/traceloop-sdk/traceloop/sdk/instruments.py b/packages/traceloop-sdk/traceloop/sdk/instruments.py index d4232a3899..c99c7c21d7 100644 --- a/packages/traceloop-sdk/traceloop/sdk/instruments.py +++ b/packages/traceloop-sdk/traceloop/sdk/instruments.py @@ -13,6 +13,7 @@ class Instruments(Enum): GOOGLE_GENERATIVEAI = "google_generativeai" GROQ = "groq" HAYSTACK = "haystack" + HTTPCLIENT = "httpclient" LANCEDB = "lancedb" LANGCHAIN = "langchain" LITELLM = "litellm" diff --git a/packages/traceloop-sdk/traceloop/sdk/tracing/tracing.py b/packages/traceloop-sdk/traceloop/sdk/tracing/tracing.py index 9db24fe4b6..a4b0854cab 100644 --- a/packages/traceloop-sdk/traceloop/sdk/tracing/tracing.py +++ b/packages/traceloop-sdk/traceloop/sdk/tracing/tracing.py @@ -542,6 +542,9 @@ def init_instrumentations( elif instrument == Instruments.HAYSTACK: if init_haystack_instrumentor(): instrument_set = True + elif instrument == Instruments.HTTPCLIENT: + if init_httpclient_instrumentor(): + instrument_set = True elif instrument == Instruments.LANCEDB: if init_lancedb_instrumentor(): instrument_set = True @@ -783,6 +786,20 @@ def init_haystack_instrumentor(): return False +def init_httpclient_instrumentor(): + try: + if is_package_installed("httpx"): + from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor + + instrumentor = HTTPXClientInstrumentor() + if not instrumentor.is_instrumented_by_opentelemetry: + instrumentor.instrument() + return True + except Exception as e: + logging.error(f"Error initializing HTTPX instrumentor: {e}") + return False + + def init_langchain_instrumentor(use_attributes: bool = True): try: if is_package_installed("langchain") or is_package_installed("langgraph"):