From 229b4d9be7d511ae09119e400a96cb62e794b730 Mon Sep 17 00:00:00 2001 From: Marko Hietala Date: Wed, 12 Mar 2025 11:00:15 -0500 Subject: [PATCH 1/4] adding agents instrumentation to configure_azure_monitor --- .../azure/monitor/opentelemetry/_configure.py | 34 +++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) 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..7c82a6f1b652 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,42 @@ def _setup_additional_azure_sdk_instrumentations(configurations: Dict[str, Confi _logger.debug("Instrumentation skipped for library azure_sdk") return + ai_inference_instrumentor_imported = False try: - from azure.ai.inference.tracing import AIInferenceInstrumentor # pylint: disable=import-error,no-name-in-module + from azure.ai.inference.tracing2 import AIInferenceInstrumentor # pylint: disable=import-error,no-name-in-module + ai_inference_instrumentor_imported = True except Exception as ex: # pylint: disable=broad-except _logger.debug( "Failed to import AIInferenceInstrumentor from azure-ai-inference", exc_info=ex, ) - return + if ai_inference_instrumentor_imported: + try: + AIInferenceInstrumentor().instrument() + except Exception as ex: # pylint: disable=broad-except + _logger.warning( + "Exception occurred when instrumenting: %s.", + "azure-ai-inference", + exc_info=ex, + ) + + ai_agents_instrumentor_imported = False try: - AIInferenceInstrumentor().instrument() + from azure.ai.projects.telemetry.agents2 import AIAgentsInstrumentor # pylint: disable=import-error,no-name-in-module + ai_agents_instrumentor_imported = True except Exception as ex: # pylint: disable=broad-except - _logger.warning( - "Exception occurred when instrumenting: %s.", - "azure-ai-inference", + _logger.debug( + "Failed to import AIAgentsInstrumentor from azure-ai-projects-telemetry-agents", exc_info=ex, ) + + if ai_agents_instrumentor_imported: + try: + AIAgentsInstrumentor().instrument() + except Exception as ex: # pylint: disable=broad-except + _logger.warning( + "Exception occurred when instrumenting: %s.", + "azure-ai-projects-telemetry-agents", + exc_info=ex, + ) From a40d90d1a063a3f735fa6041545e95dce06e8443 Mon Sep 17 00:00:00 2001 From: Marko Hietala Date: Wed, 12 Mar 2025 11:03:15 -0500 Subject: [PATCH 2/4] removing 2 from package names that was left from testing --- .../azure/monitor/opentelemetry/_configure.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 7c82a6f1b652..39f9633a5ffb 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py +++ b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py @@ -271,7 +271,7 @@ def _setup_additional_azure_sdk_instrumentations(configurations: Dict[str, Confi ai_inference_instrumentor_imported = False try: - from azure.ai.inference.tracing2 import AIInferenceInstrumentor # pylint: disable=import-error,no-name-in-module + from azure.ai.inference.tracing import AIInferenceInstrumentor # pylint: disable=import-error,no-name-in-module ai_inference_instrumentor_imported = True except Exception as ex: # pylint: disable=broad-except _logger.debug( @@ -291,7 +291,7 @@ def _setup_additional_azure_sdk_instrumentations(configurations: Dict[str, Confi ai_agents_instrumentor_imported = False try: - from azure.ai.projects.telemetry.agents2 import AIAgentsInstrumentor # pylint: disable=import-error,no-name-in-module + from azure.ai.projects.telemetry.agents import AIAgentsInstrumentor # pylint: disable=import-error,no-name-in-module ai_agents_instrumentor_imported = True except Exception as ex: # pylint: disable=broad-except _logger.debug( From 31633233f65f8219468b4e82dc9283112c5a2ad3 Mon Sep 17 00:00:00 2001 From: Marko Hietala Date: Wed, 12 Mar 2025 11:54:57 -0500 Subject: [PATCH 3/4] adding sample and updating changelog --- .../agents/sample_agents_basics_with_azure_monitor_tracing.py | 4 ++-- sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) 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 From 7ed7c3254f663e22e4b952011b19ace1585c55c5 Mon Sep 17 00:00:00 2001 From: Marko Hietala Date: Wed, 12 Mar 2025 13:03:35 -0500 Subject: [PATCH 4/4] changing to array of instrumentors --- .../azure/monitor/opentelemetry/_configure.py | 55 ++++++++----------- 1 file changed, 22 insertions(+), 33 deletions(-) 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 39f9633a5ffb..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,42 +269,31 @@ def _setup_additional_azure_sdk_instrumentations(configurations: Dict[str, Confi _logger.debug("Instrumentation skipped for library azure_sdk") return - ai_inference_instrumentor_imported = False - try: - from azure.ai.inference.tracing import AIInferenceInstrumentor # pylint: disable=import-error,no-name-in-module - ai_inference_instrumentor_imported = True - except Exception as ex: # pylint: disable=broad-except - _logger.debug( - "Failed to import AIInferenceInstrumentor from azure-ai-inference", - exc_info=ex, - ) + instrumentors = [ + ("azure.ai.inference.tracing", "AIInferenceInstrumentor"), + ("azure.ai.projects.telemetry.agents", "AIAgentsInstrumentor") + ] - if ai_inference_instrumentor_imported: + for module_path, class_name in instrumentors: + instrumentor_imported = False try: - AIInferenceInstrumentor().instrument() + module = __import__(module_path, fromlist=[class_name]) + instrumentor_imported = True except Exception as ex: # pylint: disable=broad-except - _logger.warning( - "Exception occurred when instrumenting: %s.", - "azure-ai-inference", + _logger.debug( + "Failed to import %s from %s", + class_name, + module_path, exc_info=ex, ) - ai_agents_instrumentor_imported = False - try: - from azure.ai.projects.telemetry.agents import AIAgentsInstrumentor # pylint: disable=import-error,no-name-in-module - ai_agents_instrumentor_imported = True - except Exception as ex: # pylint: disable=broad-except - _logger.debug( - "Failed to import AIAgentsInstrumentor from azure-ai-projects-telemetry-agents", - exc_info=ex, - ) - - if ai_agents_instrumentor_imported: - try: - AIAgentsInstrumentor().instrument() - except Exception as ex: # pylint: disable=broad-except - _logger.warning( - "Exception occurred when instrumenting: %s.", - "azure-ai-projects-telemetry-agents", - 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, + )