This will add Agent observability on Application Insights - Tested on Langgraph<1.0.0
uv pip install "opentelemetry-instrumentation-langchain>=0.48.1" "langchain-azure-ai==0.1.8" "azure-monitor-opentelemetry==1.8.0"
Prepare the utils function
# utils/tracers.py
import os
from dotenv import load_dotenv
from langchain_azure_ai.callbacks.tracers import AzureAIOpenTelemetryTracer
from opentelemetry.instrumentation.langchain import LangchainInstrumentor
load_dotenv()
def get_microsoft_tracer(trace_name: str) -> list:
if not os.environ.get("APPLICATION_INSIGHTS_CONNECTION_STRING"):
return []
instrumentor = LangchainInstrumentor()
if not instrumentor.is_instrumented_by_opentelemetry:
instrumentor.instrument()
azure_tracer = AzureAIOpenTelemetryTracer(
connection_string=os.environ.get("APPLICATION_INSIGHTS_CONNECTION_STRING"),
enable_content_recording=True,
name=trace_name,
)
return azure_tracerAdd it to CompiledGraph callback
...
...
callbacks = []
tracer = get_microsoft_tracer("DeepAgentEvaluation")
if tracer:
callbacks.append(tracer)
compiled_agent = agent_builder.compile().with_config({'recursion_limit': 50, 'callbacks': callbacks})
...
...