Skip to content

Commit d6946f3

Browse files
committed
feat: support ingestion API
1 parent 041281f commit d6946f3

File tree

12 files changed

+532
-207
lines changed

12 files changed

+532
-207
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ require (
1010

1111
require (
1212
github.com/davecgh/go-spew v1.1.1 // indirect
13+
github.com/google/uuid v1.6.0 // indirect
1314
github.com/pmezard/go-difflib v1.0.0 // indirect
15+
github.com/sirupsen/logrus v1.9.3 // indirect
1416
golang.org/x/net v0.40.0 // indirect
17+
golang.org/x/sys v0.33.0 // indirect
1518
gopkg.in/yaml.v3 v3.0.1 // indirect
1619
)

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
23
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
34
github.com/divar-ir/go-http-mock v0.0.0-20250513125724-292fce49eb83 h1:38lh5mrH0Jqv3wcy0Vhmd+OUEGFKPvsqh2AdL/YTwOo=
45
github.com/divar-ir/go-http-mock v0.0.0-20250513125724-292fce49eb83/go.mod h1:bEFO6+ho24AtbweHj7nhFAfLxHL+Edx9vScCaO/nDgc=
6+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
7+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
58
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
69
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
10+
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
11+
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
12+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
13+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
714
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
815
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
916
golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY=
1017
golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
18+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
19+
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
20+
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
1121
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1222
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
23+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1324
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
1425
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1526
resty.dev/v3 v3.0.0-beta.3 h1:3kEwzEgCnnS6Ob4Emlk94t+I/gClyoah7SnNi67lt+E=

internal/constants/constants.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package constants
2+
3+
type IngestionEventType string
4+
5+
const (
6+
IngestionEventTypeTraceCreate IngestionEventType = "trace-create"
7+
IngestionEventTypeGenerationCreate IngestionEventType = "generation-create"
8+
IngestionEventTypeGenerationUpdate IngestionEventType = "generation-update"
9+
IngestionEventTypeScoreCreate IngestionEventType = "score-create"
10+
IngestionEventTypeSpanCreate IngestionEventType = "span-create"
11+
IngestionEventTypeSpanUpdate IngestionEventType = "span-update"
12+
IngestionEventTypeEventCreate IngestionEventType = "event-create"
13+
)

internal/models/models.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package models
2+
3+
import (
4+
"time"
5+
6+
"github.com/divar-ir/golangfuse/internal/constants"
7+
)
8+
9+
type Trace struct {
10+
ID string `json:"id,omitempty"`
11+
Timestamp *time.Time `json:"timestamp,omitempty"`
12+
Name string `json:"name,omitempty"`
13+
UserID string `json:"userId,omitempty"`
14+
Input any `json:"input,omitempty"`
15+
Output any `json:"output,omitempty"`
16+
SessionID string `json:"sessionId,omitempty"`
17+
Release string `json:"release,omitempty"`
18+
Version string `json:"version,omitempty"`
19+
Metadata any `json:"metadata,omitempty"`
20+
Tags []string `json:"tags,omitempty"`
21+
Public bool `json:"public,omitempty"`
22+
}
23+
24+
type Ingestion struct {
25+
Batch []IngestionEvent `json:"batch"`
26+
}
27+
28+
type IngestionEvent struct {
29+
ID string `json:"id"`
30+
Timestamp time.Time `json:"timestamp"`
31+
Type constants.IngestionEventType `json:"type"`
32+
Body any `json:"body"`
33+
}
34+
35+
type PromptItem struct {
36+
Role string `json:"role"`
37+
Content string `json:"content"`
38+
}
39+
40+
type ChatPrompt struct {
41+
ID string `json:"id"`
42+
CreatedAt time.Time `json:"createdAt"`
43+
UpdatedAt time.Time `json:"updatedAt"`
44+
ProjectID string `json:"projectId"`
45+
CreatedBy string `json:"createdBy"`
46+
Prompt []PromptItem `json:"prompt"`
47+
Name string `json:"name"`
48+
Version int `json:"version"`
49+
Type string `json:"type"`
50+
Config any `json:"config"`
51+
Tags []string `json:"tags"`
52+
Labels []string `json:"labels"`
53+
}

internal/observer/observer.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package observer
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/sirupsen/logrus"
8+
)
9+
10+
type Observer[T any] interface {
11+
StartObserve(ctx context.Context, period time.Duration)
12+
}
13+
14+
type observerImpl[T any] struct {
15+
queue Queue[T]
16+
eventHandler EventHandler[T]
17+
}
18+
19+
type EventHandler[T any] func(ctx context.Context, events []T) error
20+
21+
func NewObserver[T any](queue Queue[T], eventHandler EventHandler[T]) Observer[T] {
22+
o := &observerImpl[T]{
23+
queue: queue,
24+
eventHandler: eventHandler,
25+
}
26+
return o
27+
}
28+
29+
func (o *observerImpl[T]) StartObserve(ctx context.Context, period time.Duration) {
30+
ticker := time.NewTicker(period)
31+
defer ticker.Stop()
32+
for {
33+
select {
34+
case <-ticker.C:
35+
items := o.queue.Flush()
36+
if len(items) > 0 {
37+
err := o.eventHandler(ctx, items)
38+
if err != nil {
39+
logrus.WithError(err).
40+
Error("golangfuse: error in event handler")
41+
}
42+
}
43+
case <-ctx.Done():
44+
return
45+
}
46+
}
47+
}

internal/observer/queue.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package observer
2+
3+
import "sync"
4+
5+
type Queue[T any] interface {
6+
Enqueue(event T)
7+
Flush() []T
8+
}
9+
10+
type queueImpl[T any] struct {
11+
items []T
12+
mu sync.Mutex
13+
}
14+
15+
func NewQueue[T any]() Queue[T] {
16+
return &queueImpl[T]{}
17+
}
18+
19+
func (q *queueImpl[T]) Enqueue(event T) {
20+
q.mu.Lock()
21+
defer q.mu.Unlock()
22+
q.items = append(q.items, event)
23+
}
24+
25+
func (q *queueImpl[T]) Flush() []T {
26+
q.mu.Lock()
27+
defer q.mu.Unlock()
28+
items := q.items
29+
q.items = []T{}
30+
return items
31+
}

pkg/client/client.go

Lines changed: 0 additions & 68 deletions
This file was deleted.

pkg/client/client_test.go

Lines changed: 0 additions & 116 deletions
This file was deleted.

pkg/client/models.go

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)