-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_ingestion.go
More file actions
322 lines (284 loc) · 9.89 KB
/
api_ingestion.go
File metadata and controls
322 lines (284 loc) · 9.89 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package langfuse
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"time"
)
const (
ingestionPath = "/api/public/ingestion"
maxRetries = 3
retryBaseDelay = 200 * time.Millisecond
flushTimeout = 15 * time.Second
)
// --- ingestion event types ---
type ingestionEvent struct {
ID string `json:"id"`
Type string `json:"type"`
Timestamp string `json:"timestamp"`
Body any `json:"body"`
}
type ingestionRequest struct {
Batch []ingestionEvent `json:"batch"`
}
type traceBody struct {
ID string `json:"id"`
Timestamp string `json:"timestamp,omitempty"`
Name string `json:"name,omitempty"`
UserID string `json:"userId,omitempty"`
SessionID string `json:"sessionId,omitempty"`
Input any `json:"input,omitempty"`
Output any `json:"output,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
Tags []string `json:"tags,omitempty"`
Public bool `json:"public,omitempty"`
Release string `json:"release,omitempty"`
Version string `json:"version,omitempty"`
}
type spanBody struct {
ID string `json:"id"`
TraceID string `json:"traceId"`
ParentObservationID string `json:"parentObservationId,omitempty"`
Name string `json:"name,omitempty"`
StartTime string `json:"startTime,omitempty"`
EndTime string `json:"endTime,omitempty"`
Input any `json:"input,omitempty"`
Output any `json:"output,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
Level ObservationLevel `json:"level,omitempty"`
StatusMessage string `json:"statusMessage,omitempty"`
Version string `json:"version,omitempty"`
}
type generationBody struct {
ID string `json:"id"`
TraceID string `json:"traceId"`
ParentObservationID string `json:"parentObservationId,omitempty"`
Name string `json:"name,omitempty"`
StartTime string `json:"startTime,omitempty"`
EndTime string `json:"endTime,omitempty"`
CompletionStartTime string `json:"completionStartTime,omitempty"`
Model string `json:"model,omitempty"`
ModelParameters map[string]string `json:"modelParameters,omitempty"`
Input any `json:"input,omitempty"`
Output any `json:"output,omitempty"`
Usage *Usage `json:"usage,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
Level ObservationLevel `json:"level,omitempty"`
StatusMessage string `json:"statusMessage,omitempty"`
Version string `json:"version,omitempty"`
PromptName string `json:"promptName,omitempty"`
PromptVersion int `json:"promptVersion,omitempty"`
}
type eventBody struct {
ID string `json:"id"`
TraceID string `json:"traceId"`
ParentObservationID string `json:"parentObservationId,omitempty"`
Name string `json:"name,omitempty"`
StartTime string `json:"startTime,omitempty"`
Input any `json:"input,omitempty"`
Output any `json:"output,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
Level ObservationLevel `json:"level,omitempty"`
StatusMessage string `json:"statusMessage,omitempty"`
Version string `json:"version,omitempty"`
}
type scoreBody struct {
ID string `json:"id"`
TraceID string `json:"traceId"`
Name string `json:"name"`
Value float64 `json:"value"`
ObservationID string `json:"observationId,omitempty"`
Comment string `json:"comment,omitempty"`
DataType string `json:"dataType,omitempty"`
}
// --- buffering & flush ---
// Flush sends all buffered ingestion events immediately. It blocks until the send completes.
func (c *Client) Flush(ctx context.Context) error {
c.mu.Lock()
batch := c.takeBufLocked()
c.mu.Unlock()
if len(batch) == 0 {
return nil
}
return c.sendBatch(ctx, batch)
}
// Shutdown flushes all pending ingestion events and releases resources.
func (c *Client) Shutdown(ctx context.Context) error {
c.mu.Lock()
c.closed = true
if c.timer != nil {
c.timer.Stop()
c.timer = nil
}
batch := c.takeBufLocked()
c.mu.Unlock()
// Wait for any in-flight sends.
c.wg.Wait()
if len(batch) == 0 {
return nil
}
return c.sendBatch(ctx, batch)
}
// enqueue adds an event to the buffer and triggers a flush if needed.
func (c *Client) enqueue(e ingestionEvent) {
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
log.Printf("langfuse: warning: event enqueued after Shutdown, dropping %s", e.Type)
return
}
c.buf = append(c.buf, e)
// Drop oldest events if buffer exceeds max size.
if len(c.buf) > c.cfg.MaxBufferSize {
c.buf = c.buf[len(c.buf)-c.cfg.MaxBufferSize:]
log.Printf("langfuse: buffer full (%d), dropped oldest event", c.cfg.MaxBufferSize)
}
if len(c.buf) >= c.cfg.FlushBatch {
batch := c.takeBufLocked()
c.flushAsync(batch)
return
}
// Start a flush timer if not already running.
if c.timer == nil {
c.timer = time.AfterFunc(c.cfg.FlushInterval, func() {
c.mu.Lock()
batch := c.takeBufLocked()
if len(batch) > 0 {
// Increment wg under the lock so Shutdown's wg.Wait()
// is guaranteed to see it before returning.
c.wg.Add(1)
}
c.mu.Unlock()
if len(batch) > 0 {
go func() {
defer c.wg.Done()
c.flushSem <- struct{}{}
defer func() { <-c.flushSem }()
ctx, cancel := context.WithTimeout(context.Background(), flushTimeout)
defer cancel()
if err := c.sendBatch(ctx, batch); err != nil {
log.Printf("langfuse: flush error: %v", err)
}
}()
}
})
}
}
// takeBufLocked swaps out the buffer and stops the flush timer.
// Must be called with c.mu held.
func (c *Client) takeBufLocked() []ingestionEvent {
if len(c.buf) == 0 {
return nil
}
batch := c.buf
c.buf = nil
if c.timer != nil {
c.timer.Stop()
c.timer = nil
}
return batch
}
// flushAsync sends a batch in a background goroutine. Must be called with c.mu held
// so that wg.Add is visible to Shutdown before the lock is released.
func (c *Client) flushAsync(batch []ingestionEvent) {
c.wg.Add(1)
go func() {
defer c.wg.Done()
c.flushSem <- struct{}{}
defer func() { <-c.flushSem }()
ctx, cancel := context.WithTimeout(context.Background(), flushTimeout)
defer cancel()
if err := c.sendBatch(ctx, batch); err != nil {
log.Printf("langfuse: flush error: %v", err)
}
}()
}
// sendBatch sends a batch of ingestion events with retries for transient errors.
func (c *Client) sendBatch(ctx context.Context, batch []ingestionEvent) error {
body, err := json.Marshal(ingestionRequest{Batch: batch})
if err != nil {
return fmt.Errorf("marshal: %w", err)
}
var lastErr error
for attempt := 0; attempt < maxRetries; attempt++ {
if attempt > 0 {
delay := retryBaseDelay * time.Duration(1<<(attempt-1))
select {
case <-ctx.Done():
return fmt.Errorf("retry cancelled: %w (last error: %v)", ctx.Err(), lastErr)
case <-time.After(delay):
}
log.Printf("langfuse: retrying send (attempt %d/%d)", attempt+1, maxRetries)
}
lastErr = c.doSend(ctx, body, len(batch))
if lastErr == nil {
return nil
}
if !isRetriable(lastErr) {
return lastErr
}
}
return fmt.Errorf("failed after %d attempts: %w", maxRetries, lastErr)
}
// doSend performs a single HTTP POST to the ingestion endpoint.
func (c *Client) doSend(ctx context.Context, body []byte, batchLen int) error {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.cfg.Host+ingestionPath, bytes.NewReader(body))
if err != nil {
return fmt.Errorf("new request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(c.cfg.PublicKey, c.cfg.SecretKey)
resp, err := c.httpClient.Do(req)
if err != nil {
return &retriableError{err: fmt.Errorf("http: %w", err)}
}
defer resp.Body.Close()
respBody, _ := io.ReadAll(resp.Body)
if resp.StatusCode >= 500 {
return &retriableError{err: fmt.Errorf("langfuse API returned %d: %s", resp.StatusCode, truncate(respBody, 512))}
}
if resp.StatusCode == http.StatusTooManyRequests {
return &retriableError{err: fmt.Errorf("langfuse API returned 429: %s", truncate(respBody, 512))}
}
if resp.StatusCode >= 400 {
return fmt.Errorf("langfuse API returned %d: %s", resp.StatusCode, truncate(respBody, 512))
}
// Log partial failures from 207 Multi-Status responses.
if resp.StatusCode == http.StatusMultiStatus && len(respBody) > 0 {
var result struct {
Errors []any `json:"errors"`
}
if json.Unmarshal(respBody, &result) == nil && len(result.Errors) > 0 {
log.Printf("langfuse: ingestion partial failure (%d events, %d errors): %s",
batchLen, len(result.Errors), truncate(respBody, 512))
}
}
return nil
}
// --- retriable error ---
// retriableError wraps transient errors (5xx, 429, network) to signal that
// sendBatch should retry the request.
type retriableError struct {
err error
}
func (e *retriableError) Error() string { return e.err.Error() }
func (e *retriableError) Unwrap() error { return e.err }
// isRetriable reports whether err is a transient failure worth retrying.
func isRetriable(err error) bool {
var re *retriableError
return errors.As(err, &re)
}
// event wraps a body into an ingestion event and enqueues it for sending.
func (c *Client) event(typ string, body any) {
c.enqueue(ingestionEvent{
ID: newID(),
Type: typ,
Timestamp: now(),
Body: body,
})
}