Skip to content

Commit a2ceed3

Browse files
M-HietalaMarko Hietala
andauthored
adding agents instrumentation to configure_azure_monitor (Azure#40043)
* adding agents instrumentation to configure_azure_monitor * removing 2 from package names that was left from testing * adding sample and updating changelog * changing to array of instrumentors --------- Co-authored-by: Marko Hietala <[email protected]>
1 parent e94f2de commit a2ceed3

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

sdk/ai/azure-ai-projects/samples/agents/sample_agents_basics_with_azure_monitor_tracing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
with tracer.start_as_current_span(scenario):
5353
with project_client:
54-
project_client.telemetry.enable()
54+
#project_client.telemetry.enable()
5555
# [END enable_tracing]
5656
agent = project_client.agents.create_agent(
5757
model=os.environ["MODEL_DEPLOYMENT_NAME"], name="my-assistant", instructions="You are helpful assistant"
@@ -62,7 +62,7 @@
6262
print(f"Created thread, thread ID: {thread.id}")
6363

6464
message = project_client.agents.create_message(
65-
thread_id=thread.id, role="user", content="Hello, tell me a joke"
65+
thread_id=thread.id, role="user", content="Hello, tell me a hilarious joke"
6666
)
6767
print(f"Created message, message ID: {message.id}")
6868

sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
### Features Added
66

7+
- Enable Azure AI Agents instrumentation
8+
([#40043](https://github.com/Azure/azure-sdk-for-python/pull/40043))
9+
710
### Breaking Changes
811

912
### Bugs Fixed

sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -269,20 +269,31 @@ def _setup_additional_azure_sdk_instrumentations(configurations: Dict[str, Confi
269269
_logger.debug("Instrumentation skipped for library azure_sdk")
270270
return
271271

272-
try:
273-
from azure.ai.inference.tracing import AIInferenceInstrumentor # pylint: disable=import-error,no-name-in-module
274-
except Exception as ex: # pylint: disable=broad-except
275-
_logger.debug(
276-
"Failed to import AIInferenceInstrumentor from azure-ai-inference",
277-
exc_info=ex,
278-
)
279-
return
272+
instrumentors = [
273+
("azure.ai.inference.tracing", "AIInferenceInstrumentor"),
274+
("azure.ai.projects.telemetry.agents", "AIAgentsInstrumentor")
275+
]
280276

281-
try:
282-
AIInferenceInstrumentor().instrument()
283-
except Exception as ex: # pylint: disable=broad-except
284-
_logger.warning(
285-
"Exception occurred when instrumenting: %s.",
286-
"azure-ai-inference",
287-
exc_info=ex,
288-
)
277+
for module_path, class_name in instrumentors:
278+
instrumentor_imported = False
279+
try:
280+
module = __import__(module_path, fromlist=[class_name])
281+
instrumentor_imported = True
282+
except Exception as ex: # pylint: disable=broad-except
283+
_logger.debug(
284+
"Failed to import %s from %s",
285+
class_name,
286+
module_path,
287+
exc_info=ex,
288+
)
289+
290+
if instrumentor_imported:
291+
try:
292+
instrumentor_class = getattr(module, class_name)
293+
instrumentor_class().instrument()
294+
except Exception as ex: # pylint: disable=broad-except
295+
_logger.warning(
296+
"Exception occurred when instrumenting using: %s.",
297+
class_name,
298+
exc_info=ex,
299+
)

0 commit comments

Comments
 (0)