Skip to content

Commit

Permalink
Automatically instruments demo apps, adds to install target
Browse files Browse the repository at this point in the history
This only works if openai instrumantation is installed - otherwise it
will fail silently. This also adds it to the "learn" install target
  • Loading branch information
elijahbenizzy committed Feb 24, 2025
1 parent 17c66e1 commit eaa7d49
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
15 changes: 14 additions & 1 deletion examples/email-assistant/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
router = APIRouter()


try:
from opentelemetry.instrumentation.openai import OpenAIInstrumentor

OpenAIInstrumentor().instrument()
opentelemetry_available = True
except ImportError:
opentelemetry_available = False
pass


# we want to render this after every response
class EmailAssistantState(pydantic.BaseModel):
app_id: str
Expand Down Expand Up @@ -73,7 +83,10 @@ def _get_application(project_id: str, app_id: str) -> Application:
builder = (
ApplicationBuilder()
.with_graph(graph)
.with_tracker(tracker := LocalTrackingClient(project=project_id))
.with_tracker(
tracker := LocalTrackingClient(project=project_id),
use_otel_tracing=opentelemetry_available,
)
.with_identifiers(app_id=app_id)
.initialize_from(
tracker,
Expand Down
11 changes: 10 additions & 1 deletion examples/multi-modal-chatbot/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@

graph = chat_application.graph

try:
from opentelemetry.instrumentation.openai import OpenAIInstrumentor

OpenAIInstrumentor().instrument()
opentelemetry_available = True
except ImportError:
opentelemetry_available = False
pass


class ChatItem(pydantic.BaseModel):
"""Pydantic model for a chat item. This is used to render the chat history."""
Expand All @@ -50,7 +59,7 @@ def _get_application(project_id: str, app_id: str) -> Application:
default_state={"chat_history": []},
default_entrypoint="prompt",
)
.with_tracker(tracker)
.with_tracker(tracker, use_otel_tracing=opentelemetry_available)
.with_identifiers(app_id=app_id)
.build()
)
Expand Down
12 changes: 11 additions & 1 deletion examples/streaming-fastapi/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@
graph = chat_application.graph


try:
from opentelemetry.instrumentation.openai import OpenAIInstrumentor

OpenAIInstrumentor().instrument()
opentelemetry_available = True
except ImportError:
opentelemetry_available = False
pass


class ChatItem(pydantic.BaseModel):
"""Pydantic model for a chat item. This is used to render the chat history."""

Expand All @@ -52,7 +62,7 @@ def _get_application(project_id: str, app_id: str) -> Application:
default_state={"chat_history": []},
default_entrypoint="prompt",
)
.with_tracker(tracker)
.with_tracker(tracker, use_otel_tracing=opentelemetry_available)
.with_identifiers(app_id=app_id)
.build()
)
Expand Down
11 changes: 7 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,17 @@ tracking = [
]

learn = [
"burr[tracking,streamlit,graphviz,hamilton, cli]"
"burr[tracking,streamlit,graphviz,hamilton,cli,inappexamples]"
]

start = [
"burr[learn]"
]
inappexamples = [
"opentelemetry-api",
"opentelemetry-sdk",
'opentelemetry-instrumentation-openai',
]

# All the bloatware from various LLM demos
# In the future most people will be relying on simple APIs, not this
Expand All @@ -169,9 +174,7 @@ examples = [
"langchain",
"langchain-community",
"langchain-openai",
"opentelemetry-api",
"opentelemetry-sdk",
'opentelemetry-instrumentation-openai',
"burr[inappexamples]"
]

# just install everything for developers
Expand Down

0 comments on commit eaa7d49

Please sign in to comment.