Skip to content

Commit 938bb79

Browse files
committed
add otel app
1 parent 773a4c6 commit 938bb79

2 files changed

Lines changed: 22 additions & 40 deletions

File tree

examples/agent_otel_appinsights.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
from typing import Annotated
77

88
from agent_framework import ChatAgent
9+
from agent_framework.observability import create_resource, enable_instrumentation
910
from agent_framework.openai import OpenAIChatClient
1011
from azure.identity.aio import DefaultAzureCredential, get_bearer_token_provider
12+
from azure.monitor.opentelemetry import configure_azure_monitor
1113
from dotenv import load_dotenv
1214
from pydantic import Field
1315
from rich import print
@@ -19,27 +21,16 @@
1921
logger = logging.getLogger(__name__)
2022
logger.setLevel(logging.INFO)
2123

22-
# Configure OpenTelemetry export to Azure Application Insights (if connection string is set)
24+
# Configure OpenTelemetry export to Azure Application Insights
25+
# Set APPLICATIONINSIGHTS_CONNECTION_STRING in .env (run 'azd provision' or copy from the Azure Portal)
2326
load_dotenv(override=True)
24-
appinsights_connection_string = os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING")
25-
if appinsights_connection_string:
26-
from azure.monitor.opentelemetry import configure_azure_monitor
27-
from agent_framework.observability import create_resource, enable_instrumentation
28-
29-
os.environ.setdefault("OTEL_SERVICE_NAME", "agent-framework-demo")
30-
configure_azure_monitor(
31-
connection_string=appinsights_connection_string,
32-
resource=create_resource(),
33-
enable_live_metrics=True,
34-
)
35-
enable_instrumentation(enable_sensitive_data=True)
36-
logger.info("Azure Application Insights export enabled")
37-
else:
38-
logger.info(
39-
"Set APPLICATIONINSIGHTS_CONNECTION_STRING in .env to export telemetry to Azure Application Insights. "
40-
"Run 'azd provision' to automatically provision and configure Application Insights, "
41-
"or set the connection string manually from the Azure Portal."
42-
)
27+
configure_azure_monitor(
28+
connection_string=os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"],
29+
resource=create_resource(),
30+
enable_live_metrics=True,
31+
)
32+
enable_instrumentation(enable_sensitive_data=True)
33+
logger.info("Azure Application Insights export enabled")
4334

4435
# Configure OpenAI client based on environment
4536
API_HOST = os.getenv("API_HOST", "github")

examples/spanish/agent_otel_appinsights.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
from typing import Annotated
77

88
from agent_framework import ChatAgent
9+
from agent_framework.observability import create_resource, enable_instrumentation
910
from agent_framework.openai import OpenAIChatClient
1011
from azure.identity.aio import DefaultAzureCredential, get_bearer_token_provider
12+
from azure.monitor.opentelemetry import configure_azure_monitor
1113
from dotenv import load_dotenv
1214
from pydantic import Field
1315
from rich import print
@@ -19,27 +21,16 @@
1921
logger = logging.getLogger(__name__)
2022
logger.setLevel(logging.INFO)
2123

22-
# Configura la exportación de OpenTelemetry a Azure Application Insights (si la cadena de conexión está configurada)
24+
# Configura la exportación de OpenTelemetry a Azure Application Insights
25+
# Configura APPLICATIONINSIGHTS_CONNECTION_STRING en .env (ejecuta 'azd provision' o copia desde el Portal de Azure)
2326
load_dotenv(override=True)
24-
appinsights_connection_string = os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING")
25-
if appinsights_connection_string:
26-
from agent_framework.observability import create_resource, enable_instrumentation
27-
from azure.monitor.opentelemetry import configure_azure_monitor
28-
29-
os.environ.setdefault("OTEL_SERVICE_NAME", "agent-framework-demo")
30-
configure_azure_monitor(
31-
connection_string=appinsights_connection_string,
32-
resource=create_resource(),
33-
enable_live_metrics=True,
34-
)
35-
enable_instrumentation(enable_sensitive_data=True)
36-
logger.info("Exportación a Azure Application Insights habilitada")
37-
else:
38-
logger.info(
39-
"Configura APPLICATIONINSIGHTS_CONNECTION_STRING en .env para exportar telemetría a Azure Application "
40-
"Insights. Ejecuta 'azd provision' para provisionar y configurar Application Insights automáticamente, "
41-
"o configura la cadena de conexión manualmente desde el Portal de Azure."
42-
)
27+
configure_azure_monitor(
28+
connection_string=os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"],
29+
resource=create_resource(),
30+
enable_live_metrics=True,
31+
)
32+
enable_instrumentation(enable_sensitive_data=True)
33+
logger.info("Exportación a Azure Application Insights habilitada")
4334

4435
# Configura el cliente de OpenAI según el entorno
4536
API_HOST = os.getenv("API_HOST", "github")

0 commit comments

Comments
 (0)