Skip to content

Commit fc63189

Browse files
feat(closes OPEN-7559): add callbacks and offline buffering for platform tracing
1 parent 1ec6ca7 commit fc63189

File tree

4 files changed

+1271
-167
lines changed

4 files changed

+1271
-167
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "intro",
6+
"metadata": {},
7+
"source": [
8+
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/openlayer-ai/openlayer-python/blob/main/examples/tracing/offline_buffering.ipynb)\n",
9+
"\n",
10+
"\n",
11+
"# <a id=\"top\">Offline buffering for tracing</a>\n",
12+
"\n",
13+
"This notebook shows how to use offline buffering to handle network failures gracefully when tracing with Openlayer."
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": null,
19+
"id": "install",
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"!pip install openlayer"
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"id": "setup",
29+
"metadata": {},
30+
"source": [
31+
"## 1. Configure with offline buffering"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": null,
37+
"id": "config",
38+
"metadata": {},
39+
"outputs": [],
40+
"source": [
41+
"from openlayer.lib import trace, configure\n",
42+
"\n",
43+
"\n",
44+
"# Define a callback for when traces fail to send\n",
45+
"def on_failure(trace_data, config, error): # noqa: ARG001\n",
46+
" # Failed traces are automatically buffered when offline_buffer_enabled=True\n",
47+
" pass\n",
48+
"\n",
49+
"# Configure tracer with offline buffering\n",
50+
"configure(\n",
51+
" api_key=\"your_api_key\",\n",
52+
" inference_pipeline_id=\"your_pipeline_id\",\n",
53+
" on_flush_failure=on_failure,\n",
54+
" offline_buffer_enabled=True,\n",
55+
" offline_buffer_path=\"./buffer\",\n",
56+
" max_buffer_size=100\n",
57+
")"
58+
]
59+
},
60+
{
61+
"cell_type": "markdown",
62+
"id": "usage",
63+
"metadata": {},
64+
"source": [
65+
"## 2. Use tracing normally"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": null,
71+
"id": "trace",
72+
"metadata": {},
73+
"outputs": [],
74+
"source": [
75+
"@trace()\n",
76+
"def process_query(query: str) -> str:\n",
77+
" return f\"Processed: {query}\"\n",
78+
"\n",
79+
"# If network fails, traces are automatically buffered\n",
80+
"result = process_query(\"What is AI?\")\n",
81+
"result"
82+
]
83+
},
84+
{
85+
"cell_type": "markdown",
86+
"id": "status",
87+
"metadata": {},
88+
"source": [
89+
"## 3. Check buffer status"
90+
]
91+
},
92+
{
93+
"cell_type": "code",
94+
"execution_count": null,
95+
"id": "check",
96+
"metadata": {},
97+
"outputs": [],
98+
"source": [
99+
"from openlayer.lib.tracing import tracer\n",
100+
"\n",
101+
"status = tracer.get_buffer_status()\n",
102+
"status"
103+
]
104+
},
105+
{
106+
"cell_type": "markdown",
107+
"id": "replay",
108+
"metadata": {},
109+
"source": [
110+
"## 4. Replay buffered traces"
111+
]
112+
},
113+
{
114+
"cell_type": "code",
115+
"execution_count": null,
116+
"id": "replay-code",
117+
"metadata": {},
118+
"outputs": [],
119+
"source": [
120+
"# When connectivity is restored, replay failed traces\n",
121+
"result = tracer.replay_buffered_traces()\n",
122+
"result"
123+
]
124+
},
125+
{
126+
"cell_type": "markdown",
127+
"id": "summary",
128+
"metadata": {},
129+
"source": [
130+
"## Summary\n",
131+
"\n",
132+
"With offline buffering:\n",
133+
"\n",
134+
"- ✅ Failed traces are automatically saved locally\n",
135+
"- ✅ No data loss during network outages \n",
136+
"- ✅ Easy replay when connectivity returns\n",
137+
"- ✅ Custom failure callbacks for monitoring"
138+
]
139+
}
140+
],
141+
"metadata": {
142+
"kernelspec": {
143+
"display_name": "Python 3 (ipykernel)",
144+
"language": "python",
145+
"name": "python3"
146+
},
147+
"language_info": {
148+
"codemirror_mode": {
149+
"name": "ipython",
150+
"version": 3
151+
},
152+
"file_extension": ".py",
153+
"mimetype": "text/x-python",
154+
"name": "python",
155+
"nbconvert_exporter": "python",
156+
"pygments_lexer": "ipython3",
157+
"version": "3.9.18"
158+
}
159+
},
160+
"nbformat": 4,
161+
"nbformat_minor": 5
162+
}

