|
15 | 15 | use_function_invocation, |
16 | 16 | ) |
17 | 17 | from agent_framework.exceptions import ServiceInitializationError, ServiceInvalidRequestError |
18 | | -from agent_framework.observability import use_observability |
| 18 | +from agent_framework.observability import use_instrumentation |
19 | 19 | from agent_framework.openai._responses_client import OpenAIBaseResponsesClient |
20 | 20 | from azure.ai.projects.aio import AIProjectClient |
21 | 21 | from azure.ai.projects.models import ( |
|
49 | 49 |
|
50 | 50 |
|
51 | 51 | @use_function_invocation |
52 | | -@use_observability |
| 52 | +@use_instrumentation |
53 | 53 | @use_chat_middleware |
54 | 54 | class AzureAIClient(OpenAIBaseResponsesClient): |
55 | 55 | """Azure AI Agent client.""" |
@@ -164,27 +164,94 @@ def __init__( |
164 | 164 | # Track whether we should close client connection |
165 | 165 | self._should_close_client = should_close_client |
166 | 166 |
|
167 | | - async def setup_azure_ai_observability(self, enable_sensitive_data: bool | None = None) -> None: |
168 | | - """Use this method to setup tracing in your Azure AI Project. |
| 167 | + async def configure_azure_monitor( |
| 168 | + self, |
| 169 | + enable_sensitive_data: bool = False, |
| 170 | + **kwargs: Any, |
| 171 | + ) -> None: |
| 172 | + """Setup observability with Azure Monitor (Azure AI Foundry integration). |
| 173 | +
|
| 174 | + This method configures Azure Monitor for telemetry collection using the |
| 175 | + connection string from the Azure AI project client. |
169 | 176 |
|
170 | | - This will take the connection string from the project project_client. |
171 | | - It will override any connection string that is set in the environment variables. |
172 | | - It will disable any OTLP endpoint that might have been set. |
| 177 | + Args: |
| 178 | + enable_sensitive_data: Enable sensitive data logging (prompts, responses). |
| 179 | + Should only be enabled in development/test environments. Default is False. |
| 180 | + **kwargs: Additional arguments passed to configure_azure_monitor(). |
| 181 | + Common options include: |
| 182 | + - enable_live_metrics (bool): Enable Azure Monitor Live Metrics |
| 183 | + - credential (TokenCredential): Azure credential for Entra ID auth |
| 184 | + - resource (Resource): Custom OpenTelemetry resource |
| 185 | + See https://learn.microsoft.com/python/api/azure-monitor-opentelemetry/azure.monitor.opentelemetry.configure_azure_monitor |
| 186 | + for full list of options. |
| 187 | +
|
| 188 | + Raises: |
| 189 | + ImportError: If azure-monitor-opentelemetry-exporter is not installed. |
| 190 | +
|
| 191 | + Examples: |
| 192 | + .. code-block:: python |
| 193 | +
|
| 194 | + from agent_framework.azure import AzureAIClient |
| 195 | + from azure.ai.projects.aio import AIProjectClient |
| 196 | + from azure.identity.aio import DefaultAzureCredential |
| 197 | +
|
| 198 | + async with ( |
| 199 | + DefaultAzureCredential() as credential, |
| 200 | + AIProjectClient( |
| 201 | + endpoint="https://your-project.api.azureml.ms", credential=credential |
| 202 | + ) as project_client, |
| 203 | + AzureAIClient(project_client=project_client) as client, |
| 204 | + ): |
| 205 | + # Setup observability with defaults |
| 206 | + await client.configure_azure_monitor() |
| 207 | +
|
| 208 | + # With live metrics enabled |
| 209 | + await client.configure_azure_monitor(enable_live_metrics=True) |
| 210 | +
|
| 211 | + # With sensitive data logging (dev/test only) |
| 212 | + await client.configure_azure_monitor(enable_sensitive_data=True) |
| 213 | +
|
| 214 | + Note: |
| 215 | + This method retrieves the Application Insights connection string from the |
| 216 | + Azure AI project client automatically. You must have Application Insights |
| 217 | + configured in your Azure AI project for this to work. |
173 | 218 | """ |
| 219 | + # Get connection string from project client |
174 | 220 | try: |
175 | 221 | conn_string = await self.project_client.telemetry.get_application_insights_connection_string() |
176 | 222 | except ResourceNotFoundError: |
177 | 223 | logger.warning( |
178 | | - "No Application Insights connection string found for the Azure AI Project, " |
179 | | - "please call setup_observability() manually." |
| 224 | + "No Application Insights connection string found for the Azure AI Project. " |
| 225 | + "Please ensure Application Insights is configured in your Azure AI project, " |
| 226 | + "or call configure_otel_providers() manually with custom exporters." |
180 | 227 | ) |
181 | 228 | return |
182 | | - from agent_framework.observability import setup_observability |
183 | 229 |
|
184 | | - setup_observability( |
185 | | - applicationinsights_connection_string=conn_string, enable_sensitive_data=enable_sensitive_data |
| 230 | + # Import Azure Monitor with proper error handling |
| 231 | + try: |
| 232 | + from azure.monitor.opentelemetry import configure_azure_monitor |
| 233 | + except ImportError as exc: |
| 234 | + raise ImportError( |
| 235 | + "azure-monitor-opentelemetry is required for Azure Monitor integration. " |
| 236 | + "Install it with: pip install azure-monitor-opentelemetry" |
| 237 | + ) from exc |
| 238 | + |
| 239 | + from agent_framework.observability import create_metric_views, create_resource, enable_instrumentation |
| 240 | + |
| 241 | + # Create resource if not provided in kwargs |
| 242 | + if "resource" not in kwargs: |
| 243 | + kwargs["resource"] = create_resource() |
| 244 | + |
| 245 | + # Configure Azure Monitor with connection string and kwargs |
| 246 | + configure_azure_monitor( |
| 247 | + connection_string=conn_string, |
| 248 | + views=create_metric_views(), |
| 249 | + **kwargs, |
186 | 250 | ) |
187 | 251 |
|
| 252 | + # Complete setup with core observability |
| 253 | + enable_instrumentation(enable_sensitive_data=enable_sensitive_data) |
| 254 | + |
188 | 255 | async def __aenter__(self) -> "Self": |
189 | 256 | """Async context manager entry.""" |
190 | 257 | return self |
|
0 commit comments