forked from microsoft/opentelemetry-distro-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexporter.py
More file actions
84 lines (63 loc) · 2.51 KB
/
Copy pathexporter.py
File metadata and controls
84 lines (63 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""
Sample: A365 Exporter with LangChain
Demonstrates A365 as an exporter with auto-instrumented LangChain operations.
The distro instruments LangChain automatically — all LLM calls, tool executions,
and agent runs produce OTel spans that flow through the A365 span processors.
A365 auto-enables when ENABLE_A365_OBSERVABILITY_EXPORTER=true is set.
Environment variables:
AZURE_OPENAI_API_KEY=<key> Azure OpenAI API key
AZURE_OPENAI_ENDPOINT=https://<resource>.openai.azure.com/
OPENAI_API_VERSION=2024-10-21 API version (optional, defaults to 2024-10-21)
ENABLE_A365_OBSERVABILITY_EXPORTER=true Enable A365 HTTP exporter (required)
"""
import os
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_core.tools import tool
from langchain_openai import AzureChatOpenAI
from microsoft.opentelemetry import use_microsoft_opentelemetry
def get_token(agent_id: str, tenant_id: str) -> str | None:
"""Example token resolver. Replace with your actual auth logic."""
return "example-bearer-token"
use_microsoft_opentelemetry(
enable_a365=True,
a365_token_resolver=get_token,
instrumentation_options={
"langchain": {"enabled": True},
},
)
@tool
def get_weather(city: str) -> str:
"""Get the current weather for a city."""
weather = {
"seattle": "62°F, partly cloudy",
"paris": "58°F, light rain",
"tokyo": "72°F, sunny",
}
return weather.get(city.lower(), f"No weather data for {city}.")
def main():
llm = AzureChatOpenAI(
azure_deployment="gpt-4o",
api_version=os.environ.get("OPENAI_API_VERSION", "2024-10-21"),
temperature=0,
)
# Simple LLM call — automatically traced
messages = [
SystemMessage(content="You are a helpful assistant."),
HumanMessage(content="What is the capital of France?"),
]
result = llm.invoke(messages)
print("LLM output:", result.content)
# Agent with tools — all steps automatically traced
from langchain.agents import create_agent
agent = create_agent(
llm,
tools=[get_weather],
system_prompt="You are a weather assistant. Use the tool to answer weather questions.",
name="Weather_Agent",
)
agent_result = agent.invoke({"messages": [("human", "What's the weather in Seattle?")]})
print("Agent output:", agent_result["messages"][-1].content)
if __name__ == "__main__":
main()