A programming framework for agentic AI
add env var to disable runtime tracing (#6681)
Eitan Yarmushc5b893d3f814
diff --git a/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tracing.ipynb b/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tracing.ipynbindex 682415c10..40fcc1a7d 100644--- a/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tracing.ipynb+++ b/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tracing.ipynb@@ -98,6 +98,8 @@"In turn, the runtime is already instrumented to log, see [Core Telemetry Guide](../core-user-guide/framework/telemetry.md).\n","To disable the agent runtime telemetry, you can set the `trace_provider` to\n","`opentelemetry.trace.NoOpTraceProvider` in the runtime constructor.\n",+ "\n",+ "Additionally, you can set the environment varibale `AUTOGEN_DISABLE_RUNTIME_TRACING` to `true` to disable the agent runtime telemetry if you don't have access to the runtime constructor. For example, if you are using `ComponentConfig`.\n","```"]},diff --git a/python/packages/autogen-core/src/autogen_core/_single_threaded_agent_runtime.py b/python/packages/autogen-core/src/autogen_core/_single_threaded_agent_runtime.pyindex 5f5636441..08627756f 100644--- a/python/packages/autogen-core/src/autogen_core/_single_threaded_agent_runtime.py+++ b/python/packages/autogen-core/src/autogen_core/_single_threaded_agent_runtime.py@@ -160,6 +160,7 @@ class SingleThreadedAgentRuntime(AgentRuntime):intervention_handlers (List[InterventionHandler], optional): A list of interventionhandlers that can intercept messages before they are sent or published. Defaults to None.tracer_provider (TracerProvider, optional): The tracer provider to use for tracing. Defaults to None.+ Additionally, you can set environment variable `AUTOGEN_DISABLE_RUNTIME_TRACING` to `true` to disable the agent runtime telemetry if you don't have access to the runtime constructor. For example, if you are using `ComponentConfig`.ignore_unhandled_exceptions (bool, optional): Whether to ignore unhandled exceptions in that occur in agent event handlers. Any background exceptions will be raised on the next call to `process_next` or from an awaited `stop`, `stop_when_idle` or `stop_when`. Note, this does not apply to RPC handlers. Defaults to True.Examples:diff --git a/python/packages/autogen-core/src/autogen_core/_telemetry/_tracing.py b/python/packages/autogen-core/src/autogen_core/_telemetry/_tracing.pyindex aa1835090..2f73e6e5a 100644--- a/python/packages/autogen-core/src/autogen_core/_telemetry/_tracing.py+++ b/python/packages/autogen-core/src/autogen_core/_telemetry/_tracing.py@@ -1,4 +1,5 @@import contextlib+import osfrom typing import Dict, Generic, Iterator, Optionalfrom opentelemetry.trace import NoOpTracerProvider, Span, SpanKind, TracerProvider, get_tracer_provider@@ -22,11 +23,18 @@ class TraceHelper(Generic[Operation, Destination, ExtraAttributes]):tracer_provider: TracerProvider | None,instrumentation_builder_config: TracingConfig[Operation, Destination, ExtraAttributes],) -> None:+ self.instrumentation_builder_config = instrumentation_builder_config++ disable_runtime_tracing = os.environ.get("AUTOGEN_DISABLE_RUNTIME_TRACING") == "true"+ if disable_runtime_tracing:+ self.tracer_provider: TracerProvider = NoOpTracerProvider()+ self.tracer = self.tracer_provider.get_tracer(f"autogen {instrumentation_builder_config.name}")+ return+# Evaluate in order: first try tracer_provider param, then get_tracer_provider(), finally fallback to NoOp# This allows for nested tracing with a default tracer provided by the userself.tracer_provider = tracer_provider or get_tracer_provider() or NoOpTracerProvider()self.tracer = self.tracer_provider.get_tracer(f"autogen {instrumentation_builder_config.name}")- self.instrumentation_builder_config = instrumentation_builder_config@contextlib.contextmanagerdef trace_block(