diff --git a/sdk/ai/azure-ai-projects/samples/agents/sample_agents_basics_with_azure_monitor_tracing.py b/sdk/ai/azure-ai-projects/samples/agents/sample_agents_basics_with_azure_monitor_tracing.py index b1a437b87846..f6cdad68298f 100644 --- a/sdk/ai/azure-ai-projects/samples/agents/sample_agents_basics_with_azure_monitor_tracing.py +++ b/sdk/ai/azure-ai-projects/samples/agents/sample_agents_basics_with_azure_monitor_tracing.py @@ -51,7 +51,7 @@ with tracer.start_as_current_span(scenario): with project_client: - project_client.telemetry.enable() + #project_client.telemetry.enable() # [END enable_tracing] agent = project_client.agents.create_agent( model=os.environ["MODEL_DEPLOYMENT_NAME"], name="my-assistant", instructions="You are helpful assistant" @@ -62,7 +62,7 @@ print(f"Created thread, thread ID: {thread.id}") message = project_client.agents.create_message( - thread_id=thread.id, role="user", content="Hello, tell me a joke" + thread_id=thread.id, role="user", content="Hello, tell me a hilarious joke" ) print(f"Created message, message ID: {message.id}") diff --git a/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md b/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md index 7687dd98dc0a..a8ce0f3b2026 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md +++ b/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md @@ -4,6 +4,9 @@ ### Features Added +- Enable Azure AI Agents instrumentation + ([#40043](https://github.com/Azure/azure-sdk-for-python/pull/40043)) + ### Breaking Changes ### Bugs Fixed diff --git a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py index c9910392fc19..0767e430370b 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py +++ b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py @@ -269,20 +269,31 @@ def _setup_additional_azure_sdk_instrumentations(configurations: Dict[str, Confi _logger.debug("Instrumentation skipped for library azure_sdk") return - try: - from azure.ai.inference.tracing import AIInferenceInstrumentor # pylint: disable=import-error,no-name-in-module - except Exception as ex: # pylint: disable=broad-except - _logger.debug( - "Failed to import AIInferenceInstrumentor from azure-ai-inference", - exc_info=ex, - ) - return + instrumentors = [ + ("azure.ai.inference.tracing", "AIInferenceInstrumentor"), + ("azure.ai.projects.telemetry.agents", "AIAgentsInstrumentor") + ] - try: - AIInferenceInstrumentor().instrument() - except Exception as ex: # pylint: disable=broad-except - _logger.warning( - "Exception occurred when instrumenting: %s.", - "azure-ai-inference", - exc_info=ex, - ) + for module_path, class_name in instrumentors: + instrumentor_imported = False + try: + module = __import__(module_path, fromlist=[class_name]) + instrumentor_imported = True + except Exception as ex: # pylint: disable=broad-except + _logger.debug( + "Failed to import %s from %s", + class_name, + module_path, + exc_info=ex, + ) + + if instrumentor_imported: + try: + instrumentor_class = getattr(module, class_name) + instrumentor_class().instrument() + except Exception as ex: # pylint: disable=broad-except + _logger.warning( + "Exception occurred when instrumenting using: %s.", + class_name, + exc_info=ex, + )