LangSmith는 OpenTelemetry 통합을 통해 Google Agent Development Kit (ADK) 애플리케이션 추적을 지원합니다. 이 가이드는 Google ADK 에이전트에서 자동으로 trace를 캡처하고 모니터링 및 분석을 위해 LangSmith로 전송하는 방법을 보여줍니다.
선호하는 패키지 매니저를 사용하여 필요한 패키지를 설치하세요:
pip install langsmith google-adk
최적의 OpenTelemetry 지원을 위해 LangSmith Python SDK 버전 langsmith>=0.4.26이 필요합니다.
1. 환경 변수 구성
LangSmith API key와 프로젝트 이름을 설정하세요:
export LANGSMITH_API_KEY=<your_langsmith_api_key>
export LANGSMITH_PROJECT=<your_project_name>
2. OpenTelemetry 통합 구성
Google ADK 애플리케이션에서 LangSmith OpenTelemetry 통합을 import하고 구성하세요. 이렇게 하면 OpenTelemetry용 Google ADK span이 자동으로 계측됩니다.
from langsmith.integrations.otel import configure
# Configure LangSmith tracing
configure(project_name="adk-example") # Optional: can also use LANGSMITH_PROJECT and LANGSMITH_API_KEY environment variables
또는 Openinference GoogleADKInstrumentor를 사용할 수 있습니다:
from langsmith.integrations.otel import configure
from openinference.instrumentation.google_adk import GoogleADKInstrumentor
# Configure LangSmith tracing
configure(project_name="adk-example")
# Instrument Google ADK directly
GoogleADKInstrumentor().instrument()
OpenTelemetry 환경 변수를 설정하거나 exporter를 수동으로 구성할 필요가 없습니다—configure()가 모든 것을 자동으로 처리합니다.
3. ADK 에이전트 생성 및 실행
구성이 완료되면 Google ADK 애플리케이션이 자동으로 LangSmith에 trace를 전송합니다:
이 예제는 에이전트, 세션, runner를 설정한 다음 메시지를 보내고 이벤트를 스트리밍하는 최소한의 앱을 포함합니다.
import asyncio
from langsmith.integrations.otel import configure
from google.adk import Runner
from google.adk.agents import LlmAgent
from google.adk.sessions import InMemorySessionService
from google.genai import types
# Configure LangSmith tracing
configure(project_name="travel-assistant")
# Define your tools
def get_flight_info(destination: str, departure_date: str) -> dict:
"""Get flight information for a destination."""
return {
"destination": destination,
"departure_date": departure_date,
"price": "$450",
"duration": "5h 30m",
"airline": "Example Airways"
}
def get_hotel_recommendations(city: str, check_in: str) -> dict:
"""Get hotel recommendations for a city."""
return {
"city": city,
"check_in": check_in,
"hotels": [
{"name": "Grand Plaza Hotel", "rating": 4.5, "price": "$120/night"},
{"name": "City Center Inn", "rating": 4.2, "price": "$95/night"}
]
}
async def main():
# Create your ADK agent
agent = LlmAgent(
name="travel_assistant",
tools=[get_flight_info, get_hotel_recommendations],
model="gemini-2.5-flash-lite",
instruction="You are a helpful travel assistant that can help with flights and hotels.",
)
# Set up session service and runner
session_service = InMemorySessionService()
runner = Runner(
app_name="travel_app",
agent=agent,
session_service=session_service
)
# Create a session
user_id = "traveler_456"
session_id = "session_789"
await session_service.create_session(
app_name="travel_app",
user_id=user_id,
session_id=session_id
)
# Send a message to the agent
new_message = types.Content(
parts=[types.Part(text="I need to book a flight to Paris for March 15th and find a good hotel.")],
role="user",
)
# Run the agent and process events
events = runner.run(
user_id=user_id,
session_id=session_id,
new_message=new_message,
)
for event in events:
print(event)
if __name__ == "__main__":
asyncio.run(main())
LangSmith에서 trace 보기
- Agent 대화: 사용자와 ADK 에이전트 간의 완전한 대화 흐름.
- Tool 호출: 에이전트가 수행한 개별 함수 호출.
- Model 상호작용: Gemini 모델을 사용한 LLM 요청 및 응답.
- Session 정보: 관련 trace를 구성하기 위한 사용자 및 세션 컨텍스트.
- Model 상호작용: Gemini 모델을 사용한 LLM 요청 및 응답
고급 사용법
ADK 애플리케이션에서 span attribute를 설정하여 trace에 사용자 정의 metadata를 추가할 수 있습니다:
from opentelemetry import trace
# Get the current tracer
tracer = trace.get_tracer(__name__)
async def main():
with tracer.start_as_current_span("travel_booking_session") as span:
# Add custom metadata
span.set_attribute("langsmith.metadata.user_type", "premium")
span.set_attribute("langsmith.metadata.booking_source", "mobile_app")
span.set_attribute("langsmith.span.tags", "travel,booking,premium")
agent = LlmAgent(
name="travel_assistant",
tools=[get_flight_info, get_hotel_recommendations],
model="gemini-2.5-flash-lite",
instruction="You are a helpful travel assistant that can help with flights and hotels.",
)
session_service = InMemorySessionService()
runner = Runner(
app_name="travel_app",
agent=agent,
session_service=session_service
)
# Continue with your ADK workflow
# ...