src/openlayer/lib/__init__.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
__all__ = [
44
"configure",
55
"trace",
6-
"trace_anthropic",
6+
"trace_anthropic",
77
"trace_openai",
88
"trace_openai_assistant_thread_run",
99
"trace_mistral",
@@ -16,6 +16,10 @@
1616
"trace_litellm",
1717
"update_current_trace",
1818
"update_current_step",
19+
# Offline buffer management functions
20+
"replay_buffered_traces",
21+
"get_buffer_status",
22+
"clear_offline_buffer",
1923
# User and session context functions
2024
"set_user_session_context",
2125
"update_trace_user_session",
@@ -40,6 +44,11 @@
4044
update_current_trace = tracer.update_current_trace
4145
update_current_step = tracer.update_current_step
4246

47+
# Offline buffer management functions
48+
replay_buffered_traces = tracer.replay_buffered_traces
49+
get_buffer_status = tracer.get_buffer_status
50+
clear_offline_buffer = tracer.clear_offline_buffer
51+
4352

4453
def trace_anthropic(client):
4554
"""Trace Anthropic chat completions."""
@@ -115,27 +124,19 @@ def trace_bedrock(client):
115124
try:
116125
import boto3
117126
except ImportError:
118-
raise ImportError(
119-
"boto3 is required for Bedrock tracing. Install with: pip install boto3"
120-
)
127+
raise ImportError("boto3 is required for Bedrock tracing. Install with: pip install boto3")
121128

122129
from .integrations import bedrock_tracer
123130

124131
# Check if it's a boto3 client for bedrock-runtime service
125-
if (
126-
not hasattr(client, "_service_model")
127-
or client._service_model.service_name != "bedrock-runtime"
128-
):
129-
raise ValueError(
130-
"Invalid client. Please provide a boto3 bedrock-runtime client."
131-
)
132+
if not hasattr(client, "_service_model") or client._service_model.service_name != "bedrock-runtime":
133+
raise ValueError("Invalid client. Please provide a boto3 bedrock-runtime client.")
132134
return bedrock_tracer.trace_bedrock(client)
133135

134136

135-
136137
def trace_oci_genai(client, estimate_tokens: bool = True):
137138
"""Trace OCI GenAI chat completions.
138-
139+
139140
Args:
140141
client: OCI GenAI client.
141142
estimate_tokens: Whether to estimate tokens when not available. Defaults to True.
@@ -162,31 +163,27 @@ def trace_oci_genai(client, estimate_tokens: bool = True):
162163
# --------------------------------- LiteLLM ---------------------------------- #
163164
def trace_litellm():
164165
"""Enable tracing for LiteLLM completions.
165-
166+
166167
This function patches litellm.completion to automatically trace all completions
167168
made through the LiteLLM library, which provides a unified interface to 100+ LLM APIs.
168-
169+
169170
Example:
170171
>>> import litellm
171172
>>> from openlayer.lib import trace_litellm
172-
>>>
173173
>>> # Enable tracing
174174
>>> trace_litellm()
175-
>>>
176175
>>> # Use LiteLLM normally - tracing happens automatically
177176
>>> response = litellm.completion(
178177
... model="gpt-3.5-turbo",
179178
... messages=[{"role": "user", "content": "Hello!"}],
180-
... inference_id="custom-id-123" # Optional Openlayer parameter
179+
... inference_id="custom-id-123", # Optional Openlayer parameter
181180
... )
182181
"""
183182
# pylint: disable=import-outside-toplevel
184183
try:
185184
import litellm
186185
except ImportError:
187-
raise ImportError(
188-
"litellm is required for LiteLLM tracing. Install with: pip install litellm"
189-
)
186+
raise ImportError("litellm is required for LiteLLM tracing. Install with: pip install litellm")
190187

191188
from .integrations import litellm_tracer
192189

0 commit comments

Comments
 (0)